sinclair 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +4 -1
  3. data/Dockerfile +3 -3
  4. data/README.md +1 -1
  5. data/config/check_specs.yml +5 -0
  6. data/config/yardstick.yml +3 -0
  7. data/lib/sinclair.rb +26 -9
  8. data/lib/sinclair/config/methods_builder.rb +3 -1
  9. data/lib/sinclair/config_factory.rb +4 -2
  10. data/lib/sinclair/matchers/add_class_method_to.rb +1 -0
  11. data/lib/sinclair/method_builder.rb +61 -0
  12. data/lib/sinclair/method_builder/base.rb +67 -0
  13. data/lib/sinclair/method_builder/block_method_builder.rb +34 -0
  14. data/lib/sinclair/method_builder/string_method_builder.rb +50 -0
  15. data/lib/sinclair/method_definition.rb +38 -17
  16. data/lib/sinclair/method_definition/block_definition.rb +8 -94
  17. data/lib/sinclair/method_definition/block_helper.rb +46 -0
  18. data/lib/sinclair/method_definition/string_definition.rb +7 -66
  19. data/lib/sinclair/method_definitions.rb +16 -4
  20. data/lib/sinclair/version.rb +1 -1
  21. data/sinclair.gemspec +1 -1
  22. data/spec/lib/sinclair/method_builder/block_method_builder_spec.rb +55 -0
  23. data/spec/lib/sinclair/method_builder/string_method_builder_spec.rb +54 -0
  24. data/spec/lib/sinclair/method_builder_spec.rb +53 -0
  25. data/spec/lib/sinclair/method_definition/block_definition_spec.rb +66 -0
  26. data/spec/lib/sinclair/method_definition/block_helper_spec.rb +111 -0
  27. data/spec/lib/sinclair/method_definition/string_definition_spec.rb +70 -0
  28. data/spec/lib/sinclair/method_definition_spec.rb +23 -88
  29. data/spec/lib/sinclair/method_definitions_spec.rb +45 -0
  30. data/spec/support/models/random_generator.rb +13 -0
  31. metadata +18 -20
  32. data/lib/sinclair/method_definition/class_block_definition.rb +0 -22
  33. data/lib/sinclair/method_definition/class_method_definition.rb +0 -50
  34. data/lib/sinclair/method_definition/class_string_definition.rb +0 -24
  35. data/lib/sinclair/method_definition/instance_block_definition.rb +0 -22
  36. data/lib/sinclair/method_definition/instance_method_definition.rb +0 -48
  37. data/lib/sinclair/method_definition/instance_string_definition.rb +0 -24
  38. data/spec/integration/yard/sinclair/method_definition/class_block_definition_spec.rb +0 -34
  39. data/spec/integration/yard/sinclair/method_definition/class_method_definition_spec.rb +0 -28
  40. data/spec/integration/yard/sinclair/method_definition/class_string_definition_spec.rb +0 -23
  41. data/spec/integration/yard/sinclair/method_definition/instance_block_definition_spec.rb +0 -25
  42. data/spec/integration/yard/sinclair/method_definition/instance_method_definition_spec.rb +0 -35
  43. data/spec/integration/yard/sinclair/method_definition/instance_string_definition_spec.rb +0 -32
  44. data/spec/lib/sinclair/method_definition/class_block_definition_spec.rb +0 -29
  45. data/spec/lib/sinclair/method_definition/class_string_definition_spec.rb +0 -27
  46. data/spec/lib/sinclair/method_definition/instance_block_definition_spec.rb +0 -30
  47. data/spec/lib/sinclair/method_definition/instance_string_definition_spec.rb +0 -28
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::MethodDefinition::BlockDefinition do
6
+ subject(:definition) do
7
+ described_class.new(method_name, **options, &block)
8
+ end
9
+
10
+ let(:method_name) { :the_method }
11
+ let(:block) { proc { RandomGenerator.rand } }
12
+ let(:options) { {} }
13
+
14
+ describe '#method_block' do
15
+ let(:klass) { Class.new }
16
+ let(:instance) { klass.new }
17
+ let(:new_block) { definition.method_block }
18
+
19
+ it 'returns the block' do
20
+ expect(definition.method_block).to eq(block)
21
+ end
22
+
23
+ it 'returns a block with no cache' do
24
+ expect(instance.instance_eval(&new_block))
25
+ .not_to eq(instance.instance_eval(&new_block))
26
+ end
27
+
28
+ context 'when cache true is given' do
29
+ let(:options) { { cached: true } }
30
+
31
+ it 'returns the a new block' do
32
+ expect(definition.method_block).to be_a(Proc)
33
+ end
34
+
35
+ it 'returns a block with cache' do
36
+ expect(instance.instance_eval(&new_block))
37
+ .to eq(instance.instance_eval(&new_block))
38
+ end
39
+
40
+ it 'returns a block that does not cache nil' do
41
+ instance.instance_variable_set("@#{method_name}", nil)
42
+
43
+ expect(instance.instance_eval(&new_block)).not_to be_nil
44
+ end
45
+ end
46
+
47
+ context 'when cache full is given' do
48
+ let(:options) { { cached: :full } }
49
+
50
+ it 'returns the a new block' do
51
+ expect(definition.method_block).to be_a(Proc)
52
+ end
53
+
54
+ it 'returns a block with cache' do
55
+ expect(instance.instance_eval(&new_block))
56
+ .to eq(instance.instance_eval(&new_block))
57
+ end
58
+
59
+ it 'returns a block that does caches nil' do
60
+ instance.instance_variable_set("@#{method_name}", nil)
61
+
62
+ expect(instance.instance_eval(&new_block)).to be_nil
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::MethodDefinition::BlockHelper do
6
+ subject(:helper) { described_class }
7
+
8
+ let(:name) { :method_name }
9
+ let(:block) { proc { RandomGenerator.rand } }
10
+ let(:instance) { Class.new.new }
11
+
12
+ describe '.cached_method_proc' do
13
+ let(:method_block) do
14
+ helper.cached_method_proc(name, &block)
15
+ end
16
+
17
+ it do
18
+ expect(method_block).to be_a(Proc)
19
+ end
20
+
21
+ context 'when block is ran' do
22
+ it 'returns the value from block' do
23
+ expect(instance.instance_eval(&method_block))
24
+ .to be_a(Numeric)
25
+ end
26
+
27
+ it 'cashes result of the call' do
28
+ expect(instance.instance_eval(&method_block))
29
+ .to eq(instance.instance_eval(&method_block))
30
+ end
31
+
32
+ it 'sets variable' do
33
+ expect { instance.instance_eval(&method_block) }
34
+ .to change { instance.instance_variable_get("@#{name}") }
35
+ .from(nil)
36
+ end
37
+
38
+ context 'when instance has instance variable with value' do
39
+ before do
40
+ instance.instance_variable_set("@#{name}", Random.rand)
41
+ end
42
+
43
+ it 'does not change variable value' do
44
+ expect { instance.instance_eval(&method_block) }
45
+ .not_to change { instance.instance_variable_get("@#{name}") }
46
+ end
47
+ end
48
+
49
+ context 'when instance has instance variable with nil' do
50
+ before do
51
+ instance.instance_variable_set("@#{name}", nil)
52
+ end
53
+
54
+ it 'changes variable value' do
55
+ expect { instance.instance_eval(&method_block) }
56
+ .to change { instance.instance_variable_get("@#{name}") }
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '.full_cached_method_proc' do
63
+ let(:method_block) do
64
+ helper.full_cached_method_proc(name, &block)
65
+ end
66
+
67
+ it do
68
+ expect(method_block).to be_a(Proc)
69
+ end
70
+
71
+ context 'when block is ran' do
72
+ it 'returns the value from block' do
73
+ expect(instance.instance_eval(&method_block))
74
+ .to be_a(Numeric)
75
+ end
76
+
77
+ it 'cashes result of the call' do
78
+ expect(instance.instance_eval(&method_block))
79
+ .to eq(instance.instance_eval(&method_block))
80
+ end
81
+
82
+ it 'sets variable' do
83
+ expect { instance.instance_eval(&method_block) }
84
+ .to change { instance.instance_variable_get("@#{name}") }
85
+ .from(nil)
86
+ end
87
+
88
+ context 'when instance has instance variable with value' do
89
+ before do
90
+ instance.instance_variable_set("@#{name}", Random.rand)
91
+ end
92
+
93
+ it 'does not change variable value' do
94
+ expect { instance.instance_eval(&method_block) }
95
+ .not_to change { instance.instance_variable_get("@#{name}") }
96
+ end
97
+ end
98
+
99
+ context 'when instance has instance variable with nil' do
100
+ before do
101
+ instance.instance_variable_set("@#{name}", nil)
102
+ end
103
+
104
+ it 'does not change variable value' do
105
+ expect { instance.instance_eval(&method_block) }
106
+ .not_to change { instance.instance_variable_get("@#{name}") }
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::MethodDefinition::StringDefinition do
6
+ subject(:definition) do
7
+ described_class.new(method_name, code, **options)
8
+ end
9
+
10
+ let(:method_name) { :the_method }
11
+ let(:code) { 'Random.rand' }
12
+ let(:options) { {} }
13
+
14
+ describe '#code_line' do
15
+ let(:klass) { Class.new }
16
+ let(:instance) { klass.new }
17
+ let(:code_line) { definition.code_line }
18
+
19
+ it 'returns the code' do
20
+ expect(definition.code_line).to eq(code)
21
+ end
22
+
23
+ it 'returns a code with no cache' do
24
+ expect(instance.instance_eval(code_line))
25
+ .not_to eq(instance.instance_eval(code_line))
26
+ end
27
+
28
+ context 'when cache true is given' do
29
+ let(:options) { { cached: true } }
30
+
31
+ it 'returns the code with simple cache' do
32
+ expect(definition.code_line)
33
+ .to eq("@#{method_name} ||= #{code}")
34
+ end
35
+
36
+ it 'returns a code with cache' do
37
+ expect(instance.instance_eval(code_line))
38
+ .to eq(instance.instance_eval(code_line))
39
+ end
40
+
41
+ it 'returns a code that does not cache nil' do
42
+ instance.instance_variable_set("@#{method_name}", nil)
43
+
44
+ expect(instance.instance_eval(code_line)).not_to be_nil
45
+ end
46
+ end
47
+
48
+ context 'when cache full is given' do
49
+ let(:options) { { cached: :full } }
50
+ let(:expected) do
51
+ "defined?(@#{method_name}) ? @#{method_name} : (@#{method_name} = #{code})"
52
+ end
53
+
54
+ it 'returns the code with full cache' do
55
+ expect(definition.code_line).to eq(expected)
56
+ end
57
+
58
+ it 'returns a code with cache' do
59
+ expect(instance.instance_eval(code_line))
60
+ .to eq(instance.instance_eval(code_line))
61
+ end
62
+
63
+ it 'returns a code that caches nil' do
64
+ instance.instance_variable_set("@#{method_name}", nil)
65
+
66
+ expect(instance.instance_eval(code_line)).to be_nil
67
+ end
68
+ end
69
+ end
70
+ end
@@ -3,107 +3,42 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Sinclair::MethodDefinition do
6
- let(:klass) { Class.new }
7
- let(:instance) { klass.new }
6
+ let(:method_name) { :the_method }
8
7
 
