sinclair 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -2
- data/Dockerfile +1 -1
- data/README.md +168 -15
- data/config/yardstick.yml +3 -0
- data/lib/sinclair.rb +56 -1
- data/lib/sinclair/method_definition.rb +27 -77
- data/lib/sinclair/method_definition/block_definition.rb +102 -0
- data/lib/sinclair/method_definition/string_definition.rb +82 -0
- data/lib/sinclair/version.rb +1 -1
- data/spec/integration/readme/sinclair/configurable_spec.rb +44 -0
- data/spec/integration/readme/sinclair_spec.rb +70 -0
- data/spec/integration/yard/sinclair/configurable_spec.rb +4 -0
- data/spec/integration/yard/sinclair/method_definition/block_definition_spec.rb +25 -0
- data/spec/integration/yard/sinclair/method_definition/string_definition_spec.rb +32 -0
- data/spec/integration/yard/sinclair/method_definition_spec.rb +2 -2
- data/spec/integration/yard/sinclair_spec.rb +30 -0
- data/spec/lib/sinclair/matchers/add_method_to_spec.rb +10 -0
- data/spec/lib/sinclair/method_definition/block_definition_spec.rb +30 -0
- data/spec/lib/sinclair/method_definition/string_definition_spec.rb +28 -0
- data/spec/lib/sinclair/method_definition_spec.rb +22 -63
- data/spec/support/models/default_value_builder.rb +21 -0
- data/spec/support/models/default_valueable.rb +11 -0
- data/spec/support/models/http_json_model.rb +27 -0
- data/spec/support/models/http_person.rb +11 -0
- data/spec/support/models/server.rb +18 -0
- data/spec/support/shared_examples/method_definition.rb +95 -0
- metadata +15 -2
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Sinclair::MethodDefinition::BlockDefinition do
|
6
|
+
let(:klass) { Class.new }
|
7
|
+
let(:instance) { klass.new }
|
8
|
+
|
9
|
+
describe '#build' do
|
10
|
+
subject(:method_definition) do
|
11
|
+
described_class.new(method_name) do
|
12
|
+
@x = @x.to_i + 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:method_name) { :the_method }
|
17
|
+
|
18
|
+
it_behaves_like 'MethodDefinition#build without cache'
|
19
|
+
|
20
|
+
context 'with cached options' do
|
21
|
+
subject(:method_definition) do
|
22
|
+
described_class.from(method_name, cached: cached_option) do
|
23
|
+
@x = @x.to_i + 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it_behaves_like 'MethodDefinition#build with cache options'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Sinclair::MethodDefinition::StringDefinition do
|
6
|
+
let(:klass) { Class.new }
|
7
|
+
let(:instance) { klass.new }
|
8
|
+
|
9
|
+
describe '#build' do
|
10
|
+
subject(:method_definition) do
|
11
|
+
described_class.new(method_name, code)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:method_name) { :the_method }
|
15
|
+
|
16
|
+
let(:code) { '@x = @x.to_i + 1' }
|
17
|
+
|
18
|
+
it_behaves_like 'MethodDefinition#build without cache'
|
19
|
+
|
20
|
+
context 'with cached options' do
|
21
|
+
subject(:method_definition) do
|
22
|
+
described_class.from(method_name, code, cached: cached_option)
|
23
|
+
end
|
24
|
+
|
25
|
+
it_behaves_like 'MethodDefinition#build with cache options'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -2,32 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
shared_examples 'MethodDefinition#build' do
|
6
|
-
it do
|
7
|
-
expect { method_definition.build(klass) }.to add_method(method_name).to(klass)
|
8
|
-
end
|
9
|
-
|
10
|
-
describe 'after build' do
|
11
|
-
before do
|
12
|
-
method_definition.build(klass)
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'adds the method to the klass instance' do
|
16
|
-
expect(instance).to respond_to(method_name)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'evaluates return of the method within the instance context' do
|
20
|
-
expect(instance.the_method).to eq(1)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'evaluates in the context of the instance' do
|
24
|
-
expect { instance.the_method }
|
25
|
-
.to change { instance.instance_variable_get(:@x) }
|
26
|
-
.from(nil).to(1)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
5
|
describe Sinclair::MethodDefinition do
|
32
6
|
let(:klass) { Class.new }
|
33
7
|
let(:instance) { klass.new }
|
@@ -35,70 +9,55 @@ describe Sinclair::MethodDefinition do
|
|
35
9
|
describe '#build' do
|
36
10
|
let(:method_name) { :the_method }
|
37
11
|
|
12
|
+
context 'when instantiating the class itself' do
|
13
|
+
subject(:method_definition) do
|
14
|
+
described_class.new(method_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
it do
|
18
|
+
expect { method_definition.build(klass) }.to raise_error(
|
19
|
+
RuntimeError,
|
20
|
+
'Build is implemented in subclasses. ' \
|
21
|
+
"Use #{described_class}.from to initialize a proper object"
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
38
26
|
context 'when method was defined with a string' do
|
39
27
|
subject(:method_definition) do
|
40
|
-
described_class.
|
28
|
+
described_class.from(method_name, code)
|
41
29
|
end
|
42
30
|
|
43
31
|
let(:code) { '@x = @x.to_i + 1' }
|
44
32
|
|
45
|
-
it_behaves_like 'MethodDefinition#build'
|
46
|
-
|
47
|
-
it 'creates a dynamic method' do
|
48
|
-
method_definition.build(klass)
|
49
|
-
expect { instance.the_method }.to change(instance, :the_method)
|
50
|
-
.from(1).to(3)
|
51
|
-
end
|
33
|
+
it_behaves_like 'MethodDefinition#build without cache'
|
52
34
|
|
53
35
|
context 'with cached options' do
|
54
36
|
subject(:method_definition) do
|
55
|
-
described_class.
|
37
|
+
described_class.from(method_name, code, cached: cached_option)
|
56
38
|
end
|
57
39
|
|
58
|
-
it_behaves_like 'MethodDefinition#build'
|
59
|
-
|
60
|
-
it 'creates a semi-dynamic method' do
|
61
|
-
method_definition.build(klass)
|
62
|
-
expect { instance.the_method }.not_to change(instance, :the_method)
|
63
|
-
end
|
40
|
+
it_behaves_like 'MethodDefinition#build with cache options'
|
64
41
|
end
|
65
42
|
end
|
66
43
|
|
67
44
|
context 'when method was defined with a block' do
|
68
45
|
subject(:method_definition) do
|
69
|
-
described_class.
|
46
|
+
described_class.from(method_name) do
|
70
47
|
@x = @x.to_i + 1
|
71
48
|
end
|
72
49
|
end
|
73
50
|
|
74
|
-
it_behaves_like 'MethodDefinition#build'
|
75
|
-
|
76
|
-
it 'creates a dynamic method' do
|
77
|
-
method_definition.build(klass)
|
78
|
-
expect { instance.the_method }.to change(instance, :the_method)
|
79
|
-
.from(1).to(3)
|
80
|
-
end
|
51
|
+
it_behaves_like 'MethodDefinition#build without cache'
|
81
52
|
|
82
53
|
context 'with cached options' do
|
83
54
|
subject(:method_definition) do
|
84
|
-
described_class.
|
55
|
+
described_class.from(method_name, cached: cached_option) do
|
85
56
|
@x = @x.to_i + 1
|
86
57
|
end
|
87
58
|
end
|
88
59
|
|
89
|
-
it_behaves_like 'MethodDefinition#build'
|
90
|
-
|
91
|
-
it 'creates a dynamic method' do
|
92
|
-
method_definition.build(klass)
|
93
|
-
expect { instance.the_method }.not_to change(instance, :the_method)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'sets the instance variable' do
|
97
|
-
method_definition.build(klass)
|
98
|
-
expect { instance.the_method }
|
99
|
-
.to change { instance.instance_variable_get("@#{method_name}") }
|
100
|
-
.from(nil).to(1)
|
101
|
-
end
|
60
|
+
it_behaves_like 'MethodDefinition#build with cache options'
|
102
61
|
end
|
103
62
|
end
|
104
63
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class DefaultValueBuilder < Sinclair
|
4
|
+
def add_default_values(*methods)
|
5
|
+
default_value = value
|
6
|
+
|
7
|
+
methods.each do |method|
|
8
|
+
add_method(method, cached: cache_type) { default_value }
|
9
|
+
end
|
10
|
+
|
11
|
+
build
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
delegate :accept_nil, :value, to: :options_object
|
17
|
+
|
18
|
+
def cache_type
|
19
|
+
accept_nil ? :full : :simple
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './spec/support/models/default_value_builder'
|
4
|
+
|
5
|
+
module DefaultValueable
|
6
|
+
def default_reader(*methods, value:, accept_nil: false)
|
7
|
+
DefaultValueBuilder.new(
|
8
|
+
self, value: value, accept_nil: accept_nil
|
9
|
+
).add_default_values(*methods)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class HttpJsonModel
|
4
|
+
attr_reader :json
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def parse(attribute, path: [])
|
8
|
+
builder = Sinclair.new(self)
|
9
|
+
|
10
|
+
keys = (path + [attribute]).map(&:to_s)
|
11
|
+
|
12
|
+
builder.add_method(attribute) do
|
13
|
+
keys.inject(hash) { |h, key| h[key] }
|
14
|
+
end
|
15
|
+
|
16
|
+
builder.build
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(json)
|
21
|
+
@json = json
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash
|
25
|
+
@hash ||= JSON.parse(json)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './spec/support/models/http_json_model'
|
4
|
+
|
5
|
+
class HttpPerson < HttpJsonModel
|
6
|
+
parse :uid
|
7
|
+
parse :name, path: [:personal_information]
|
8
|
+
parse :age, path: [:personal_information]
|
9
|
+
parse :username, path: [:digital_information]
|
10
|
+
parse :email, path: [:digital_information]
|
11
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './spec/support/models/default_valueable'
|
4
|
+
|
5
|
+
class Server
|
6
|
+
extend DefaultValueable
|
7
|
+
|
8
|
+
attr_writer :host, :port
|
9
|
+
|
10
|
+
default_reader :host, value: 'server.com', accept_nil: false
|
11
|
+
default_reader :port, value: 80, accept_nil: true
|
12
|
+
|
13
|
+
def url
|
14
|
+
return "http://#{host}" unless port
|
15
|
+
|
16
|
+
"http://#{host}:#{port}"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
shared_examples 'MethodDefinition#build' do
|
4
|
+
it do
|
5
|
+
expect { method_definition.build(klass) }.to add_method(method_name).to(klass)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'after build' do
|
9
|
+
before do
|
10
|
+
method_definition.build(klass)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'adds the method to the klass instance' do
|
14
|
+
expect(instance).to respond_to(method_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'evaluates return of the method within the instance context' do
|
18
|
+
expect(instance.the_method).to eq(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'evaluates in the context of the instance' do
|
22
|
+
expect { instance.the_method }
|
23
|
+
.to change { instance.instance_variable_get(:@x) }
|
24
|
+
.from(nil).to(1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
shared_examples 'MethodDefinition#build without cache' do
|
30
|
+
it_behaves_like 'MethodDefinition#build'
|
31
|
+
|
32
|
+
it 'creates a dynamic method' do
|
33
|
+
method_definition.build(klass)
|
34
|
+
expect { instance.the_method }.to change(instance, :the_method)
|
35
|
+
.from(1).to(3)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
shared_examples 'MethodDefinition#build with cache' do
|
40
|
+
it_behaves_like 'MethodDefinition#build'
|
41
|
+
|
42
|
+
it 'creates a semi-dynamic method' do
|
43
|
+
method_definition.build(klass)
|
44
|
+
expect { instance.the_method }.not_to change(instance, :the_method)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'sets the instance variable' do
|
48
|
+
method_definition.build(klass)
|
49
|
+
expect { instance.the_method }
|
50
|
+
.to change { instance.instance_variable_get("@#{method_name}") }
|
51
|
+
.from(nil).to(1)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
shared_examples 'MethodDefinition#build with cache options' do
|
56
|
+
context 'when cached is true' do
|
57
|
+
let(:cached_option) { true }
|
58
|
+
|
59
|
+
it_behaves_like 'MethodDefinition#build with cache'
|
60
|
+
|
61
|
+
context 'when instance variable has been set as nil' do
|
62
|
+
before do
|
63
|
+
method_definition.build(klass)
|
64
|
+
instance.instance_variable_set(:@the_method, nil)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns a new value' do
|
68
|
+
expect(instance.the_method).to eq(1)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when cached is full' do
|
74
|
+
let(:cached_option) { :full }
|
75
|
+
|
76
|
+
it_behaves_like 'MethodDefinition#build with cache'
|
77
|
+
|
78
|
+
context 'when instance variable has been set as nil' do
|
79
|
+
before do
|
80
|
+
method_definition.build(klass)
|
81
|
+
instance.instance_variable_set(:@the_method, nil)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'returns always nil' do
|
85
|
+
expect(instance.the_method).to be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'when cached is false' do
|
91
|
+
let(:cached_option) { false }
|
92
|
+
|
93
|
+
it_behaves_like 'MethodDefinition#build without cache'
|
94
|
+
end
|
95
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinclair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarthJee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -194,6 +194,8 @@ files:
|
|
194
194
|
- lib/sinclair/matchers/add_method.rb
|
195
195
|
- lib/sinclair/matchers/add_method_to.rb
|
196
196
|
- lib/sinclair/method_definition.rb
|
197
|
+
- lib/sinclair/method_definition/block_definition.rb
|
198
|
+
- lib/sinclair/method_definition/string_definition.rb
|
197
199
|
- lib/sinclair/options_parser.rb
|
198
200
|
- lib/sinclair/version.rb
|
199
201
|
- scripts/check_readme.sh
|
@@ -202,6 +204,7 @@ files:
|
|
202
204
|
- sinclair.jpg
|
203
205
|
- spec/integration/readme/my_class_spec.rb
|
204
206
|
- spec/integration/readme/my_model_spec.rb
|
207
|
+
- spec/integration/readme/sinclair/configurable_spec.rb
|
205
208
|
- spec/integration/readme/sinclair/matchers_spec.rb
|
206
209
|
- spec/integration/readme/sinclair_spec.rb
|
207
210
|
- spec/integration/sinclair/matchers_spec.rb
|
@@ -211,6 +214,8 @@ files:
|
|
211
214
|
- spec/integration/yard/sinclair/configurable_spec.rb
|
212
215
|
- spec/integration/yard/sinclair/matchers/add_method_spec.rb
|
213
216
|
- spec/integration/yard/sinclair/matchers/add_method_to_spec.rb
|
217
|
+
- spec/integration/yard/sinclair/method_definition/block_definition_spec.rb
|
218
|
+
- spec/integration/yard/sinclair/method_definition/string_definition_spec.rb
|
214
219
|
- spec/integration/yard/sinclair/method_definition_spec.rb
|
215
220
|
- spec/integration/yard/sinclair/options_parser_spec.rb
|
216
221
|
- spec/integration/yard/sinclair_spec.rb
|
@@ -220,16 +225,22 @@ files:
|
|
220
225
|
- spec/lib/sinclair/matchers/add_method_spec.rb
|
221
226
|
- spec/lib/sinclair/matchers/add_method_to_spec.rb
|
222
227
|
- spec/lib/sinclair/matchers_spec.rb
|
228
|
+
- spec/lib/sinclair/method_definition/block_definition_spec.rb
|
229
|
+
- spec/lib/sinclair/method_definition/string_definition_spec.rb
|
223
230
|
- spec/lib/sinclair/method_definition_spec.rb
|
224
231
|
- spec/lib/sinclair/options_parser_spec.rb
|
225
232
|
- spec/lib/sinclair_spec.rb
|
226
233
|
- spec/spec_helper.rb
|
227
234
|
- spec/support/fixture_helpers.rb
|
228
235
|
- spec/support/models/default_value.rb
|
236
|
+
- spec/support/models/default_value_builder.rb
|
237
|
+
- spec/support/models/default_valueable.rb
|
229
238
|
- spec/support/models/dummy_builder.rb
|
230
239
|
- spec/support/models/dummy_config.rb
|
231
240
|
- spec/support/models/dummy_configurable.rb
|
232
241
|
- spec/support/models/dummy_options_parser.rb
|
242
|
+
- spec/support/models/http_json_model.rb
|
243
|
+
- spec/support/models/http_person.rb
|
233
244
|
- spec/support/models/initial_valuer.rb
|
234
245
|
- spec/support/models/my_builder.rb
|
235
246
|
- spec/support/models/my_class.rb
|
@@ -239,7 +250,9 @@ files:
|
|
239
250
|
- spec/support/models/my_model.rb
|
240
251
|
- spec/support/models/person.rb
|
241
252
|
- spec/support/models/purchase.rb
|
253
|
+
- spec/support/models/server.rb
|
242
254
|
- spec/support/models/validator_builder.rb
|
255
|
+
- spec/support/shared_examples/method_definition.rb
|
243
256
|
homepage: https://github.com/darthjee/sinclair
|
244
257
|
licenses: []
|
245
258
|
metadata: {}
|