Skip to content

Commit

Permalink
V1
Browse files Browse the repository at this point in the history
- Adding option `run_until`
- Adding option `exec_step`
- Update readme
  • Loading branch information
Rukomoynikov authored Oct 20, 2023
1 parent 7194bb7 commit 9718197
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 40 deletions.
19 changes: 18 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude`
# on 2022-07-16 11:38:10 UTC using RuboCop version 0.93.1.
# on 2023-10-20 07:00:15 UTC using RuboCop version 0.93.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'lib/processable.rb'

# Offense count: 1
# Configuration parameters: IgnoredMethods, Max.
Metrics/AbcSize:
Exclude:
- 'lib/processable.rb'

# Offense count: 1
# Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
# ExcludedMethods: refine
Expand All @@ -14,6 +25,12 @@ Metrics/BlockLength:
- '**/*.gemspec'
- 'spec/processable_spec.rb'

# Offense count: 1
# Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
Metrics/MethodLength:
Exclude:
- 'lib/processable.rb'

# Offense count: 2
Style/ClassVars:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.5
42 changes: 22 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
processable (0.0.2)
processable (1.0.0)

GEM
remote: https://rubygems.org/
Expand All @@ -10,26 +10,28 @@ GEM
byebug (11.1.3)
diff-lcs (1.5.0)
docile (1.4.0)
parallel (1.22.1)
parser (3.1.2.0)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
regexp_parser (2.8.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (0.93.1)
parallel (~> 1.10)
parser (>= 2.7.1.5)
Expand All @@ -39,12 +41,12 @@ GEM
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.18.0)
parser (>= 3.1.1.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-rspec (1.44.1)
rubocop (~> 0.87)
rubocop-ast (>= 0.7.1)
ruby-progressbar (1.11.0)
ruby-progressbar (1.13.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -66,4 +68,4 @@ DEPENDENCIES
simplecov (~> 0.21.2)

BUNDLED WITH
2.2.3
2.4.20
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class PrintGithubRepos < Processable
repos_json.except(:title, :description)
end

step :print_to_console do |repos_json|
print repos_json
step :print_to_console do |result_from_prev_step, current_step, list_of_steps|
print result_from_prev_step
end
end

Expand All @@ -30,13 +30,14 @@ gem 'processable'
```

And then execute:

$ bundle install
```shell
bundle install
```

Or install it yourself as:

$ gem install processable

```shell
gem install processable
```
## Usage

**Define new class with required behavior**
Expand Down Expand Up @@ -72,6 +73,19 @@ To start the process simply call...
SiteScrapper.new.process
```

To run the process until some certain step you can pass the name of the step as an argument. Also keep in mind the step defined in run_until will not be executed as well.

```ruby
SiteScrapper.new.process(run_until: :save_to_database)
```

To exec only one particular step you can pass the name of the step as an argument.

```ruby
SiteScrapper.new.process(exec_step: :save_to_database)
```


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
29 changes: 23 additions & 6 deletions lib/processable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Processable
# Collections of steps like
# @type [String]
@@registered_steps = []
class Error < StandardError; end
class StepNotFoundError < StandardError; end

class << self
# Adding new step to the process
Expand All @@ -31,19 +31,36 @@ def step(name, &block)
end

# Run steps in order as they were defined
def process
@@registered_steps.each_with_index do |step, index|
previous_step = index.zero? ? {} : @@registered_steps[index - 1]
def process(run_until: nil)
steps_to_run = if run_until
run_until_index = @@registered_steps.find_index { |step_hash| step_hash[:name] == run_until }
raise StepNotFoundError, "Step '#{run_until}' not found" unless run_until_index

@@registered_steps[0..run_until_index]
else
@@registered_steps
end

steps_to_run.each_with_index do |step, index|
previous_step = index.zero? ? {} : steps_to_run[index - 1]

result = instance_exec(previous_step[:result],
step,
@@registered_steps,
steps_to_run,
&step[:block])

step[:result] = result
end

@@registered_steps.last[:result]
steps_to_run.last[:result]
end

def exec_step(step_name:, options:)
step = @@registered_steps.find { |step| step[:name] == step_name }

raise StepNotFoundError, "Step '#{step_name}' not found" unless step

instance_exec(options, step, @@registered_steps, &step[:block])
end

def initialize(*arguments); end
Expand Down
2 changes: 1 addition & 1 deletion processable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "processable"
spec.version = "0.0.2"
spec.version = "1.0.0"
spec.authors = ["Max Rukomoynikov"]
spec.email = ["[email protected]"]

Expand Down
25 changes: 20 additions & 5 deletions spec/processable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
# frozen_string_literal: true

RSpec.describe Processable do
describe "class level step method" do
let(:github_to_database_class) do
describe "developers can control which steps to call" do
let(:class_1) do
Class.new(Processable) do
step :get_repos do
step :step1 do
1
end

step :step2 do |previous_step_result|
previous_step_result + 2
end
end
end

it 'supports "step" class method' do
expect { github_to_database_class.new }.not_to raise_error
it "calls only until second step" do
expect(class_1.new.process(run_until: :step1)).to eq(1)
end

it "executes a specific step with provided option" do
expect(class_1.new.exec_step(step_name: :step2, options: 3)).to eq(5)
end

it "raises an error when an invalid step name is provided" do
expect do
class_1.new.exec_step(step_name: :invalid_step, options: 3)
end.to raise_error(Processable::StepNotFoundError)
end
end

Expand Down

0 comments on commit 9718197

Please sign in to comment.