9
- describe '#build' do
10
- let(:method_name) { :the_method }
8
+ describe '.default_value' do
9
+ subject(:klass) { Class.new(described_class) }
11
10
 
12
- context 'when instantiating the class itself' do
13
- subject(:method_definition) do
14
- described_class.new(method_name)
15
- end
11
+ let(:value) { Random.rand }
12
+ let(:instance) { klass.new(:other_method) }
16
13
 
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
14
+ it do
15
+ expect { klass.default_value(method_name, value) }
16
+ .to add_method(method_name).to(klass)
24
17
  end
25
18
 
26
- context 'when method was defined with a string for instance' do
27
- subject(:method_definition) do
28
- definition_class.new(method_name, code)
19
+ context 'when method is defined and called' do
20
+ before do
21
+ klass.default_value(method_name, value)
29
22
  end
30
23
 
31
- let(:definition_class) { described_class::InstanceStringDefinition }
32
-
33
- let(:code) { '@x = @x.to_i + 1' }
34
-
35
- it_behaves_like 'MethodDefinition#build without cache'
36
-
37
- context 'with cached options' do
38
- subject(:method_definition) do
39
- definition_class.new(method_name, code, cached: cached_option)
40
- end
41
-
42
- it_behaves_like 'MethodDefinition#build with cache options'
43
- end
44
- end
45
-
46
- context 'when method was defined with a block for instance' do
47
- subject(:method_definition) do
48
- definition_class.new(method_name) do
49
- @x = @x.to_i + 1
50
- end
51
- end
52
-
53
- let(:definition_class) { described_class::InstanceBlockDefinition }
54
-
55
- it_behaves_like 'MethodDefinition#build without cache'
56
-
57
- context 'with cached options' do
58
- subject(:method_definition) do
59
- definition_class.new(method_name, cached: cached_option) do
60
- @x = @x.to_i + 1
61
- end
62
- end
63
-
64
- it_behaves_like 'MethodDefinition#build with cache options'
24
+ it 'adds method that always return given value' do
25
+ expect(instance.the_method).to eq(value)
65
26
  end
