homerun 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff66481985f29c18e9fa72cb2c695cc5e1a238a996df05030d29c3bc2fb6ce03
4
- data.tar.gz: b5b5c2b51fe364828ef8c5505f1633df720ea4c254ebd10a9fa421ee1196e3cd
3
+ metadata.gz: cb930c5f82071175ed28032b12a529bfe358e1b81a4fade4aba1704834f77f9f
4
+ data.tar.gz: 8a3febe2c97a12288497b8c5063805263a1610dc9b4a1893d51fa60f00f89272
5
5
  SHA512:
6
- metadata.gz: 641d66333ee0ce05259320b21deffcd7f9dbc7954410f0f4c7e5832bfc15f4940a13cf8a3121c3af0e1bd5c121afe7eec0811f40e930ab90572be95da7327992
7
- data.tar.gz: 377377068c3aff99ba650237349f6194df936fe7167f625e7e1c78c94af1eccae83c711ef34550b04c785609424da7f43a8e61869e335683c0ad682dee0160d1
6
+ metadata.gz: 1172118936a993829202af5fd1583b032198059f40356283b82933678723767fc56148be371eb0b46299c68e902bd364647057bb7cf249562388ff30d5518414
7
+ data.tar.gz: 9c638afb6ede813911872919d96e6002bb7e70ee793b17f6581fd9351ab9ebe3f704cca43286b3413c8b254dad980552ebfe90a1a962a147df6a011874a41d26
@@ -1,12 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- homerun (0.1.0)
4
+ homerun (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coderay (1.1.2)
10
+ method_source (0.9.2)
9
11
  minitest (5.11.3)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
10
15
  rake (10.5.0)
11
16
 
12
17
  PLATFORMS
@@ -16,6 +21,7 @@ DEPENDENCIES
16
21
  bundler (~> 1.17)
17
22
  homerun!
18
23
  minitest (~> 5.0)
24
+ pry (~> 0.12)
19
25
  rake (~> 10.0)
20
26
 
21
27
  BUNDLED WITH
data/README.md CHANGED
@@ -1,8 +1,22 @@
1
1
  # Homerun
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/homerun`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Sometimes you need to execute sequence of commands,
4
+ depends on execution result you want to do some redirection in execution,
5
+ even retry execution of some part of sequence.
6
+ "homerun" gem is exactly what you want.
4
7
 
5
- TODO: Delete this and the text above, and describe your gem
8
+ ```ruby
9
+ class A < Homerun::Instruction
10
+ step ->(ctx) { ctx[:greetings] += " hello" }
11
+ step ->(ctx) { ctx[:greetings] += " my"; false }, failure: :end
12
+ step ->(ctx) { ctx[:greetings] += " dear" }
13
+ step ->(ctx) { ctx[:greetings] += " friend" }, name: :end
14
+ end
15
+
16
+ A.call(greetings: "Oh,")
17
+
18
+ => "Oh, hello my friend"
19
+ ```
6
20
 
7
21
  ## Installation
8
22
 
@@ -22,7 +36,75 @@ Or install it yourself as:
22
36
 
23
37
  ## Usage
24
38
 
25
- TODO: Write usage instructions here
39
+ ### plain execution of sequence
40
+ ```ruby
41
+ class A < Homerun::Instruction
42
+ step ->(ctx) { ctx[:var] += 1 }
43
+ step ->(ctx) { ctx[:var] += 1 }
44
+ step ->(ctx) { ctx[:var] += 1 }
45
+ end
46
+
47
+ A.call(var: 0)
48
+ => { var: 3, _pass: true }
49
+ ```
50
+
51
+ ### false step catch & redirection to another step
52
+ ```ruby
53
+ class B < Homerun::Instruction
54
+ step ->(ctx) { false }, failure: :recover
55
+ step ->(ctx) { ctx[:var] += 3 }
56
+ step ->(ctx) { ctx[:var] += 1 }, name: :recover
57
+ end
58
+
59
+ B.call(var: 0)
60
+ => { var: 1, _pass: true }
61
+ ```
62
+
63
+ ### true step catch & redirection to another step
64
+ ```ruby
65
+ class B < Homerun::Instruction
66
+ step ->(ctx) { true }, success: :recover
67
+ step ->(ctx) { ctx[:var] += 3 }
68
+ step ->(ctx) { ctx[:var] += 1 }, name: :recover
69
+ end
70
+
71
+ B.call(var: 0)
72
+ => { var: 1, _pass: true }
73
+ ```
74
+ ### _pass as a result state (true/false)
75
+ ```ruby
76
+ class C < Homerun::Instruction
77
+ step ->(ctx) { false }
78
+ step ->(ctx) { ctx[:var] += 3 }
79
+ step ->(ctx) { ctx[:var] += 1}
80
+ end
81
+
82
+ C.call(var: 0)
83
+ => { var: 0, _pass: false }
84
+
85
+ class D < Homerun::Instruction
86
+ step ->(ctx) { ctx[:var] += 1 }
87
+ step ->(ctx) { ctx[:var] += 1 }
88
+ step ->(ctx) { false }
89
+ end
90
+
91
+ D.call(var: 0)
92
+ => { var: 2, _pass: false }
93
+ ```
94
+
95
+ ### block execution on _pass == true
96
+ ```ruby
97
+ class C < Homerun::Instruction
98
+ step ->(ctx) { ctx[:var] += 1 }
99
+ step ->(ctx) { ctx[:var] += 1 }
100
+ end
101
+
102
+ C.call(var: 0) do |ctx|
103
+ ctx[:var] += 1
104
+ end
105
+
106
+ => { var: 3, _pass: true }
107
+ ```
26
108
 
27
109
  ## Development
28
110
 
@@ -38,5 +38,6 @@ Gem::Specification.new do |spec|
38
38
 
39
39
  spec.add_development_dependency "bundler", "~> 1.17"
40
40
  spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "pry", "~> 0.12"
41
42
  spec.add_development_dependency "minitest", "~> 5.0"
42
43
  end
@@ -1,5 +1,6 @@
1
1
  require "homerun/version"
2
2
  require 'singleton'
3
+ require 'pry'
3
4
 
4
5
  module Homerun
5
6
  class Error < StandardError; end
@@ -7,25 +8,33 @@ module Homerun
7
8
  class Instruction
8
9
  include Singleton
9
10
 
10
- attr_reader :steps, :context
11
+ attr_reader :steps, :ctx
11
12
 
12
13
  def initialize
13
14
  super
14
15
 
15
16
  @steps = []
16
- @context = {}
17
+ @ctx = { _pass: true }
17
18
  end
18
19
 
19
20
  def add_step(step)
20
21
  @steps << step
21
22
  end
22
23
 
24
+ def set_ctx(_ctx)
25
+ @ctx = { **ctx, **_ctx }
26
+ end
27
+
28
+ def set_pass(val)
29
+ @ctx[:_pass] = val
30
+ end
31
+
23
32
  def self.step(item, failure: nil, success: nil, name: nil)
24
33
  instance.add_step({ item: item, failure: failure, success: success, name: name })
25
34
  end
26
35
 
27
- def self.call(ctx)
28
- @context = ctx.dup
36
+ def self.call(_ctx)
37
+ instance.set_ctx(_ctx)
29
38
 
30
39
  cur = 0
31
40
 
@@ -36,7 +45,7 @@ module Homerun
36
45
  instance.steps.index(instance.steps.find { |x| x[:name] == _name })
37
46
  }
38
47
 
39
- if step[:item].call(instance.context)
48
+ if step[:item].call(instance.ctx)
40
49
  if step[:success]
41
50
  cur = pos.call(step[:success])
42
51
  next
@@ -48,10 +57,17 @@ module Homerun
48
57
  cur = pos.call(step[:failure])
49
58
  next
50
59
  else
60
+ instance.set_pass(false)
51
61
  cur = instance.steps.count
52
62
  end
53
63
  end
54
64
  end
65
+
66
+ if block_given?
67
+ yield(instance.ctx) if instance.ctx[:_pass]
68
+ end
69
+
70
+ instance.ctx
55
71
  end
56
72
  end
57
73
  end
@@ -1,3 +1,3 @@
1
1
  module Homerun
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homerun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - skcc321
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-24 00:00:00.000000000 Z
11
+ date: 2019-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: minitest
43
57
  requirement: !ruby/object:Gem::Requirement