mandate 0.2.0 → 0.3.0

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: 8e2c86ccab594b7a9c1df7a455ab4949c36f23b9face3e07b91beab615398ded
4
- data.tar.gz: 64fa8dbae3945ff3b5dc4fe7d5a1118dc4d64745351a3213a8297eedcebc2144
3
+ metadata.gz: 941eb8ffe29c4078c8137e331db4b798e856f3bb746b6a988a835c5010d5a5c7
4
+ data.tar.gz: 34ce742d20c88fb3cecde1f624268f47192113f17be3606ad42bff6d90c4b04b
5
5
  SHA512:
6
- metadata.gz: e06458866a07824be2b0901778cd0f65b73a0b33458de8e92995028c7a2720e906f29dfc3b7c17ffb0b7d79d3bfb6c5669caceba730610eee729f53f2e5803fc
7
- data.tar.gz: 007c3108ec701f6571ba7ea051d9c1933dc1dbe7645ccfd543a7173c43e9f29b428c12705726e776a1825df4bbfd58d3305983cb76b5b17639c0ffa19630c425
6
+ metadata.gz: 56b303c59f85575ad40b296682db47a97969631fd0611b2f536b665ad388981d218d5ca06c0ba5dfe2f8d25016bd57c1e8ed74ab889d32f6ab41d9d28b15db7b
7
+ data.tar.gz: e4d0917116cfb2a5dd346ded9db25c3f2ba5367913c9f9595dcea08bf3bff5e15fb85a1b8b74575886a8d2e2690c056f1795bf74e9a9de9f534265d7c0475d91
@@ -1,2 +1,5 @@
1
+ # 0.3.0 / 2019-09-06
2
+ * [FEATURE] Add success/failure callbacks
3
+
1
4
  # 0.2.0 / 2018-08-11
2
5
  * [FEATURE] Add initialize_with
data/README.md CHANGED
@@ -48,9 +48,9 @@ Multiplies.(20, 3)
48
48
 
49
49
  The `initialize_with` method creates an initializer and private attr_readers for the specified variables.
50
50
 
51
- For example `initialize_with :foo, :bar` is the equivelent of:
51
+ For example `initialize_with :foo, :bar` is the equivalent of:
52
52
 
53
- ```
53
+ ```ruby
54
54
  def initialize(foo, bar)
55
55
  @foo = foo
56
56
  @bar = bar
@@ -60,6 +60,52 @@ private
60
60
  attr_reader :foo, :bar
