moonshine 0.1.0.pre → 0.2.0.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: 1f5558c94202fe30b961a00b82bd32f526282f02
4
- data.tar.gz: a514229cee0d1d404dffd55c7a554ffa47d165ff
3
+ metadata.gz: 54b627ff20527b42c4931ca04f2d0fb952feb496
4
+ data.tar.gz: 9c715207747d8e3669c398534e22ed649cc9c712
5
5
  SHA512:
6
- metadata.gz: 31c49e76e3032ef4ab86ccdc5de41dac75ba949679ab88653386b2429263bf6da1c956fc10fdefd459aedd13c35b656684b4123098beb4c1febfd53fd66c017e
7
- data.tar.gz: b478a9067cbc6bccb3202ea6aabc35561593f3537ede04cf3ec6b8488f5f41341c40869b2ef0892e7489903b33f55db158c94d2f179a9c183a2b6d5ee7f2b279
6
+ metadata.gz: 65703b07dc3896796d67aa220a35543e0335faeccaf5904cb68699a051444aa78066d98f985d63fa4a4437336f98aefd0c4313591415887436cc8e7f7cb4487d
7
+ data.tar.gz: 246a7a139e67869f86ab9f5b72747d2f38700787919611b8c170f80c316736a7e0688aeb61beed676a07160150afd93a8081c8671b57dcadbc3de952062c6662
@@ -29,7 +29,7 @@ module Moonshine
29
29
 
30
30
  def param name, call: nil, transform: nil, default: nil, as_boolean: nil
31
31
  self.default_chain ||= []
32
- self.default_chain << Moonshine::Filter.new(name, method: call || name, transform: transform, default: default, as_boolean: as_boolean)
32
+ self.default_chain << Moonshine::Filter.new(name, method_name: call || name, transform: transform, default: default, as_boolean: as_boolean)
33
33
  end
34
34
  end
35
35
  end
@@ -1,33 +1,34 @@
1
1
  module Moonshine
2
2
  class Filter
3
3
 
4
- attr_accessor :name, :method, :transform, :default, :as_boolean
4
+ attr_accessor :name, :method_name, :transform, :default, :as_boolean
5
5
 
6
- def initialize(name, method: nil, transform: nil, default: nil, as_boolean: nil)
6
+ def initialize(name, method_name: nil, transform: nil, default: nil, as_boolean: nil, &block)
7
7
  @name = name
8
- @method = method
8
+ @method_name = method_name || block
9
9
  @transform = transform
10
10
  @default = default
11
11
  @as_boolean = as_boolean
12
12
  end
13
13
 
14
14
  def execute(klass)
15
- if klass.filters[name] || default
16
- if as_boolean
17
- if klass.filters[name]
18
- return klass.subject.send(method)
19
- else
20
- return klass.subject.send(method) if default
21
- end
22
- else
23
- return klass.subject.send(method, set_transform(klass, set_default(klass.filters[name])))
24
- end
15
+ unless as_boolean
16
+ args = set_transform(klass, set_default(klass.filters[name]))
25
17
  end
18
+ return method_call(klass, args) if klass.filters[name] || default
26
19
  klass.subject
27
20
  end
28
21
 
29
22
  private
30
23
 
24
+ def method_call(klass, *args)
25
+ if method_name.is_a? Proc
26
+ method_name.call(klass, *args)
27
+ else
28
+ klass.subject.send(method_name, *args)
29
+ end
30
+ end
31
+
31
32
  def set_default value
32
33
  value || default
33
34
  end
@@ -1,3 +1,3 @@
1
1
  module Moonshine
2
- VERSION = "0.1.0.pre"
2
+ VERSION = "0.2.0.pre"
3
3
  end
@@ -32,15 +32,23 @@ describe Moonshine::Base do
32
32
 
33
33
  describe '.param' do
34
34
  it 'instantiates a Moonshine::Filter class' do
35
- Moonshine::Filter.expects(:new).with(:filter_name, method: :method, transform: nil, default: nil, as_boolean: nil)
35
+ Moonshine::Filter.expects(:new).with(:filter_name, method_name: :method, transform: nil, default: nil, as_boolean: nil)
36
36
  @chain_builder.param :filter_name, call: :method
37
37
  end
38
38
 
39
39
  describe 'when :call is not given' do
40
40
  it 'instantiates a Moonshine::Filter class with call same as first param' do
41
- Moonshine::Filter.expects(:new).with(:filter_name, method: :filter_name, transform: nil, default: nil, as_boolean: nil)
41
+ Moonshine::Filter.expects(:new).with(:filter_name, method_name: :filter_name, transform: nil, default: nil, as_boolean: nil)
42
42
  @chain_builder.param :filter_name
43
43
  end
44
+
45
+ describe 'and a block is given' do
46
+ it 'instantiates a Moonshine::Filter call with block as call' do
47
+ block = Proc.new{ |subject| subject }
48
+ Moonshine::Filter.expects(:new).with(:filter_name, &block)
49
+ @chain_builder.param :filter_name, &block
50
+ end
51
+ end
44
52
  end
45
53
 
46
54
  it 'adds filter to default_chain' do
@@ -6,11 +6,14 @@ describe Moonshine::Filter do
6
6
  @chain_builder = Class.new(Moonshine::Base) do
7
7
  subject default_subject
8
8
  param :name, call: :scope
9
+ param :block do |subject, value|
10
+ subject.some_method(value)
11
+ end
9
12
  end
10
13
  end
11
14
 
12
15
  describe '#execute' do
13
- let(:filter) { Moonshine::Filter.new(:filter, method: :filter) }
16
+ let(:filter) { Moonshine::Filter.new(:filter, method_name: :filter) }
14
17
  let(:chain_builder_instance) { @chain_builder.new({ filter: 1 }) }
15
18
 
16
19
  it 'sends scope to klass' do
@@ -24,6 +27,16 @@ describe Moonshine::Filter do
24
27
  filter.execute(chain_builder_instance).must_equal chain_builder_instance.subject
25
28
  end
26
29
 
30
+ describe 'when block is given' do
31
+ it 'calls block' do
32
+ block = Proc.new { |subject, value| subject.some_method(value) }
33
+ filter = Moonshine::Filter.new(:filter, &block)
34
+ chain_builder_instance = @chain_builder.new({ filter: 1 })
35
+ filter.method_name.expects(:call)
36
+ filter.execute(chain_builder_instance)
37
+ end
38
+ end
39
+
27
40
  describe 'options' do
28
41
  describe 'transform' do
29
42
  it 'changes value with transform method from klass' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonshine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre
4
+ version: 0.2.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessio Rocco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-01 00:00:00.000000000 Z
11
+ date: 2014-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler