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 +4 -4
- data/lib/moonshine/base.rb +2 -2
- data/lib/moonshine/filter.rb +15 -15
- data/lib/moonshine/version.rb +1 -1
- data/test/lib/moonshine/base_test.rb +2 -2
- data/test/lib/moonshine/filter_test.rb +8 -8
- data/test/lib/moonshine/version_test.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aae2eb56018f3507bd3d5f36ded77930c1b1c7e6
|
4
|
+
data.tar.gz: b240d7956adb487b96eee495552fe492ade864c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47b2be0dd1bf28acf80a0bb3224e383398aaf6250e1047685089b9e41bef8a5950d9c9745ae37aeb3375e1bb51d350a8f27f31793c74851ef8daddd9f333c26e
|
7
|
+
data.tar.gz: 4867578bd37b75012ff1c23ca5e3f7da42b8d5db571dc511dda9fca2e5e382ba3ce4a5c9d831cae744a93adfd6771d635ebc9d81c1533156059c1753b107905f
|
data/lib/moonshine/base.rb
CHANGED
@@ -27,9 +27,9 @@ module Moonshine
|
|
27
27
|
self.default_subject = subject
|
28
28
|
end
|
29
29
|
|
30
|
-
def param
|
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,
|
32
|
+
self.default_chain << Moonshine::Filter.new(name, method_name: (call || name), **options, &block)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/lib/moonshine/filter.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
module Moonshine
|
2
2
|
class Filter
|
3
3
|
|
4
|
-
attr_accessor :name, :method_name, :
|
4
|
+
attr_accessor :name, :method_name, :options, :klass
|
5
5
|
|
6
|
-
def initialize(name, method_name: nil,
|
6
|
+
def initialize(name, method_name: nil, **options, &block)
|
7
7
|
@name = name
|
8
|
-
@method_name =
|
9
|
-
@
|
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
|
-
|
16
|
-
|
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
|
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
|
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
|
data/lib/moonshine/version.rb
CHANGED
@@ -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
|
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
|
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)
|