sinclair 1.16.0 → 1.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/config/yardstick.yml +1 -0
- data/lib/sinclair/caster/class_methods.rb +11 -1
- data/lib/sinclair/caster.rb +3 -1
- data/lib/sinclair/equals_checker/reader.rb +58 -0
- data/lib/sinclair/equals_checker.rb +5 -4
- data/lib/sinclair/method_builder.rb +3 -2
- data/lib/sinclair/method_definition.rb +1 -1
- data/lib/sinclair/options/builder.rb +2 -2
- data/lib/sinclair/options_parser.rb +2 -0
- data/lib/sinclair/version.rb +1 -1
- data/lib/sinclair.rb +2 -2
- data/spec/lib/sinclair/caster_spec.rb +22 -1
- data/spec/lib/sinclair/equals_checker/reader_spec.rb +55 -0
- data/spec/lib/sinclair/equals_checker_spec.rb +12 -0
- data/spec/support/{sample_model.rb → models/sample_model.rb} +4 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 474f2601f89233396a82476209781ffb365ceb839756d70defeadade14a0b07e
|
4
|
+
data.tar.gz: 0de9aa5777826bb36c69e7499fce572f7dabcb2fff8d3fb322de75f71edcbf41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1b82cbba327c1310c7783b3223acb1d7d412dfc8af26f98993499104cd8aac30ba06cfc334f433c59d052631c7d28a76638b6894d8247811ad682d95a8d2c90
|
7
|
+
data.tar.gz: bf2039f736954188ee656c8f6d871a4d10546f9a9b6e80f5bdaa88f8a22cbf4ea94a650bb5d1101b4d68ab3fbf1553701d70f27613c8233f0fe699fc619add0f
|
data/README.md
CHANGED
@@ -15,13 +15,13 @@ create custom comparators, configure your application, create powerfull options,
|
|
15
15
|
|
16
16
|
Employing Sinclair in your applications helps you streamline your development workflow and enhance your development process through more efficient, cleaner code
|
17
17
|
|
18
|
-
Current Release: [1.16.
|
18
|
+
Current Release: [1.16.2](https://github.com/darthjee/sinclair/tree/1.16.2)
|
19
19
|
|
20
|
-
[Next release](https://github.com/darthjee/sinclair/compare/1.16.
|
20
|
+
[Next release](https://github.com/darthjee/sinclair/compare/1.16.2...master)
|
21
21
|
|
22
22
|
Yard Documentation
|
23
23
|
-------------------
|
24
|
-
[https://www.rubydoc.info/gems/sinclair/1.16.
|
24
|
+
[https://www.rubydoc.info/gems/sinclair/1.16.2](https://www.rubydoc.info/gems/sinclair/1.16.2)
|
25
25
|
|
26
26
|
Installation
|
27
27
|
---------------
|
data/config/yardstick.yml
CHANGED
@@ -57,6 +57,7 @@ rules:
|
|
57
57
|
- Sinclair::EnvSettable::Builder#initialize
|
58
58
|
- Sinclair::Exception::InvalidOptions#initialize
|
59
59
|
- Sinclair::EqualsChecker#initialize
|
60
|
+
- Sinclair::EqualsChecker::Reader#initialize
|
60
61
|
- Sinclair::InputHash#initialize
|
61
62
|
- Sinclair::Matchers::AddInstanceMethodTo#initialize
|
62
63
|
- Sinclair::Matchers::AddClassMethodTo#initialize
|
@@ -30,11 +30,21 @@ class Sinclair
|
|
30
30
|
def caster_for(key)
|
31
31
|
return casters[key] if casters.key?(key)
|
32
32
|
|
33
|
-
caster_for_class(key) || superclas_caster_for(key) ||
|
33
|
+
caster_for_class(key) || superclas_caster_for(key) || Sinclair::Caster.default
|
34
34
|
end
|
35
35
|
|
36
36
|
protected
|
37
37
|
|
38
|
+
# @private
|
39
|
+
# @api private
|
40
|
+
#
|
41
|
+
# Default caster that performs no casting returning the value itself
|
42
|
+
#
|
43
|
+
# @return [Caster]
|
44
|
+
def default
|
45
|
+
@default ||= new
|
46
|
+
end
|
47
|
+
|
38
48
|
# @api private
|
39
49
|
#
|
40
50
|
# Returns a caster from the superclass
|
data/lib/sinclair/caster.rb
CHANGED
@@ -317,7 +317,7 @@ class Sinclair
|
|
317
317
|
|
318
318
|
# @param block [Proc] Proc to be used when converting the value object
|
319
319
|
def initialize(&block)
|
320
|
-
@block = block
|
320
|
+
@block = block&.to_proc
|
321
321
|
end
|
322
322
|
|
323
323
|
# Cast a value using the given the set +block+
|
@@ -348,6 +348,8 @@ class Sinclair
|
|
348
348
|
#
|
349
349
|
# @return [Object] the result of the converting block
|
350
350
|
def cast(value, **opts)
|
351
|
+
return value unless block
|
352
|
+
|
351
353
|
options = opts.select do |key, _|
|
352
354
|
options_keys.include?(key)
|
353
355
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sinclair
|
4
|
+
class EqualsChecker
|
5
|
+
# @api private
|
6
|
+
# @author darthjee
|
7
|
+
#
|
8
|
+
# Class capable of reading an attribute from models
|
9
|
+
class Reader
|
10
|
+
# @private
|
11
|
+
# @api private
|
12
|
+
#
|
13
|
+
# Checks if two attributes from 2 object match
|
14
|
+
#
|
15
|
+
# @param attribute [Symbol] attribute name
|
16
|
+
# @param model [Object] object to be compared with other
|
17
|
+
# @param other [Object] object to be compared with model
|
18
|
+
#
|
19
|
+
# @see #match?
|
20
|
+
# @return [TrueClass,FalseClass]
|
21
|
+
def self.attributes_match?(attribute, model, other)
|
22
|
+
reader = new(attribute)
|
23
|
+
|
24
|
+
reader.read_from(model) == reader.read_from(other)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param attribute [Symbol] name of the attribute (method or variable)
|
28
|
+
# to be accessed in the models
|
29
|
+
def initialize(attribute)
|
30
|
+
@attribute = attribute
|
31
|
+
end
|
32
|
+
|
33
|
+
# Reads the +attribute+ from the model
|
34
|
+
#
|
35
|
+
# When attribute is a method name, calls that method on the model
|
36
|
+
#
|
37
|
+
# When attribute is an instance variable name, that is read directly from the model
|
38
|
+
#
|
39
|
+
# @param model [Object] the model to be read
|
40
|
+
#
|
41
|
+
# @return [Object]
|
42
|
+
def read_from(model)
|
43
|
+
return model.send(attribute) unless attribute.to_s.match?(/^@.*/)
|
44
|
+
|
45
|
+
model.instance_variable_get(attribute)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# @attr_reader :attribute
|
51
|
+
#
|
52
|
+
# Reads the attribute that will be used to extract the value
|
53
|
+
#
|
54
|
+
# @return [Symbol]
|
55
|
+
attr_reader :attribute
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -50,6 +50,8 @@ class Sinclair
|
|
50
50
|
#
|
51
51
|
# checker.match?(model1, model2) # returns false
|
52
52
|
class EqualsChecker
|
53
|
+
autoload :Reader, 'sinclair/equals_checker/reader'
|
54
|
+
|
53
55
|
# @param attributes [Array<Symbol,String>] list of relevant attributes
|
54
56
|
def initialize(*attributes)
|
55
57
|
@attributes = Set.new(attributes.flatten)
|
@@ -91,20 +93,19 @@ class Sinclair
|
|
91
93
|
return false unless model.class == other.class
|
92
94
|
|
93
95
|
attributes.all? do |attr|
|
94
|
-
|
96
|
+
Reader.attributes_match?(attr, model, other)
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
98
100
|
private
|
99
101
|
|
100
|
-
attr_reader :attributes
|
101
|
-
|
102
102
|
# @private
|
103
103
|
# @api private
|
104
|
-
# @
|
104
|
+
# @attr_reader attributes
|
105
105
|
#
|
106
106
|
# attributes relevant for checking difference
|
107
107
|
#
|
108
108
|
# @return [Set<Symbol,String>]
|
109
|
+
attr_reader :attributes
|
109
110
|
end
|
110
111
|
end
|
@@ -36,13 +36,14 @@ class Sinclair
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
attr_reader
|
40
|
-
# @method klass
|
39
|
+
# @attr_reader klass
|
41
40
|
# @private
|
42
41
|
# @api private
|
42
|
+
# @!visibility public
|
43
43
|
#
|
44
44
|
# class to receive the method
|
45
45
|
#
|
46
46
|
# @return [Class]
|
47
|
+
attr_reader :klass
|
47
48
|
end
|
48
49
|
end
|
@@ -43,14 +43,14 @@ class Sinclair
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
-
attr_reader :attributes
|
47
|
-
# @method attributes
|
48
46
|
# @api private
|
49
47
|
# @private
|
48
|
+
# @attr_reader attributes
|
50
49
|
#
|
51
50
|
# Options attributes
|
52
51
|
#
|
53
52
|
# @return [Hash<Symbol.Object>]
|
53
|
+
attr_reader :attributes
|
54
54
|
|
55
55
|
# Add all methods for options
|
56
56
|
#
|
data/lib/sinclair/version.rb
CHANGED
data/lib/sinclair.rb
CHANGED
@@ -457,14 +457,14 @@ class Sinclair
|
|
457
457
|
|
458
458
|
private
|
459
459
|
|
460
|
-
|
461
|
-
# @method klass
|
460
|
+
# @!visibility public
|
462
461
|
# @api private
|
463
462
|
# @private
|
464
463
|
#
|
465
464
|
# Class that will receive the methods
|
466
465
|
#
|
467
466
|
# @return [Class]
|
467
|
+
attr_reader :klass
|
468
468
|
|
469
469
|
# @private
|
470
470
|
# @api private
|
@@ -6,8 +6,29 @@ describe Sinclair::Caster do
|
|
6
6
|
subject(:caster) { caster_class.new(&method_name) }
|
7
7
|
|
8
8
|
let(:caster_class) { Class.new(described_class) }
|
9
|
+
let(:value) { values.sample }
|
10
|
+
let(:values) do
|
11
|
+
[Random.rand, 'some string', { key: 10 }, Object.new, Class.new, [2, 3]]
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#cast' do
|
15
|
+
context 'when no block is given' do
|
16
|
+
subject(:caster) { caster_class.new }
|
17
|
+
|
18
|
+
context 'when no options are given' do
|
19
|
+
it 'returns the value' do
|
20
|
+
expect(caster.cast(value)).to be(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when options are given' do
|
25
|
+
it 'returns the value' do
|
26
|
+
expect(caster.cast(value, key: :value))
|
27
|
+
.to be(value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
9
31
|
|
10
|
-
describe '.cast' do
|
11
32
|
context 'when no options are given and the block accepts none' do
|
12
33
|
let(:method_name) { :to_s }
|
13
34
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Sinclair::EqualsChecker::Reader do
|
6
|
+
subject(:reader) { described_class.new(attribute) }
|
7
|
+
|
8
|
+
let(:attribute) { :information }
|
9
|
+
let(:model) { SampleModel.new(name: name, age: age) }
|
10
|
+
let(:name) { 'The Name' }
|
11
|
+
let(:age) { 25 }
|
12
|
+
|
13
|
+
describe '.attributes_match?' do
|
14
|
+
let(:other) { SampleModel.new(name: other_name) }
|
15
|
+
let(:attribute) { :name }
|
16
|
+
|
17
|
+
context 'when the value match' do
|
18
|
+
let(:other_name) { name }
|
19
|
+
|
20
|
+
it do
|
21
|
+
expect(described_class)
|
22
|
+
.to be_attributes_match(attribute, model, other)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when the value does not match' do
|
27
|
+
let(:other_name) { 'Other Name' }
|
28
|
+
|
29
|
+
it do
|
30
|
+
expect(described_class)
|
31
|
+
.not_to be_attributes_match(attribute, model, other)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#read_from' do
|
37
|
+
context 'when reading from a method' do
|
38
|
+
it 'returns the value from the method' do
|
39
|
+
expect(reader.read_from(model)).to eq('The Name: 25 yo')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when reading from a variable' do
|
44
|
+
let(:attribute) { :@inner_variable }
|
45
|
+
|
46
|
+
before do
|
47
|
+
model.instance_variable_set(:@inner_variable, 301)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns the value from the variable' do
|
51
|
+
expect(reader.read_from(model)).to eq(301)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -119,6 +119,18 @@ describe Sinclair::EqualsChecker do
|
|
119
119
|
end
|
120
120
|
end
|
121
121
|
end
|
122
|
+
|
123
|
+
context 'when one of the attributes is an instance variable' do
|
124
|
+
let(:attributes) { %i[name @age] }
|
125
|
+
|
126
|
+
context 'when the instance variable is different and the method the same' do
|
127
|
+
let(:name2) { name1 }
|
128
|
+
|
129
|
+
it do
|
130
|
+
expect(checker).not_to be_match(model1, model2)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
122
134
|
end
|
123
135
|
|
124
136
|
describe '#add' do
|
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.16.
|
4
|
+
version: 1.16.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarthJee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -288,6 +288,7 @@ files:
|
|
288
288
|
- lib/sinclair/env_settable.rb
|
289
289
|
- lib/sinclair/env_settable/builder.rb
|
290
290
|
- lib/sinclair/equals_checker.rb
|
291
|
+
- lib/sinclair/equals_checker/reader.rb
|
291
292
|
- lib/sinclair/exception.rb
|
292
293
|
- lib/sinclair/input_hash.rb
|
293
294
|
- lib/sinclair/matchers.rb
|
@@ -380,6 +381,7 @@ files:
|
|
380
381
|
- spec/lib/sinclair/core_ext/object_spec.rb
|
381
382
|
- spec/lib/sinclair/env_settable/builder_spec.rb
|
382
383
|
- spec/lib/sinclair/env_settable_spec.rb
|
384
|
+
- spec/lib/sinclair/equals_checker/reader_spec.rb
|
383
385
|
- spec/lib/sinclair/equals_checker_spec.rb
|
384
386
|
- spec/lib/sinclair/exception/invalid_options_spec.rb
|
385
387
|
- spec/lib/sinclair/input_hash_spec.rb
|
@@ -455,13 +457,13 @@ files:
|
|
455
457
|
- spec/support/models/purchase.rb
|
456
458
|
- spec/support/models/random_generator.rb
|
457
459
|
- spec/support/models/ruby_string_caster.rb
|
460
|
+
- spec/support/models/sample_model.rb
|
458
461
|
- spec/support/models/server.rb
|
459
462
|
- spec/support/models/server_config.rb
|
460
463
|
- spec/support/models/service_client.rb
|
461
464
|
- spec/support/models/string_parser.rb
|
462
465
|
- spec/support/models/tv.rb
|
463
466
|
- spec/support/models/validator_builder.rb
|
464
|
-
- spec/support/sample_model.rb
|
465
467
|
- spec/support/shared_examples/attribute_accessor.rb
|
466
468
|
- spec/support/shared_examples/class_method_definition.rb
|
467
469
|
- spec/support/shared_examples/config.rb
|