moonshine 0.2.0.pre → 0.2.1.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: 54b627ff20527b42c4931ca04f2d0fb952feb496
4
- data.tar.gz: 9c715207747d8e3669c398534e22ed649cc9c712
3
+ metadata.gz: aae2eb56018f3507bd3d5f36ded77930c1b1c7e6
4
+ data.tar.gz: b240d7956adb487b96eee495552fe492ade864c4
5
5
  SHA512:
6
- metadata.gz: 65703b07dc3896796d67aa220a35543e0335faeccaf5904cb68699a051444aa78066d98f985d63fa4a4437336f98aefd0c4313591415887436cc8e7f7cb4487d
7
- data.tar.gz: 246a7a139e67869f86ab9f5b72747d2f38700787919611b8c170f80c316736a7e0688aeb61beed676a07160150afd93a8081c8671b57dcadbc3de952062c6662
6
+ metadata.gz: 47b2be0dd1bf28acf80a0bb3224e383398aaf6250e1047685089b9e41bef8a5950d9c9745ae37aeb3375e1bb51d350a8f27f31793c74851ef8daddd9f333c26e
7
+ data.tar.gz: 4867578bd37b75012ff1c23ca5e3f7da42b8d5db571dc511dda9fca2e5e382ba3ce4a5c9d831cae744a93adfd6771d635ebc9d81c1533156059c1753b107905f
@@ -27,9 +27,9 @@ module Moonshine
27
27
  self.default_subject = subject
28
28
  end
29
29
 
30
- def param name, call: nil, transform: nil, default: nil, as_boolean: nil
30
+ def param(name, call: nil, **options, &block)
31
31
  self.default_chain ||= []
32
- self.default_chain << Moonshine::Filter.new(name, method_name: call || name, transform: transform, default: default, as_boolean: as_boolean)
32
+ self.default_chain << Moonshine::Filter.new(name, method_name: (call || name), **options, &block)
33
33
  end
34
34
  end
35
35
  end
@@ -1,40 +1,40 @@
1
1
  module Moonshine
2
2
  class Filter
3
3
 
4
- attr_accessor :name, :method_name, :transform, :default, :as_boolean
4
+ attr_accessor :name, :method_name, :options, :klass
5
5
 
6
- def initialize(name, method_name: nil, transform: nil, default: nil, as_boolean: nil, &block)
6
+ def initialize(name, method_name: nil, **options, &block)
7
7
  @name = name
8
- @method_name = method_name || block
9
- @transform = transform
10
- @default = default
11
- @as_boolean = as_boolean
8
+ @method_name = block || method_name
9
+ @options = options
12
10
  end
13
11
 
14
12
  def execute(klass)
15
- unless as_boolean
16
- args = set_transform(klass, set_default(klass.filters[name]))
17
- end
18
- return method_call(klass, args) if klass.filters[name] || default
13
+ @klass = klass
14
+ return method_call if klass.filters[name] || options[:default]
19
15
  klass.subject
20
16
  end
21
17
 
22
18
  private
23
19
 
24
- def method_call(klass, *args)
20
+ def method_call
25
21
  if method_name.is_a? Proc
26
- method_name.call(klass, *args)
22
+ method_name.call(klass.subject, *args)
27
23
  else
28
24
  klass.subject.send(method_name, *args)
29
25
  end
30
26
  end
31
27
 
28
+ def args
29
+ set_transform(set_default(klass.filters[name])) unless options[:as_boolean]
30
+ end
31
+
32
32
  def set_default value
33
- value || default
33
+ value || options[:default]
34
34
  end
35
35
 
36
- def set_transform klass, value
37
- return klass.send(transform, value) if transform
36
+ def set_transform value
37
+ return klass.send(options[:transform], value) if options[:transform]
38
38
  value
39
39
  end
40
40
  end
@@ -1,3 +1,3 @@
1
1
  module Moonshine
2
- VERSION = "0.2.0.pre"
2
+ VERSION = "0.2.1.pre"
3
3
  end
