sinclair 1.8.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +20 -6
  3. data/.rubocop.yml +5 -2
  4. data/Dockerfile +1 -1
  5. data/README.md +25 -1
  6. data/config/check_specs.yml +1 -0
  7. data/config/yardstick.yml +9 -1
  8. data/lib/sinclair/comparable/class_methods.rb +33 -0
  9. data/lib/sinclair/comparable.rb +47 -0
  10. data/lib/sinclair/config_builder.rb +4 -2
  11. data/lib/sinclair/configurable.rb +2 -0
  12. data/lib/sinclair/equals_checker.rb +110 -0
  13. data/lib/sinclair/matchers/add_class_method.rb +27 -36
  14. data/lib/sinclair/matchers/add_instance_method.rb +59 -59
  15. data/lib/sinclair/matchers/add_method.rb +33 -35
  16. data/lib/sinclair/matchers/change_class_method.rb +22 -16
  17. data/lib/sinclair/matchers/change_instance_method.rb +46 -16
  18. data/lib/sinclair/matchers.rb +2 -8
  19. data/lib/sinclair/method_builder/call_method_builder.rb +49 -0
  20. data/lib/sinclair/method_builder.rb +4 -1
  21. data/lib/sinclair/method_definition/call_definition.rb +52 -0
  22. data/lib/sinclair/method_definition/string_definition.rb +0 -2
  23. data/lib/sinclair/method_definition.rb +40 -24
  24. data/lib/sinclair/method_definitions.rb +21 -1
  25. data/lib/sinclair/options/builder.rb +8 -0
  26. data/lib/sinclair/options.rb +1 -13
  27. data/lib/sinclair/version.rb +1 -1
  28. data/lib/sinclair.rb +2 -0
  29. data/sinclair.gemspec +1 -1
  30. data/spec/integration/readme/sinclair/comparable_spec.rb +24 -0
  31. data/spec/integration/yard/sinclair/comparable_spec.rb +24 -0
  32. data/spec/integration/yard/sinclair/equals_checker_spec.rb +51 -0
  33. data/spec/integration/yard/sinclair/matchers/change_class_method_spec.rb +24 -0
  34. data/spec/integration/yard/sinclair/matchers/change_instance_method_spec.rb +40 -0
  35. data/spec/lib/sinclair/comparable_spec.rb +202 -0
  36. data/spec/lib/sinclair/equals_checker_spec.rb +148 -0
  37. data/spec/lib/sinclair/method_builder/call_method_builder_spec.rb +76 -0
  38. data/spec/lib/sinclair/method_builder_spec.rb +63 -20
  39. data/spec/lib/sinclair/method_definition/call_definition_spec.rb +36 -0
  40. data/spec/lib/sinclair/method_definition_spec.rb +64 -0
  41. data/spec/lib/sinclair/method_definitions_spec.rb +79 -0
  42. data/spec/lib/sinclair/options/builder_spec.rb +13 -0
  43. data/spec/lib/sinclair/options/class_methods_spec.rb +23 -8
  44. data/spec/support/sample_model.rb +19 -0
  45. data/spec/support/shared_examples/attribute_accessor.rb +103 -0
  46. metadata +21 -5
@@ -42,4 +42,68 @@ describe Sinclair::MethodDefinition do
42
42
  end
43
43
  end
44
44
  end