66
27
  end
28
+ end
67
29
 
68
- context 'when method was defined with a string for class' do
69
- subject(:method_definition) do
70
- definition_class.new(method_name, code)
71
- end
72
-
73
- let(:definition_class) { described_class::ClassStringDefinition }
74
-
75
- let(:code) { '@x = @x.to_i + 1' }
76
-
77
- it_behaves_like 'ClassMethodDefinition#build without cache'
78
-
79
- context 'with cached options' do
80
- subject(:method_definition) do
81
- definition_class.new(method_name, code, cached: cached_option)
82
- end
83
-
84
- it_behaves_like 'ClassMethodDefinition#build with cache options'
30
+ describe '.from' do
31
+ context 'when passing a block' do
32
+ it do
33
+ expect(described_class.from(method_name) { 1 })
34
+ .to be_a(described_class::BlockDefinition)
85
35
  end
86
36
  end
87
37
 
88
- context 'when method was defined with a block for class' do
89
- subject(:method_definition) do
90
- definition_class.new(method_name) do
91
- @x = @x.to_i + 1
92
- end
93
- end
94
-
95
- let(:definition_class) { described_class::ClassBlockDefinition }
96
-
97
- it_behaves_like 'ClassMethodDefinition#build without cache'
98
-
99
- context 'with cached options' do
100
- subject(:method_definition) do
101
- definition_class.new(method_name, cached: cached_option) do
102
- @x = @x.to_i + 1
103
- end
104
- end
105
-
106
- it_behaves_like 'ClassMethodDefinition#build with cache options'
38
+ context 'when passing string' do
39
+ it do
40
+ expect(described_class.from(method_name, 'code'))
41
+ .to be_a(described_class::StringDefinition)
107
42
  end