@@ -32,13 +32,13 @@ 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_name: :method, transform: nil, default: nil, as_boolean: nil)
35
+ Moonshine::Filter.expects(:new).with(:filter_name, method_name: :method)
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_name: :filter_name, transform: nil, default: nil, as_boolean: nil)
41
+ Moonshine::Filter.expects(:new).with(:filter_name, method_name: :filter_name)
42
42
  @chain_builder.param :filter_name
43
43
  end
44
44
 
@@ -22,7 +22,7 @@ describe Moonshine::Filter do
22
22
  end
23
23
 
24
24
  it 'return subject when default and value are nil' do
25
- filter.default = nil
25
+ filter.options[:default] = nil
26
26
  chain_builder_instance.filters = {}
27
27
  filter.execute(chain_builder_instance).must_equal chain_builder_instance.subject
28
28
  end
@@ -32,7 +32,7 @@ describe Moonshine::Filter do
32
32
  block = Proc.new { |subject, value| subject.some_method(value) }
33
33
  filter = Moonshine::Filter.new(:filter, &block)
34
34
  chain_builder_instance = @chain_builder.new({ filter: 1 })
35
- filter.method_name.expects(:call)
35
+ filter.method_name.expects(:call).with(chain_builder_instance.subject, 1)
36
36
  filter.execute(chain_builder_instance)
37
37
  end
38
38
  end
@@ -40,7 +40,7 @@ describe Moonshine::Filter do
40
40
  describe 'options' do
41
41
  describe 'transform' do
42
42
  it 'changes value with transform method from klass' do
43
- filter.transform = :transform_method
43
+ filter.options[:transform] = :transform_method
44
44
  chain_builder_instance.subject.stubs(:filter)
45
45
  chain_builder_instance.expects(:transform_method).with(1)
46
46
  filter.execute(chain_builder_instance)
@@ -49,21 +49,21 @@ describe Moonshine::Filter do
49
49
 
50
50
  describe 'default' do
51
51
  it 'uses default value if filter is nil' do
52
- filter.default = 2
52
+ filter.options[:default] = 2
53
53
  chain_builder_instance.filters = {}
54
54
  chain_builder_instance.subject.expects(:filter).with(2)
55
55
  filter.execute(chain_builder_instance)
56
56
  end
57
57
 
58
58
  it 'not use default value if filter is not nil' do
59
- filter.default = 2
59
+ filter.options[:default] = 2
60
60
  chain_builder_instance.filters = { filter: 1 }
61
61
  chain_builder_instance.subject.expects(:filter).with(1)
62
62
  filter.execute(chain_builder_instance)
63
63
  end
64
64
 
65
65
  it 'not sends filter if default and value are nil' do
66
- filter.default = nil
66
+ filter.options[:default] = nil
67
67
  chain_builder_instance.filters = {}
68
68
  chain_builder_instance.subject.expects(:filter).never
69
69
  filter.execute(chain_builder_instance)
@@ -72,14 +72,14 @@ describe Moonshine::Filter do
72
72
 
73
73
  describe 'as_boolean' do
74
74
  it 'sends method without value when true' do
75
- filter.as_boolean = true
75
+ filter.options[:as_boolean] = true
76
76
  chain_builder_instance.filters = { filter: true }
77
77
  chain_builder_instance.subject.expects(:filter)
78
78
  filter.execute(chain_builder_instance)
79
79
  end
80
80
 
81
81
  it 'sends method without value when false' do
82
- filter.as_boolean = true
82
+ filter.options[:as_boolean] = true
83
83
  chain_builder_instance.filters = { filter: false }
84
84
  chain_builder_instance.subject.expects(:filter).never
85
85
  filter.execute(chain_builder_instance)
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  describe Moonshine::VERSION do
4
4
 
5
- it 'is 0.1.0.pre' do
6
- Moonshine::VERSION.must_equal '0.1.0.pre'
5
+ it 'is 0.2.1.pre' do
6
+ Moonshine::VERSION.must_equal '0.2.1.pre'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonshine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre
4
+ version: 0.2.1.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessio Rocco