45
+
46
+ describe '.for' do
47
+ context 'when there are no options nor block' do
48
+ let(:type) { :call }
49
+ let(:arguments) { %i[attr_reader some_attribute other_attribute] }
50
+
51
+ it do
52
+ expect(described_class.for(type, *arguments))
53
+ .to be_a(described_class)
54
+ end
55
+
56
+ it 'Returns an instance of the given type' do
57
+ expect(described_class.for(type, *arguments))
58
+ .to be_a(described_class::CallDefinition)
59
+ end
60
+
61
+ it 'initializes it correctly' do
62
+ expect(described_class.for(type, *arguments).code_string)
63
+ .to eq('attr_reader :some_attribute, :other_attribute')
64
+ end
65
+ end
66
+
67
+ context 'when a block is given' do
68
+ let(:type) { :block }
69
+ let(:method_name) { :the_method }
70
+ let(:block) { proc { 10 } }
71
+
72
+ it do
73
+ expect(described_class.for(type, method_name, &block))
74
+ .to be_a(described_class)
75
+ end
76
+
77
+ it 'Returns an instance of the given type' do
78
+ expect(described_class.for(type, method_name, &block))
79
+ .to be_a(described_class::BlockDefinition)
80
+ end
81
+
82
+ it 'initializes it correctly' do
83
+ expect(described_class.for(type, method_name, &block).name)
84
+ .to eq(method_name)
85
+ end
86
+ end
87
+
88
+ context 'when options are given' do
89
+ let(:type) { :string }
90
+ let(:method_name) { :the_method }
91
+ let(:code) { '10' }
92
+
93
+ it do
94
+ expect(described_class.for(type, method_name, code))
95
+ .to be_a(described_class)
96
+ end
97
+
98
+ it 'Returns an instance of the given type' do
99
+ expect(described_class.for(type, method_name, code))
100
+ .to be_a(described_class::StringDefinition)
101
+ end
102
+
103
+ it 'initializes it correctly' do
104
+ expect(described_class.for(type, method_name, code).name)
105
+ .to eq(method_name)
106
+ end
107
+ end
108
+ end
45
109
  end
@@ -42,4 +42,83 @@ describe Sinclair::MethodDefinitions do
42
42
  end
43
43
  end
44
44
  end
45
+
46
+ describe '#add_definition' do
47
+ context 'when there are no options nor block' do
48
+ let(:type) { :call }
49
+ let(:arguments) { %i[attr_reader some_attribute other_attribute] }
50
+
51
+ it do
52
+ expect(definitions.add_definition(type, *arguments))
53
+ .to be_a(Array)
54
+ end
55
+
56
+ it 'creates a new definition' do
57
+ expect(definitions.add_definition(type, *arguments).last)
58
+ .to be_a(Sinclair::MethodDefinition)
59
+ end
60
+
61
+ it 'creates a new definition of the chosen type' do
62
+ expect(definitions.add_definition(type, *arguments).last)
63
+ .to be_a(Sinclair::MethodDefinition::CallDefinition)
64
+ end
65
+
66
+ it 'initializes it correctly' do
67
+ expect(definitions.add_definition(type, *arguments).last.code_string)
68
+ .to eq('attr_reader :some_attribute, :other_attribute')
69
+ end
70
+ end
71
+
72
+ context 'when a block is given' do
73
+ let(:type) { :block }
74
+ let(:method_name) { :the_method }
75
+ let(:block) { proc { 10 } }
76
+
77
+ it do
78
+ expect(definitions.add_definition(type, method_name, &block))
79
+ .to be_a(Array)
80
+ end
81
+
82
+ it 'creates a new definition' do
83
+ expect(definitions.add_definition(type, method_name, &block).last)
84
+ .to be_a(Sinclair::MethodDefinition)
85
+ end
86
+
87
+ it 'creates a new definition of the chosen type' do
88
+ expect(definitions.add_definition(type, method_name, &block).last)
89
+ .to be_a(Sinclair::MethodDefinition::BlockDefinition)
90
+ end
91
+
92
+ it 'initializes it correctly' do
93
+ expect(definitions.add_definition(type, method_name, &block).last.name)
94
+ .to eq(method_name)
95
+ end
96
+ end
97
+
98
+ context 'when options are given' do
99
+ let(:type) { :string }
100
+ let(:method_name) { :the_method }
101
+ let(:code) { '10' }
102
+
103
+ it do
104
+ expect(definitions.add_definition(type, method_name, code))
105
+ .to be_a(Array)
106
+ end
107
+
108
+ it 'creates a new definition' do
109
+ expect(definitions.add_definition(type, method_name, code).last)
110
+ .to be_a(Sinclair::MethodDefinition)
111
+ end
112
+
113
+ it 'creates a new definition of the chosen type' do
114
+ expect(definitions.add_definition(type, method_name, code).last)
115
+ .to be_a(Sinclair::MethodDefinition::StringDefinition)
116
+ end
117
+
118
+ it 'initializes it correctly' do
119
+ expect(definitions.add_definition(type, method_name, code).last.name)
120
+ .to eq(method_name)
121
+ end
122
+ end
123
+ end
45
124
  end
@@ -40,6 +40,19 @@ describe Sinclair::Options::Builder do
40
40
  .not_to change { Sinclair::Options.invalid_options_in(test_keys) }
41
41
  end
