trailblazer-option 0.1.1 → 0.1.2

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: b6002d9d525ee513b07f18ba89480e2a69be11892664186e8020c085e0286bf2
4
- data.tar.gz: af25382707b80a01760a204861da7860ce4f60cd9a534b3015278de344cc8236
3
+ metadata.gz: a0d62e8bc54ba2cf87c8f8039a753a23ec1d1ae86c54345c44b049465ed6c7e4
4
+ data.tar.gz: 5a84e2737e097ac47fd952fdab0888413e77ef9586863eb25dfc7a4221bda2e4
5
5
  SHA512:
6
- metadata.gz: 1b3cfd6578fd2faf2a4d5e8f0df117ce387c01868e95056c3e6c0c4217129495ffd80f50a7ba05a166fcdefeab58931cccb5fbea845750b13c8b2e283044bc3d
7
- data.tar.gz: fe6c2ce74b8ed5aac51ab0bc4fa773f2b8408f993667cb01299310806ed2d867c5d9724375dabc326d515107f79e59f6690522b02ee56fc68941364a87a08265
6
+ metadata.gz: 6bff90fce5bc96d842ddfc81c7ce281a6abf1a474a35f02513d0dd764c3506a612ee92f88f2bdb55cd4ec0e12811554bcfd36408760b0536694d524942d3f96f
7
+ data.tar.gz: 469819438960ec5e67ff5020543399c79b795e5632b9bc57ef836f3b62aa2a029521f8346ad233e0c9d655691b8908f3916d658b2cca6fe19ed8db5b6e219d4e
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.2
2
+
3
+ * Evaluate callable at compile time itself to improve performance.
4
+
1
5
  # 0.1.1
2
6
 
3
7
  * Don't pass empty `keyword_arguments` for ruby <= 2.6.
data/README.md CHANGED
@@ -1 +1,62 @@
1
- # trailblazer-option
1
+ # Trailblazer::Option
2
+
3
+ _Dynamic options to evaluate at runtime._
4
+
5
+ `Trailblazer::Option` is the one of the core concept behind `traiblazer-operation`'s [step API](https://trailblazer.to/2.1/docs/activity.html#activity-wiring-api), `reform`'s [populator API](https://trailblazer.to/2.1/docs/reform.html#reform-populators) etc. It makes us possible to accept any kind of callable objects at compile time and execute them at runtime.
6
+
7
+ ```ruby
8
+ class Song::Create < Trailblazer::Operation
9
+ step Authorize # Module callable
10
+ step :model # Method callable
11
+ step ->(ctx, model:, **) { puts model } # Proc callable
12
+ end
13
+ ```
14
+
15
+ This gem is a replacement over [declarative-option](https://github.com/apotonick/declarative-option) and has been extracted out from [trailblazer-context](https://github.com/trailblazer/trailblazer-context) by identifying common callable patterns.
16
+
17
+ # Option
18
+
19
+ `Trailblazer::Option()` accepts `:symbol`, `lambda` and any other type of `callable` as an argument. It will be wrapped accordingly to make an executable, so you can call the value at runtime to evaluate it.
20
+
21
+ When passing in a `:symbol`, this will be treated as a method that's called on the given `exec_context`.
22
+
23
+ ```ruby
24
+ option = Trailblazer::Option(:object_id)
25
+ option.(exec_context: Object.new) #=> 2354383
26
+ ```
27
+
28
+ Same with objects responding to `.call` or `#call` method.
29
+
30
+ ```ruby
31
+ class CallMe
32
+ def self.call(*args, message:, **options)
33
+ message
34
+ end
35
+ end
36
+
37
+ option = Trailblazer::Option(CallMe)
38
+ option.(*args, keyword_arguments: { message: "hello!" }, exec_context: nil) => "hello!"
39
+ ```
40
+
41
+ Notice the usage of `keyword_arguments` while calling an `Option`. This is because keyword arguments needs to be forwarded separately in order for them to be [compatible](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/) with ruby 2.7+.
42
+
43
+ And of course, passing lambdas. They gets executed within given `exec_context` when set.
44
+
45
+ ```ruby
46
+ option = Trailblazer::Option( -> { object_id } )
47
+ option.(exec_context: Object.new) #=> 1234567
48
+ ```
49
+
50
+ # Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'trailblazer-option'
56
+ ```
57
+
58
+ # Copyright
59
+
60
+ Copyright (c) 2017-2021 TRAILBLAZER GmbH.
61
+
62
+ `trailblazer-option` is released under the [MIT License](http://www.opensource.org/licenses/MIT).
@@ -1,5 +1,5 @@
1
1
  module Trailblazer
2
2
  class Option
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -36,24 +36,18 @@ module Trailblazer
36
36
  call!(exec_context.method(value), *args, **options, &block)
37
37
  end
38
38
 
39
- # Choose appropriate evaluator and forward all arguments.
40
- # @private
41
- def self.evaluator(value, *args, **options, &block)
39
+ # Generic builder for a callable "option".
40
+ # @param call_implementation [Class, Module] implements the process of calling the proc
41
+ # while passing arguments/options to it in a specific style (e.g. kw args, step interface).
42
+ # @return [Proc] when called, this proc will evaluate its option (at run-time).
43
+ def self.build(value)
42
44
  evaluate = case value
43
45
  when Symbol then method(:evaluate_method)
44
46
  when Proc then method(:evaluate_proc)
45
47
  else method(:evaluate_callable)
46
48
  end
47
49
 
48
- evaluate.(value, *args, **options, &block)
49
- end
50
-
51
- # Generic builder for a callable "option".
52
- # @param call_implementation [Class, Module] implements the process of calling the proc
53
- # while passing arguments/options to it in a specific style (e.g. kw args, step interface).
54
- # @return [Proc] when called, this proc will evaluate its option (at run-time).
55
- def self.build(value)
56
- ->(*args, **options, &block) { evaluator(value, *args, **options, &block) }
50
+ ->(*args, **options, &block) { evaluate.(value, *args, **options, &block) }
57
51
  end
58
52
  end
59
53
 
@@ -0,0 +1,26 @@
1
+ require "test_helper"
2
+
3
+ class DocsOptionTest < Minitest::Spec
4
+ it do
5
+ #:method
6
+ option = Trailblazer::Option(:object_id)
7
+ option.(exec_context: Object.new) # => 1234567
8
+ #:method end
9
+
10
+ #:lambda
11
+ option = Trailblazer::Option(-> { object_id })
12
+ option.(exec_context: Object.new) # => 1234567
13
+ #:lambda end
14
+
15
+ #:module
16
+ class CallMe
17
+ def self.call(message:, **options)
18
+ message
19
+ end
20
+ end
21
+
22
+ option = Trailblazer::Option(CallMe)
23
+ option.(keyword_arguments: { message: "hello!" }, exec_context: nil) # => "hello!"
24
+ #:module end
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer-option
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-13 00:00:00.000000000 Z
11
+ date: 2021-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -72,6 +72,7 @@ files:
72
72
  - lib/trailblazer-option.rb
73
73
  - lib/trailblazer/option.rb
74
74
  - lib/trailblazer/option/version.rb
75
+ - test/docs/option_test.rb
75
76
  - test/option_test.rb
76
77
  - test/test_helper.rb
77
78
  - trailblazer-option.gemspec
@@ -99,5 +100,6 @@ signing_key:
99
100
  specification_version: 4
100
101
  summary: Callable patterns for options in Trailblazer
101
102
  test_files:
103
+ - test/docs/option_test.rb
102
104
  - test/option_test.rb
103
105
  - test/test_helper.rb