108
43
  end
109
44
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::MethodDefinitions do
6
+ subject(:definitions) { described_class.new }
7
+
8
+ describe '#add' do
9
+ let(:name) { :the_method }
10
+
11
+ context 'when passing block' do
12
+ it 'returns the resulting array' do
13
+ expect(definitions.add(name) { RandomGenerator.rand })
14
+ .to be_a(Array)
15
+ end
16
+
17
+ it 'returns an array of MethodDefinition' do
18
+ expect(definitions.add(name) { RandomGenerator.rand }.first)
19
+ .to be_a(Sinclair::MethodDefinition)
20
+ end
21
+
22
+ it 'creates a new BlockDefinition' do
23
+ expect(definitions.add(name) { RandomGenerator.rand }.first)
24
+ .to be_a(Sinclair::MethodDefinition::BlockDefinition)
25
+ end
26
+ end
27
+
28
+ context 'when passing block' do
29
+ it 'returns the resulting array' do
30
+ expect(definitions.add(name, 'RandomGenerator.rand'))
31
+ .to be_a(Array)
32
+ end
33
+
34
+ it 'returns an array of MethodDefinition' do
35
+ expect(definitions.add(name, 'RandomGenerator.rand').last)
36
+ .to be_a(Sinclair::MethodDefinition)
37
+ end
38
+
39
+ it 'creates a new StringDefinition' do
40
+ expect(definitions.add(name, 'RandomGenerator.rand').last)
41
+ .to be_a(Sinclair::MethodDefinition::StringDefinition)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RandomGenerator
4
+ KAPA = 3.9 + Random.rand / 10.0
5
+
6
+ class << self
7
+ def rand
8
+ @rand ||= Random.rand
9
+
10
+ @rand = KAPA * @rand * (1 - @rand)
11
+ end
12
+ end
13
+ 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.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -226,14 +226,14 @@ dependencies:
226
226
  requirements:
227
227
  - - '='
228
228
  - !ruby/object:Gem::Version
229
- version: 0.9.19
229
+ version: 0.9.20
230
230
  type: :development
231
231
  prerelease: false
232
232
  version_requirements: !ruby/object:Gem::Requirement
233
233
  requirements:
234
234
  - - '='
235
235
  - !ruby/object:Gem::Version
236
- version: 0.9.19
236
+ version: 0.9.20
237
237
  - !ruby/object:Gem::Dependency
238
238
  name: yardstick
239
239
  requirement: !ruby/object:Gem::Requirement
@@ -266,6 +266,7 @@ files:
266
266
  - README.md