42
42
 
43
+ context 'when the object is compared with others' do
44
+ before { klass.skip_validation }
45
+
46
+ let(:options1) { klass.new(timeout: 10) }
47
+ let(:options2) { klass.new(timeout: 15) }
48
+
49
+ it 'adds the field to the equals check' do
50
+ expect { builder.build }
51
+ .to change { options1 == options2 }
52
+ .from(true).to(false)
53
+ end
54
+ end
55
+
43
56
  context 'when when calling method after building' do
44
57
  before { builder.build }
45
58
 
@@ -27,11 +27,26 @@ describe Sinclair::Options::ClassMethods do
27
27
  .to([:invalid])
28
28
  end
29
29
 
30
+ context 'when the added fild makes an object different' do
31
+ let(:options1_hash) { { timeout: 10 } }
32
+ let(:options2_hash) { { timeout: 11 } }
33
+
34
+ before do
35
+ klass.skip_validation
36
+ end
37
+
38
+ it 'adds the field to == check' do
39
+ expect { klass.send(:with_options, :timeout) }
40
+ .to change { klass.new(options1_hash) == klass.new(options2_hash) }
41
+ .from(true).to(false)
42
+ end
43
+ end
44
+
30
45
  it do
31
46
  expect { klass.send(:with_options, :timeout, 'retries') }
32
47
  .not_to change {
33
- Sinclair::Options.invalid_options_in(%i[timeout retries invalid])
34
- }
48
+ Sinclair::Options.invalid_options_in(%i[timeout retries invalid])
49
+ }
35
50
  end
36
51
 
37
52
  context 'when when calling method after building' do
@@ -46,8 +61,8 @@ describe Sinclair::Options::ClassMethods do
46
61
  it do
47
62
  expect { klass.send(:with_options, :timeout, :retries) }
48
63
  .not_to change {
49
- klass.invalid_options_in(%i[timeout retries invalid])
50
- }
64
+ klass.invalid_options_in(%i[timeout retries invalid])
65
+ }
51
66
  end
52
67
  end
53
68
  end
@@ -90,16 +105,16 @@ describe Sinclair::Options::ClassMethods do
90
105
  it do
91
106
  expect { klass.send(:with_options, 'protocol', port: 443) }
92
107
  .to change {
93
- klass.invalid_options_in(test_keys)
94
- }.from(%i[protocol port invalid])
108
+ klass.invalid_options_in(test_keys)
109
+ }.from(%i[protocol port invalid])
95
110
  .to([:invalid])
96
111
  end
97
112
 
98
113
  it do
99
114
  expect { klass.send(:with_options, 'protocol', port: 443) }
100
115
  .not_to change {
101
- super_class.invalid_options_in(%i[protocol port])
102
- }
116
+ super_class.invalid_options_in(%i[protocol port])
117
+ }
103
118
  end
104
119
 