61
61
  ```
62
62
 
63
+ ### Using on_success/on_failure callbacks
64
+
65
+ Sometimes it is helpful for the class to return on_success/on_failure callbacks rather than just the resulting value.
66
+ This can be achieved by including the `Mandate::Callbacks` module as follows:
67
+
68
+ ```ruby
69
+ class Sumer
70
+ include Mandate
71
+ include Mandate::Callbacks
72
+
73
+ initialize_with :num1, :num2
74
+
75
+ def call
76
+ abort!("num1 must be an Integer") unless num1.is_a?(Integer)
77
+ abort!("num2 must be an Integer") unless num2.is_a?(Integer)
78
+
79
+ num1 + num2
80
+ end
81
+ end
82
+
83
+ res = Sumer.(1,2)
84
+ res.on_success { |result| p result } # puts 3
85
+ res.on_failure { |errors| p errors } # Noop
86
+ res.succeeded? # true
87
+ res.result # 3
88
+ res.errors # []
89
+
90
+ res = Sumer.("1","2")
91
+ res.on_success { |result| p result } # Noop
92
+ res.on_failure { |errors| p errors } # puts ["num1 must be an Integer"]
93
+
94
+ res = Sumer.("1","2")
95
+ res.on_failure { |errors| p errors } # puts ["num1 must be an Integer", "num2 must be an Integer"]
96
+ res.errors # ["num1 must be an Integer", "num2 must be an Integer"]
97
+ ```
98
+
99
+ It is also possible to chain methods, for example:
100
+
101
+ ```ruby
102
+ Sumer.(1,2).
103
+ on_success { |result| p result }.
104
+ on_failure { |errors| p errors }
105
+ ```
106
+
107
+ The `succeeded?` method is also aliased as `success?`.
108
+
63
109
  ## Development
64
110
 
65
111
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -68,4 +114,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
68
114
 
69
115
  ## Contributing
70
116
 
71
- Bug reports and pull requests are welcome on GitHub at https://github.com/ThalamusAI/mandate.
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/iHiD/mandate.
@@ -2,6 +2,7 @@ require "mandate/version"
2
2
  require "mandate/memoize"
3
3
  require "mandate/call_injector"
4
4
  require "mandate/initializer_injector"
5
+ require "mandate/callbacks"
5
6
 
6
7
  module Mandate
7
8
  def self.included(base)
@@ -0,0 +1,90 @@
1
+ module Mandate
2
+ module Callbacks
3
+ class AbortError < RuntimeError
4
+ end
5
+
6
+ class Results
7
+ attr_reader :result, :errors
8
+
9
+ def initialize
10
+ @succeeded = false
11
+ @errors = []
12
+ end
13
+
14
+ def succeeded!(result)
15
+ @result = result
16
+ @succeeded = true
17
+ end
18
+
19
+ def add_error(error)
20
+ errors << error
21
+ end
22
+
23
+ def succeeded?
24
+ !!succeeded
25
+ end
26
+ alias_method :success?, :succeeded?
27
+
28
+ def on_success(&block)
29
+ block.call(result) if succeeded?
30
+ self
31
+ end
32
+
33
+ def on_failure(&block)
34
+ block.call(errors) unless succeeded?
35
+ self
36
+ end
37
+
38
+ private
39
+ attr_reader :succeeded
40
+ end
41
+
42
+ def self.included(base)
43
+ # Override self.call to call the internal call_with_callbacks
44
+ # function which returns a method with on_success/on_failure callbacks
45
+ class << base
46
+ # Remove the existing created by the "include Mandate"
47
+ remove_method(:call)
48
+
49
+ # Define a new call methods which calls the instance call
50
+ # method but with the added callbacks needed for on_success/on_failure
51
+ def call(*args)
52
+ new(*args).call_with_callbacks
53
+ end
54
+ end
55
+
56
+ base.extend(Callbacks)
57
+ end
58
+
59
+ def self.extended(base)
60
+ base.send(:define_method, :call_with_callbacks) do
61
+ begin
62
+ # Create results object
63
+ @__mandate_results = Results.new
64
+
65
+ # Run the actual command
66
+ # If call fails, succeeded! will never get called
67
+ @__mandate_results.succeeded!(call)
68
+ rescue AbortError
69
+ end
70
+
71
+ @__mandate_results
72
+ end
73
+
74
+ private
75
+
76
+ base.send(:define_method, :add_error!) do |error|
77
+ @__mandate_results.add_error(error)
78
+ end
79
+
80
+ base.send(:define_method, :abort!) do |error = nil|
81
+ add_error!(error) if error
82
+ raise AbortError
83
+ end
84
+
85
+ base.send(:define_method, :abort_if_errored!) do
86
+ raise AbortError if @__mandate_results.errors.size > 0
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module Mandate
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = %q{A simple command-pattern helper gem for Ruby}
14
14
  spec.description = %q{This Ruby Gem adds functionality for the command pattern in Ruby, and for memoization.}
15
- spec.homepage = "https://github.com/thalamusai/mandate"
15
+ spec.homepage = "https://github.com/iHiD/mandate"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Walker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-13 00:00:00.000000000 Z
11
+ date: 2019-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,11 +71,12 @@ files:
71
71
  - bin/setup
72
72
  - lib/mandate.rb
73
73
  - lib/mandate/call_injector.rb
74
+ - lib/mandate/callbacks.rb
74
75
  - lib/mandate/initializer_injector.rb
75
76
  - lib/mandate/memoize.rb
76
77
  - lib/mandate/version.rb
77
78
  - mandate.gemspec
78
- homepage: https://github.com/thalamusai/mandate
79
+ homepage: https://github.com/iHiD/mandate
79
80
  licenses:
80
81
  - MIT
81
82
  metadata: {}
@@ -94,8 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.7.7
98
+ rubygems_version: 3.0.3
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: A simple command-pattern helper gem for Ruby