composed_commands 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4775acaf75bfcfd71d058150498c180e733711f
4
- data.tar.gz: 7582413063cf65455d42cdf5a866ceed1650da52
3
+ metadata.gz: d209b58f7f8bb590af4b58a10dad9726850b1ab1
4
+ data.tar.gz: d8584a3b2eb4c06c6371f1c5747df59adb4d4e2d
5
5
  SHA512:
6
- metadata.gz: 26ff219030602949dfb7e7222abac747b8091cf9807e71a7c6925ed38fd8ead7dfdc0335475136ebeba93a1eb2ee356855689330e66559cd6d2b0551d26baf64
7
- data.tar.gz: 416b077022a893e30b9397c18f6c077920fd8b928ebc4f8281262d70b1234174d11610d3d9131712cf6a0d6baecf8da5931c0442a0d46c18d525a58fc814dd8c
6
+ metadata.gz: 77dd4802682c46749504b8b09fb3de1e051ba982b483271229b95765476c6ceb0f6becb27de4b4410911502e607574cee4c27b2b99d86016f647b88d4422a240
7
+ data.tar.gz: 4db9f234c3c57a93911306079dc290819d251e43bb194a8f47f29e2a12ea820287f59aaf97c4e63886e44673a81d990d1d708406636b9ecd63f1ebf7347f5559
data/README.md CHANGED
@@ -175,7 +175,27 @@ class SomeComposedCommand < ComposedCommands::ComposedCommand
175
175
  end
176
176
  ```
177
177
 
178
- This based on [Composable Operations](https://github.com/t6d/composable_operations) written by Konstantin Tennhard
178
+ You can configure part of Composed Command at runtime using `before_execute` callback':
179
+
180
+ ```ruby
181
+ class AbilityChecker < ComposedCommands::ComposedCommand
182
+ attribute :user, User, require: true
183
+
184
+ use Presence
185
+ use RegisteredAt, after: '2010-10-10
186
+ use Admin
187
+ # ...
188
+
189
+ def before_execute(command)
190
+ command.user = user
191
+ end
192
+ end
193
+
194
+ ability = AbilityChecker.new(user: current_user)
195
+ ability.perform(post)
196
+ ```
197
+
198
+ This gem based on [Composable Operations](https://github.com/t6d/composable_operations) written by Konstantin Tennhard
179
199
 
180
200
  ## Contributing
181
201
 
@@ -5,6 +5,7 @@ module ComposedCommands
5
5
  extend ActiveSupport::Autoload
6
6
 
7
7
  autoload :Command
8
+ autoload :CommandFactory
8
9
  autoload :ComposedCommand
9
10
  end
10
11
 
@@ -0,0 +1,13 @@
1
+ module ComposedCommands
2
+ class CommandFactory
3
+ attr_reader :command
4
+ def initialize(command, options = {})
5
+ @command = command
6
+ @options = options
7
+ end
8
+
9
+ def create
10
+ @command.new(@options)
11
+ end
12
+ end
13
+ end
@@ -13,12 +13,20 @@ module ComposedCommands
13
13
  end
14
14
 
15
15
  def self.use(klass, options = {})
16
- (@commands ||= []).push(klass.new(options))
16
+ (@commands ||= []).push(ComposedCommands::CommandFactory.new(klass, options))
17
+ end
18
+
19
+ def commands
20
+ self.class.commands.map do |command_factory|
21
+ command = command_factory.create
22
+ before_execute(command) if respond_to? :before_execute
23
+ command
24
+ end
17
25
  end
18
26
 
19
27
  protected
20
28
  def execute(*args)
21
- self.class.commands.inject(args) do |data, command|
29
+ commands.inject(args) do |data, command|
22
30
  command.perform(*Array(data))
23
31
 
24
32
  if command.halted?
@@ -1,3 +1,3 @@
1
1
  module ComposedCommands
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,6 +4,15 @@ RSpec.describe ComposedCommands::ComposedCommand do
4
4
  let(:input) { 'chunky bacon' }
5
5
 
6
6
  context '#perfrorm' do
7
+ it 'run factory creation callback' do
8
+ command = ChunkyBaconCapitalizer.new
9
+ def command.before_execute(command)
10
+ end
11
+ expect(command).to receive(:before_execute).twice
12
+
13
+ command.perform(input)
14
+ end
15
+
7
16
  it 'performs successfully' do
8
17
  command = ChunkyBaconCapitalizer.new
9
18
  command.perform(input)
@@ -43,16 +52,16 @@ RSpec.describe ComposedCommands::ComposedCommand do
43
52
  context '.use' do
44
53
  subject(:command) { ChunkyBaconCapitalizer }
45
54
  it { expect(command.commands.size).to eq 2 }
46
- it { expect(command.commands.first).to be_kind_of StringGenerator }
47
- it { expect(command.commands.last).to be_kind_of StringCapitalizer }
55
+ it { expect(command.commands.first.command).to eq StringGenerator }
56
+ it { expect(command.commands.last.command).to eq StringCapitalizer }
48
57
 
49
58
  context 'inherited compound commands' do
50
59
  subject(:child_command) { ChunkyBaconCapitalizerWithMuliplication }
51
60
 
52
61
  it { expect(child_command.commands.size).to eq 3 }
53
- it { expect(child_command.commands[0]).to be_kind_of StringGenerator }
54
- it { expect(child_command.commands[1]).to be_kind_of StringCapitalizer }
55
- it { expect(child_command.commands[2]).to be_kind_of StringMultiplier }
62
+ it { expect(child_command.commands[0].command).to eq StringGenerator }
63
+ it { expect(child_command.commands[1].command).to eq StringCapitalizer }
64
+ it { expect(child_command.commands[2].command).to eq StringMultiplier }
56
65
  end
57
66
  end
58
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composed_commands
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tema Bolshakov
@@ -112,12 +112,13 @@ files:
112
112
  - lib/composed_commands/command.rb
113
113
  - lib/composed_commands/command/execution.rb
114
114
  - lib/composed_commands/command/state.rb
115
+ - lib/composed_commands/command_factory.rb
115
116
  - lib/composed_commands/composed_command.rb
116
117
  - lib/composed_commands/version.rb
117
118
  - spec/compound_commands/command_attributes_spec.rb
118
119
  - spec/compound_commands/command_spec.rb
119
- - spec/compound_commands/compound_command_attributes_spec.rb
120
- - spec/compound_commands/compound_command_spec.rb
120
+ - spec/compound_commands/composed_command_attributes_spec.rb
121
+ - spec/compound_commands/composed_command_spec.rb
121
122
  - spec/fixtures/commans.rb
122
123
  - spec/spec_helper.rb
123
124
  homepage: ''
@@ -147,7 +148,7 @@ summary: Tool for creating commands and commands chains.
147
148
  test_files:
148
149
  - spec/compound_commands/command_attributes_spec.rb
149
150
  - spec/compound_commands/command_spec.rb
150
- - spec/compound_commands/compound_command_attributes_spec.rb
151
- - spec/compound_commands/compound_command_spec.rb
151
+ - spec/compound_commands/composed_command_attributes_spec.rb
152
+ - spec/compound_commands/composed_command_spec.rb
152
153
  - spec/fixtures/commans.rb
153
154
  - spec/spec_helper.rb