stack_commander 0.0.4 → 0.0.5.pre

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
  SHA1:
3
- metadata.gz: 193bc58f68f2558b4b761f2c229210af8cfb4ac8
4
- data.tar.gz: 93db5d8c010915aae4b2660d52dc23a4d8af2466
3
+ metadata.gz: 66aee20a0fb9f59498989df522fb6d69890ec944
4
+ data.tar.gz: 584761dd820d46a8b6b3dc67bd09e4f0a8ba90bd
5
5
  SHA512:
6
- metadata.gz: 8f58bdd6428f9f29e5691b7627edb6560e1b4414392b8e930fa032aef7935b7e30640d644469ea3eff2432a405eb4b0068bff5b04930909d6679960da8809a9d
7
- data.tar.gz: 0c1fd12293e2e411d3fa183fd6f36cbad7d534086b595803a2f94dc7c1eeb86bebde91b00ebff87b34ec6d0839df594b639cfdd9d8e28ca67959506809d52401
6
+ metadata.gz: 99bf87c6ae908ab8d872b3ecd1933627fefd2743835a54f105b7bfc57907262475285a9fbe3ce236d4f1064dfd4ea03b691feb22bcb3ea0f8c107668f535ea0e
7
+ data.tar.gz: 9df3ca90d6096473d7539464b56fa36eadcf1cb2690b537ca4350ed61c9e05aa13f1cf3bd42b4426dc0f893ef8d61510a60d3395b8f1df6b4cac5d803c3a31b6
@@ -1,5 +1,9 @@
1
1
  module StackCommander
2
2
  module Command
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
3
7
  def call(stack)
4
8
  action
5
9
  stack.call
@@ -18,5 +22,31 @@ module StackCommander
18
22
 
19
23
  def insurance
20
24
  end
25
+
26
+ def configuration
27
+ self.class.configuration
28
+ end
29
+
30
+ private
31
+
32
+ module ClassMethods
33
+ CONFIGURATION = :@@configuration
34
+
35
+ def configuration
36
+ class_variable_get(CONFIGURATION) if class_variable_defined?(CONFIGURATION)
37
+ end
38
+
39
+ def dependency_injection(&block)
40
+ class_variable_set(:@@dependency_injection, block)
41
+ end
42
+
43
+ # Creates an anonymous subclass of current command, configured with passed value
44
+ # then all instances of that command will have the same configuration
45
+ def [](value)
46
+ Class.new(self) do
47
+ class_variable_set(CONFIGURATION, value)
48
+ end
49
+ end
50
+ end
21
51
  end
22
52
  end
@@ -2,6 +2,8 @@ module StackCommander
2
2
  class DependencyInjection
3
3
  InvalidScope = Class.new(StandardError)
4
4
 
5
+ CVAR = :@@dependency_injection
6
+
5
7
  def initialize(klass)
6
8
  @klass = klass
7
9
  end
@@ -15,14 +17,23 @@ module StackCommander
15
17
  Array(parameters)
16
18
  end
17
19
 
18
- def matches?(scope)
19
- required_parameters.all? do |param|
20
+ def configured_parameters
21
+ parameters = @klass.class_variable_defined?(CVAR) && @klass.class_variable_get(CVAR)
22
+ parameters && parameters.respond_to?(:[]) ? parameters[:initialize] : parameters
23
+ end
24
+
25
+ def parameters
26
+ configured_parameters || required_parameters
27
+ end
28
+
29
+ def matching?(scope)
30
+ parameters.all? do |param|
20
31
  scope.respond_to?(param)
21
32
  end
22
33
  end
23
34
 
24
35
  def match!(scope)
25
- raise InvalidScope, scope unless matches?(scope)
36
+ raise InvalidScope, scope unless matching?(scope)
26
37
  end
27
38
 
28
39
  def extract(scope)
@@ -1,3 +1,3 @@
1
1
  module StackCommander
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5.pre'
3
3
  end
data/spec/command_spec.rb CHANGED
@@ -53,4 +53,42 @@ describe StackCommander::Command do
53
53
  end
54
54
  end
55
55
  end
56
+
57
+ context 'classes including command' do
58
+ subject(:command) { Class.new{ include StackCommander::Command } }
59
+
60
+ before do
61
+ stub_const('TestCommand', command)
62
+ end
63
+
64
+ it 'has name' do
65
+ expect(command.to_s).to eq('TestCommand')
66
+ end
67
+
68
+ it 'has configuration' do
69
+ expect(command.configuration).to be_nil
70
+ end
71
+
72
+ context 'with config' do
73
+ subject(:configured_command) { command[:test] }
74
+
75
+ expect_it { to be }
76
+
77
+ it 'does not have a name' do
78
+ expect(configured_command.to_s).not_to eq('TestCommand')
79
+ end
80
+
81
+ it 'has configuration' do
82
+ expect(configured_command.configuration).to eq(:test)
83
+ end
84
+
85
+ context 'an instance' do
86
+ subject(:instance) { configured_command.new }
87
+
88
+ it 'has configuration' do
89
+ expect(instance.configuration).to eq(:test)
90
+ end
91
+ end
92
+ end
93
+ end
56
94
  end
@@ -14,7 +14,7 @@ describe StackCommander::DependencyInjection do
14
14
  let(:scope) { double('scope', one: 'one value', two: 'two') }
15
15
 
16
16
  it do
17
- expect(di.matches?(scope)).to be
17
+ expect(di).to be_matching(scope)
18
18
  end
19
19
 
20
20
  it do
@@ -22,15 +22,30 @@ describe StackCommander::DependencyInjection do
22
22
  end
23
23
  end
24
24
 
25
- context 'scope does not have requried methods' do
25
+ context 'scope does not have required methods' do
26
26
  let(:scope) { double('scope', one: true) }
27
27
 
28
28
  it do
29
- expect(di.matches?(scope)).not_to be
29
+ expect(di).not_to be_matching(scope)
30
30
  end
31
31
 
32
32
  it do
33
33
  expect{ di.extract(scope) }.to raise_error(StackCommander::DependencyInjection::InvalidScope)
34
34
  end
35
35
  end
36
+
37
+ context 'class has configured parameters' do
38
+ before do
39
+ custom_class.class_variable_set :@@dependency_injection, proc { [:three, :four] }
40
+ end
41
+
42
+ it do
43
+ expect(di.parameters).to eq([:three, :four])
44
+ end
45
+
46
+ it 'matches new scope' do
47
+ scope = double(three: true, four: true)
48
+ expect(di).to be_matching(scope)
49
+ end
50
+ end
36
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stack_commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Cichra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-09 00:00:00.000000000 Z
11
+ date: 2014-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,9 +108,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
- - - ">="
111
+ - - ">"
112
112
  - !ruby/object:Gem::Version
113
- version: '0'
113
+ version: 1.3.1
114
114
  requirements: []
115
115
  rubyforge_project:
116
116
  rubygems_version: 2.2.2