267
267
  - Rakefile
268
268
  - WARNINGS.md
269
+ - config/check_specs.yml
269
270
  - config/rubycritc.rb
270
271
  - config/yardstick.rb
271
272
  - config/yardstick.yml
@@ -284,14 +285,13 @@ files:
284
285
  - lib/sinclair/matchers/add_instance_method_to.rb
285
286
  - lib/sinclair/matchers/add_method.rb
286
287
  - lib/sinclair/matchers/add_method_to.rb
288
+ - lib/sinclair/method_builder.rb
289
+ - lib/sinclair/method_builder/base.rb
290
+ - lib/sinclair/method_builder/block_method_builder.rb
291
+ - lib/sinclair/method_builder/string_method_builder.rb
287
292
  - lib/sinclair/method_definition.rb
288
293
  - lib/sinclair/method_definition/block_definition.rb
289
- - lib/sinclair/method_definition/class_block_definition.rb
290
- - lib/sinclair/method_definition/class_method_definition.rb
291
- - lib/sinclair/method_definition/class_string_definition.rb
292
- - lib/sinclair/method_definition/instance_block_definition.rb
293
- - lib/sinclair/method_definition/instance_method_definition.rb
294
- - lib/sinclair/method_definition/instance_string_definition.rb
294
+ - lib/sinclair/method_definition/block_helper.rb
295
295
  - lib/sinclair/method_definition/string_definition.rb
296
296
  - lib/sinclair/method_definitions.rb
297
297
  - lib/sinclair/options_parser.rb
@@ -313,12 +313,6 @@ files:
313
313
  - spec/integration/yard/sinclair/matchers/add_class_method_to_spec.rb
314
314
  - spec/integration/yard/sinclair/matchers/add_instance_method_spec.rb
315
315
  - spec/integration/yard/sinclair/matchers/add_instance_method_to_spec.rb
316
- - spec/integration/yard/sinclair/method_definition/class_block_definition_spec.rb
317
- - spec/integration/yard/sinclair/method_definition/class_method_definition_spec.rb
318
- - spec/integration/yard/sinclair/method_definition/class_string_definition_spec.rb
319
- - spec/integration/yard/sinclair/method_definition/instance_block_definition_spec.rb
320
- - spec/integration/yard/sinclair/method_definition/instance_method_definition_spec.rb
321
- - spec/integration/yard/sinclair/method_definition/instance_string_definition_spec.rb
322
316
  - spec/integration/yard/sinclair/options_parser_spec.rb
323
317
  - spec/integration/yard/sinclair_spec.rb
324
318
  - spec/lib/sinclair/config/methods_builder_spec.rb
@@ -332,11 +326,14 @@ files:
332
326
  - spec/lib/sinclair/matchers/add_instance_method_spec.rb
333
327
  - spec/lib/sinclair/matchers/add_instance_method_to_spec.rb
334
328
  - spec/lib/sinclair/matchers_spec.rb
335
- - spec/lib/sinclair/method_definition/class_block_definition_spec.rb
336
- - spec/lib/sinclair/method_definition/class_string_definition_spec.rb
337
- - spec/lib/sinclair/method_definition/instance_block_definition_spec.rb
338
- - spec/lib/sinclair/method_definition/instance_string_definition_spec.rb
329
+ - spec/lib/sinclair/method_builder/block_method_builder_spec.rb
330
+ - spec/lib/sinclair/method_builder/string_method_builder_spec.rb
331
+ - spec/lib/sinclair/method_builder_spec.rb
332
+ - spec/lib/sinclair/method_definition/block_definition_spec.rb
333
+ - spec/lib/sinclair/method_definition/block_helper_spec.rb
334
+ - spec/lib/sinclair/method_definition/string_definition_spec.rb
339
335
  - spec/lib/sinclair/method_definition_spec.rb
336
+ - spec/lib/sinclair/method_definitions_spec.rb
340
337
  - spec/lib/sinclair/options_parser_spec.rb
341
338
  - spec/lib/sinclair_spec.rb
342
339
  - spec/spec_helper.rb
@@ -364,6 +361,7 @@ files:
364
361
  - spec/support/models/my_server_config.rb
365
362
  - spec/support/models/person.rb
366
363
  - spec/support/models/purchase.rb
364
+ - spec/support/models/random_generator.rb
367
365
  - spec/support/models/server.rb
368
366
  - spec/support/models/server_config.rb
369
367
  - spec/support/models/validator_builder.rb