105
120
  context 'when overriding a method' do
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SampleModel
4
+ def initialize(name: nil, age: nil)
5
+ @name = name
6
+ @age = age
7
+ end
8
+
9
+ protected
10
+
11
+ attr_reader :name
12
+
13
+ private
14
+
15
+ attr_reader :age
16
+ end
17
+
18
+ class OtherModel < SampleModel
19
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'a method builder that adds attribute reader' do
4
+ let(:klass) { Class.new }
5
+ let(:instance) { klass.new }
6
+ let(:value) { Random.rand(10..20) }
7
+ let(:method_name) { :the_method }
8
+ let(:attributes) { [method_name] }
9
+
10
+ context 'when type is instance' do
11
+ let(:type) { Sinclair::MethodBuilder::INSTANCE_METHOD }
12
+
13
+ it do
14
+ expect { builder.build }
15
+ .to add_method(method_name).to(instance)
16
+ end
17
+
18
+ context 'when the method is built' do
19
+ before do
20
+ builder.build
21
+ instance.instance_variable_set("@#{method_name}", value)
22
+ end
23
+
24
+ it 'returns the value of the instance variable' do
25
+ expect(instance.the_method).to eq(value)
26
+ end
27
+ end
28
+ end
29
+
30
+ context 'when type is class' do
31
+ let(:type) { Sinclair::MethodBuilder::CLASS_METHOD }
32
+
33
+ it do
34
+ expect { builder.build }
35
+ .to add_class_method(method_name).to(klass)
36
+ end
37
+
38
+ context 'when the method is built' do
39
+ before do
40
+ builder.build
41
+ klass.instance_variable_set("@#{method_name}", value)
42
+ end
43
+
44
+ it 'returns the value of the instance variable' do
45
+ expect(klass.the_method).to eq(value)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ shared_examples 'a method builder that adds attribute writer' do
52
+ let(:klass) { Class.new }
53
+ let(:instance) { klass.new }
54
+ let(:value) { Random.rand }
55
+ let(:method_name) { :the_method }
56
+ let(:attributes) { [method_name] }
57
+
58
+ context 'when type is instance' do
59
+ let(:type) { Sinclair::MethodBuilder::INSTANCE_METHOD }
60
+
61
+ it do
62
+ expect { builder.build }
63
+ .to add_method("#{method_name}=").to(instance)
64
+ end
65
+
66
+ context 'when the method is built' do
67
+ let(:value) { Random.rand(10..20) }
68
+
69
+ before do
70
+ builder.build
71
+ end
72
+
73
+ it 'change the instance variable' do
74
+ expect { instance.the_method = value }
75
+ .to change { instance.instance_variable_get("@#{method_name}") }
76
+ .from(nil).to(value)
77
+ end
78
+ end
79
+ end
80
+
81
+ context 'when type is class' do
82
+ let(:type) { Sinclair::MethodBuilder::CLASS_METHOD }
83
+
84
+ it do
85
+ expect { builder.build }
86
+ .to add_class_method("#{method_name}=").to(klass)
87
+ end
88
+
89
+ context 'when the method is built' do
90
+ let(:value) { Random.rand(10..20) }
91
+
92
+ before do
93
+ builder.build
94
+ end
95
+
96
+ it 'change the instance variable' do
97
+ expect { klass.the_method = value }
98
+ .to change { klass.instance_variable_get("@#{method_name}") }
99
+ .from(nil).to(value)
100
+ end
101
+ end
102
+ end
103
+ 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.8.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-31 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 2.3.20
33
+ version: 2.3.25
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 2.3.20
40
+ version: 2.3.25
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -272,6 +272,8 @@ files:
272
272
  - config/yardstick.yml
273
273
  - docker-compose.yml
274
274
  - lib/sinclair.rb
275
+ - lib/sinclair/comparable.rb
276
+ - lib/sinclair/comparable/class_methods.rb
275
277
  - lib/sinclair/config.rb
276
278
  - lib/sinclair/config/methods_builder.rb
277
279
  - lib/sinclair/config_builder.rb
@@ -280,6 +282,7 @@ files:
280
282
  - lib/sinclair/configurable.rb
281
283
  - lib/sinclair/env_settable.rb
282
284
  - lib/sinclair/env_settable/builder.rb
285
+ - lib/sinclair/equals_checker.rb
283
286
  - lib/sinclair/exception.rb
284
287
  - lib/sinclair/input_hash.rb
285
288
  - lib/sinclair/matchers.rb
@@ -299,10 +302,12 @@ files:
299
302
  - lib/sinclair/method_builder.rb
300
303
  - lib/sinclair/method_builder/base.rb
301
304
  - lib/sinclair/method_builder/block_method_builder.rb
305
+ - lib/sinclair/method_builder/call_method_builder.rb
302
306
  - lib/sinclair/method_builder/string_method_builder.rb
303
307
  - lib/sinclair/method_definition.rb
304
308
  - lib/sinclair/method_definition/block_definition.rb
305
309
  - lib/sinclair/method_definition/block_helper.rb
310
+ - lib/sinclair/method_definition/call_definition.rb
306
311
  - lib/sinclair/method_definition/string_definition.rb
307
312
  - lib/sinclair/method_definitions.rb
308
313
  - lib/sinclair/options.rb
@@ -314,26 +319,32 @@ files:
314
319
  - sinclair.jpg
315
320
  - spec/integration/readme/my_class_spec.rb
316
321
  - spec/integration/readme/my_model_spec.rb
322
+ - spec/integration/readme/sinclair/comparable_spec.rb
317
323
  - spec/integration/readme/sinclair/configurable_spec.rb
318
324
  - spec/integration/readme/sinclair/env_settable_spec.rb
319
325
  - spec/integration/readme/sinclair/matchers_spec.rb
