moonshine 0.0.1.pre → 0.1.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +2 -2
- data/lib/moonshine/base.rb +20 -8
- data/lib/moonshine/filter.rb +36 -0
- data/lib/moonshine/version.rb +1 -1
- data/test/lib/moonshine/base_test.rb +83 -0
- data/test/lib/moonshine/filter_test.rb +78 -0
- data/test/lib/moonshine/version_test.rb +8 -0
- data/{spec → test}/test_helper.rb +8 -5
- metadata +10 -8
- data/spec/lib/moonshine/base_spec.rb +0 -49
- data/spec/lib/moonshine/version_spec.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f5558c94202fe30b961a00b82bd32f526282f02
|
4
|
+
data.tar.gz: a514229cee0d1d404dffd55c7a554ffa47d165ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31c49e76e3032ef4ab86ccdc5de41dac75ba949679ab88653386b2429263bf6da1c956fc10fdefd459aedd13c35b656684b4123098beb4c1febfd53fd66c017e
|
7
|
+
data.tar.gz: b478a9067cbc6bccb3202ea6aabc35561593f3537ede04cf3ec6b8488f5f41341c40869b2ef0892e7489903b33f55db158c94d2f179a9c183a2b6d5ee7f2b279
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Moonshine [![Build Status](https://travis-ci.org/nebulab/moonshine.png?branch=master)](https://travis-ci.org/nebulab/moonshine) [![Coverage Status](https://coveralls.io/repos/nebulab/moonshine/badge.png?branch=master)](https://coveralls.io/r/nebulab/moonshine?branch=master) [![Code Climate](https://codeclimate.com/github/nebulab/moonshine.png)](https://codeclimate.com/github/nebulab/moonshine)
|
1
|
+
# Moonshine [![Gem Version](https://badge.fury.io/rb/moonshine.png)](http://badge.fury.io/rb/moonshine) [![Build Status](https://travis-ci.org/nebulab/moonshine.png?branch=master)](https://travis-ci.org/nebulab/moonshine) [![Coverage Status](https://coveralls.io/repos/nebulab/moonshine/badge.png?branch=master)](https://coveralls.io/r/nebulab/moonshine?branch=master) [![Code Climate](https://codeclimate.com/github/nebulab/moonshine.png)](https://codeclimate.com/github/nebulab/moonshine)
|
2
2
|
|
3
3
|
TODO: Write a gem description
|
4
4
|
|
data/Rakefile
CHANGED
data/lib/moonshine/base.rb
CHANGED
@@ -2,22 +2,34 @@ module Moonshine
|
|
2
2
|
class Base
|
3
3
|
|
4
4
|
attr_accessor :filters
|
5
|
-
|
5
|
+
attr_reader :chain, :subject
|
6
6
|
|
7
7
|
def initialize(filters, subject = nil)
|
8
8
|
@filters = filters
|
9
|
-
@subject = subject || default_subject
|
9
|
+
@subject = subject || self.class.default_subject
|
10
|
+
@chain = self.class.default_chain || []
|
11
|
+
end
|
12
|
+
|
13
|
+
def add(filter)
|
14
|
+
@chain << filter
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
chain.each { |filter| @subject = filter.execute(self) }
|
19
|
+
@subject
|
10
20
|
end
|
11
21
|
|
12
22
|
class << self
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
23
|
+
|
24
|
+
attr_accessor :default_subject, :default_chain
|
25
|
+
|
26
|
+
def subject(subject)
|
27
|
+
self.default_subject = subject
|
17
28
|
end
|
18
29
|
|
19
|
-
def
|
20
|
-
|
30
|
+
def param name, call: nil, transform: nil, default: nil, as_boolean: nil
|
31
|
+
self.default_chain ||= []
|
32
|
+
self.default_chain << Moonshine::Filter.new(name, method: call || name, transform: transform, default: default, as_boolean: as_boolean)
|
21
33
|
end
|
22
34
|
end
|
23
35
|
end
|
data/lib/moonshine/filter.rb
CHANGED
@@ -1,4 +1,40 @@
|
|
1
1
|
module Moonshine
|
2
2
|
class Filter
|
3
|
+
|
4
|
+
attr_accessor :name, :method, :transform, :default, :as_boolean
|
5
|
+
|
6
|
+
def initialize(name, method: nil, transform: nil, default: nil, as_boolean: nil)
|
7
|
+
@name = name
|
8
|
+
@method = method
|
9
|
+
@transform = transform
|
10
|
+
@default = default
|
11
|
+
@as_boolean = as_boolean
|
12
|
+
end
|
13
|
+
|
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
|
25
|
+
end
|
26
|
+
klass.subject
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def set_default value
|
32
|
+
value || default
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_transform klass, value
|
36
|
+
return klass.send(transform, value) if transform
|
37
|
+
value
|
38
|
+
end
|
3
39
|
end
|
4
40
|
end
|
data/lib/moonshine/version.rb
CHANGED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Moonshine::Base do
|
4
|
+
before(:each) do
|
5
|
+
@chain_builder = Class.new(Moonshine::Base)
|
6
|
+
@default_subject = mock('default_subject')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.subject' do
|
10
|
+
before do
|
11
|
+
@chain_builder.default_subject = @default_subject
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'sets default subject class attribute' do
|
15
|
+
@chain_builder.default_subject.must_equal @default_subject
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'when it is initialized without a subject' do
|
19
|
+
it 'sets subject as default subject' do
|
20
|
+
@chain_builder.new({}).subject.must_equal @default_subject
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'when it is initialized with a subject' do
|
25
|
+
let(:subject){ mock('subject') }
|
26
|
+
|
27
|
+
it 'sets subject as requested' do
|
28
|
+
@chain_builder.new({}, subject).subject.must_equal subject
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.param' do
|
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)
|
36
|
+
@chain_builder.param :filter_name, call: :method
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'when :call is not given' do
|
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)
|
42
|
+
@chain_builder.param :filter_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'adds filter to default_chain' do
|
47
|
+
filter = mock('filter')
|
48
|
+
Moonshine::Filter.stubs(:new).returns(filter)
|
49
|
+
@chain_builder.param :filter_name, call: :scope
|
50
|
+
@chain_builder.default_chain.must_equal [filter]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#add' do
|
55
|
+
it 'adds filter to chain' do
|
56
|
+
filter = mock('filter')
|
57
|
+
chain_builder_instance = @chain_builder.new({})
|
58
|
+
chain_builder_instance.add(filter)
|
59
|
+
chain_builder_instance.chain.must_equal [filter]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#run' do
|
64
|
+
|
65
|
+
it 'run execute on each filter' do
|
66
|
+
filter1 = mock('filter1')
|
67
|
+
filter1.stubs(:name).returns(:filter1)
|
68
|
+
filter2 = mock('filter2')
|
69
|
+
filter2.stubs(:name).returns(:filter2)
|
70
|
+
filters = [filter1, filter2]
|
71
|
+
@chain_builder.default_chain = filters
|
72
|
+
chain_builder_instance = @chain_builder.new({ filter1: 1, filter2: 2})
|
73
|
+
filter1.expects(:execute).with(chain_builder_instance)
|
74
|
+
filter2.expects(:execute).with(chain_builder_instance)
|
75
|
+
chain_builder_instance.run
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'returns subject' do
|
79
|
+
chain_builder_instance = @chain_builder.new({})
|
80
|
+
chain_builder_instance.run.must_equal chain_builder_instance.subject
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Moonshine::Filter do
|
4
|
+
before(:each) do
|
5
|
+
default_subject = mock('default_subject')
|
6
|
+
@chain_builder = Class.new(Moonshine::Base) do
|
7
|
+
subject default_subject
|
8
|
+
param :name, call: :scope
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#execute' do
|
13
|
+
let(:filter) { Moonshine::Filter.new(:filter, method: :filter) }
|
14
|
+
let(:chain_builder_instance) { @chain_builder.new({ filter: 1 }) }
|
15
|
+
|
16
|
+
it 'sends scope to klass' do
|
17
|
+
chain_builder_instance.subject.expects(:filter).with(1)
|
18
|
+
filter.execute(chain_builder_instance)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'return subject when default and value are nil' do
|
22
|
+
filter.default = nil
|
23
|
+
chain_builder_instance.filters = {}
|
24
|
+
filter.execute(chain_builder_instance).must_equal chain_builder_instance.subject
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'options' do
|
28
|
+
describe 'transform' do
|
29
|
+
it 'changes value with transform method from klass' do
|
30
|
+
filter.transform = :transform_method
|
31
|
+
chain_builder_instance.subject.stubs(:filter)
|
32
|
+
chain_builder_instance.expects(:transform_method).with(1)
|
33
|
+
filter.execute(chain_builder_instance)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'default' do
|
38
|
+
it 'uses default value if filter is nil' do
|
39
|
+
filter.default = 2
|
40
|
+
chain_builder_instance.filters = {}
|
41
|
+
chain_builder_instance.subject.expects(:filter).with(2)
|
42
|
+
filter.execute(chain_builder_instance)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'not use default value if filter is not nil' do
|
46
|
+
filter.default = 2
|
47
|
+
chain_builder_instance.filters = { filter: 1 }
|
48
|
+
chain_builder_instance.subject.expects(:filter).with(1)
|
49
|
+
filter.execute(chain_builder_instance)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'not sends filter if default and value are nil' do
|
53
|
+
filter.default = nil
|
54
|
+
chain_builder_instance.filters = {}
|
55
|
+
chain_builder_instance.subject.expects(:filter).never
|
56
|
+
filter.execute(chain_builder_instance)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'as_boolean' do
|
61
|
+
it 'sends method without value when true' do
|
62
|
+
filter.as_boolean = true
|
63
|
+
chain_builder_instance.filters = { filter: true }
|
64
|
+
chain_builder_instance.subject.expects(:filter)
|
65
|
+
filter.execute(chain_builder_instance)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'sends method without value when false' do
|
69
|
+
filter.as_boolean = true
|
70
|
+
chain_builder_instance.filters = { filter: false }
|
71
|
+
chain_builder_instance.subject.expects(:filter).never
|
72
|
+
filter.execute(chain_builder_instance)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -1,9 +1,12 @@
|
|
1
|
-
require 'moonshine'
|
2
|
-
require 'minitest/unit'
|
3
|
-
require 'mocha/mini_test'
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require 'minitest/pride'
|
6
1
|
require 'coveralls'
|
7
2
|
|
8
3
|
# Code coverage
|
9
4
|
Coveralls.wear!
|
5
|
+
|
6
|
+
require 'moonshine'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'mocha/mini_test'
|
9
|
+
require 'minitest/pride'
|
10
|
+
|
11
|
+
class MiniTest::Spec
|
12
|
+
end
|
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.0.
|
4
|
+
version: 0.1.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-01
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,9 +99,10 @@ files:
|
|
99
99
|
- lib/moonshine/filter.rb
|
100
100
|
- lib/moonshine/version.rb
|
101
101
|
- moonshine.gemspec
|
102
|
-
-
|
103
|
-
-
|
104
|
-
-
|
102
|
+
- test/lib/moonshine/base_test.rb
|
103
|
+
- test/lib/moonshine/filter_test.rb
|
104
|
+
- test/lib/moonshine/version_test.rb
|
105
|
+
- test/test_helper.rb
|
105
106
|
homepage: https://github.com/nebulab/moonshine
|
106
107
|
licenses:
|
107
108
|
- MIT
|
@@ -127,6 +128,7 @@ signing_key:
|
|
127
128
|
specification_version: 4
|
128
129
|
summary: Conditional method chaining
|
129
130
|
test_files:
|
130
|
-
-
|
131
|
-
-
|
132
|
-
-
|
131
|
+
- test/lib/moonshine/base_test.rb
|
132
|
+
- test/lib/moonshine/filter_test.rb
|
133
|
+
- test/lib/moonshine/version_test.rb
|
134
|
+
- test/test_helper.rb
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Moonshine::Base do
|
4
|
-
|
5
|
-
describe '.subject' do
|
6
|
-
let(:mock_class){ MockClass.new({}) }
|
7
|
-
|
8
|
-
it 'defines default_subject method on instance' do
|
9
|
-
mock_class.must_respond_to(:default_subject)
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'sets default_subject' do
|
13
|
-
mock_class.default_subject.must_equal DefaultSubject
|
14
|
-
end
|
15
|
-
|
16
|
-
describe 'when it is initialized without a subject' do
|
17
|
-
it 'sets subject as default subject' do
|
18
|
-
mock_class.subject.must_be_instance_of DefaultSubject
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe 'when it is initialized with a subject' do
|
23
|
-
let(:mock_class){ MockClass.new({}, Subject.new) }
|
24
|
-
|
25
|
-
it 'sets subject as requested' do
|
26
|
-
mock_class.subject.must_be_instance_of Subject
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '.filter' do
|
32
|
-
it 'instantiates a Moonshine::Filter class' do
|
33
|
-
Moonshine::Filter.expects(:new).with(MockClass, :filter_name, :scope, transform: nil, default: nil)
|
34
|
-
MockClass.filter :filter_name, :scope
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
class Subject
|
42
|
-
end
|
43
|
-
|
44
|
-
class DefaultSubject
|
45
|
-
end
|
46
|
-
|
47
|
-
class MockClass < Moonshine::Base
|
48
|
-
subject DefaultSubject
|
49
|
-
end
|