320
326
  - spec/integration/readme/sinclair/options_spec.rb
321
327
  - spec/integration/readme/sinclair_spec.rb
322
328
  - spec/integration/yard/my_builder_spec.rb
329
+ - spec/integration/yard/sinclair/comparable_spec.rb
323
330
  - spec/integration/yard/sinclair/config_builder_spec.rb
324
331
  - spec/integration/yard/sinclair/config_class_spec.rb
325
332
  - spec/integration/yard/sinclair/config_factory_spec.rb
326
333
  - spec/integration/yard/sinclair/config_spec.rb
327
334
  - spec/integration/yard/sinclair/configurable_spec.rb
328
335
  - spec/integration/yard/sinclair/env_settable_spec.rb
336
+ - spec/integration/yard/sinclair/equals_checker_spec.rb
329
337
  - spec/integration/yard/sinclair/input_hash_spec.rb
330
338
  - spec/integration/yard/sinclair/matchers/add_class_method_spec.rb
331
339
  - spec/integration/yard/sinclair/matchers/add_class_method_to_spec.rb
332
340
  - spec/integration/yard/sinclair/matchers/add_instance_method_spec.rb
333
341
  - spec/integration/yard/sinclair/matchers/add_instance_method_to_spec.rb
342
+ - spec/integration/yard/sinclair/matchers/change_class_method_spec.rb
343
+ - spec/integration/yard/sinclair/matchers/change_instance_method_spec.rb
334
344
  - spec/integration/yard/sinclair/options_parser_spec.rb
335
345
  - spec/integration/yard/sinclair/options_spec.rb
336
346
  - spec/integration/yard/sinclair_spec.rb
347
+ - spec/lib/sinclair/comparable_spec.rb
337
348
  - spec/lib/sinclair/config/methods_builder_spec.rb
338
349
  - spec/lib/sinclair/config_builder_spec.rb
339
350
  - spec/lib/sinclair/config_class_spec.rb
@@ -342,6 +353,7 @@ files:
342
353
  - spec/lib/sinclair/configurable_spec.rb
343
354
  - spec/lib/sinclair/env_settable/builder_spec.rb
344
355
  - spec/lib/sinclair/env_settable_spec.rb
356
+ - spec/lib/sinclair/equals_checker_spec.rb
345
357
  - spec/lib/sinclair/exception/invalid_options_spec.rb
346
358
  - spec/lib/sinclair/input_hash_spec.rb
347
359
  - spec/lib/sinclair/matchers/add_class_method_spec.rb
@@ -354,10 +366,12 @@ files:
354
366
  - spec/lib/sinclair/matchers/change_instance_method_spec.rb
355
367
  - spec/lib/sinclair/matchers_spec.rb
356
368
  - spec/lib/sinclair/method_builder/block_method_builder_spec.rb
369
+ - spec/lib/sinclair/method_builder/call_method_builder_spec.rb
357
370
  - spec/lib/sinclair/method_builder/string_method_builder_spec.rb
358
371
  - spec/lib/sinclair/method_builder_spec.rb
359
372
  - spec/lib/sinclair/method_definition/block_definition_spec.rb
360
373
  - spec/lib/sinclair/method_definition/block_helper_spec.rb
374
+ - spec/lib/sinclair/method_definition/call_definition_spec.rb
361
375
  - spec/lib/sinclair/method_definition/string_definition_spec.rb
362
376
  - spec/lib/sinclair/method_definition_spec.rb
363
377
  - spec/lib/sinclair/method_definitions_spec.rb
@@ -404,6 +418,8 @@ files:
404
418
  - spec/support/models/server_config.rb
405
419
  - spec/support/models/service_client.rb
406
420
  - spec/support/models/validator_builder.rb
421
+ - spec/support/sample_model.rb
422
+ - spec/support/shared_examples/attribute_accessor.rb
407
423
  - spec/support/shared_examples/class_method_definition.rb
408
424
  - spec/support/shared_examples/config.rb
409
425
  - spec/support/shared_examples/config_factory.rb
@@ -427,7 +443,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
427
443
  - !ruby/object:Gem::Version
428
444
  version: '0'
429
445
  requirements: []
430
- rubygems_version: 3.3.20
446
+ rubygems_version: 3.3.25
431
447
  signing_key:
432
448
  specification_version: 4
433
449
  summary: Gem for easy concern creation