sinclair 1.15.0 → 1.16.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd10c98e0ec9983e0bd34a8b438126cc391ae667c9e7d5b7ca62f473c5296aea
4
- data.tar.gz: f1bffcf49524f9dc05ee27e7510c4e0a76be23b674642fcfa2b10cfdb9f43a82
3
+ metadata.gz: '0633819d57447ccb757e81a154d58e8b5c47ed5fb49a6b738b6e3b27801ceeb0'
4
+ data.tar.gz: 0b50ae42aa0c487d3ae12d5e2b0c0933b4cff0040d7635340d82a5122a038c47
5
5
  SHA512:
6
- metadata.gz: f3afe247b25b8b10f692227c3d5c031e4d6fd04f8aa00e021d0aa8d5813d9cba75c05b4a7d8aae6cc5b6be96d7de404becf35e9391db92c3ceadfc2804f72ce5
7
- data.tar.gz: c80cee176cd24ecce3410fa6a64789ca3f52789bce24d558b421751fb6c74a4bf5c86eb1246e48d09abbad962e3bc6f965f1dcf09beae9eca68a198d4e969771
6
+ metadata.gz: a72bfc2c48a33ef60fa56a4a7dd8009a12737752ffa3075ef58a50f43968779155f95b095afc7b01d2f9172b2f6d88a2cdf1f2b1e010036686cdb4be7ae376fb
7
+ data.tar.gz: 3e0e0a9a8bc66d1d13c427895bd9473d1a984b30bd66a8fbc961d2f80d9f54910381a2ec6d3f1ab256590a9c6aaf559b71bba921f8c1bd2466501c02e835ced0
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.15.0](https://github.com/darthjee/sinclair/tree/1.15.0)
18
+ Current Release: [1.16.1](https://github.com/darthjee/sinclair/tree/1.16.1)
19
19
 
20
- [Next release](https://github.com/darthjee/sinclair/compare/1.15.0...master)
20
+ [Next release](https://github.com/darthjee/sinclair/compare/1.16.1...master)
21
21
 
22
22
  Yard Documentation
23
23
  -------------------
24
- [https://www.rubydoc.info/gems/sinclair/1.15.0](https://www.rubydoc.info/gems/sinclair/1.15.0)
24
+ [https://www.rubydoc.info/gems/sinclair/1.16.1](https://www.rubydoc.info/gems/sinclair/1.16.1)
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
@@ -62,6 +62,8 @@ class Sinclair
62
62
  #
63
63
  # @return [Caster]
64
64
  def caster_for_class(klass)
65
+ return unless klass.is_a?(Class) || klass.is_a?(Module)
66
+
65
67
  class_casters.find do |klazz, _|
66
68
  klass <= klazz
67
69
  end&.second
@@ -24,7 +24,11 @@ class Sinclair
24
24
  # The master caster never checks with its an
25
25
  #
26
26
  # @example
27
- # class MyCaster < Sinclair::Caster
27
+ # class BaseCaster < Sinclair::Caster
28
+ # cast_with(:string, :to_s)
29
+ # end
30
+ #
31
+ # class MyCaster < BaseCaster
28
32
  # end
29
33
  #
30
34
  # MyCaster.cast(10, :string) # returns '10'
@@ -373,9 +377,5 @@ class Sinclair
373
377
  #
374
378
  # @return [Proc]
375
379
  attr_reader :block
376
-
377
- cast_with(:string, :to_s)
378
- cast_with(:integer, :to_i)
379
- cast_with(:float, :to_f)
380
380
  end
381
381
  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,7 +93,7 @@ class Sinclair
91
93
  return false unless model.class == other.class
92
94
 
93
95
  attributes.all? do |attr|
94
- model.send(attr) == other.send(attr)
96
+ Reader.attributes_match?(attr, model, other)
95
97
  end
96
98
  end
97
99
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sinclair
4
- VERSION = '1.15.0'
4
+ VERSION = '1.16.1'
5
5
  end
@@ -3,7 +3,13 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe 'yard Sinclair::Caster::ClassMethods' do
6
- let(:my_caster) { Class.new(Sinclair::Caster) }
6
+ let(:my_caster) { Class.new(superclass) }
7
+
8
+ let(:superclass) do
9
+ Class.new(Sinclair::Caster) do
10
+ cast_with(:string, :to_s)
11
+ end
12
+ end
7
13
 
8
14
  describe '.master_caster!' do
9
15
  it 'Making a class to be a master caster' do
@@ -3,7 +3,16 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Sinclair::Caster::ClassMethods do
6
- subject(:caster) { Class.new(Sinclair::Caster) }
6
+ subject(:caster) { Class.new(superclass) }
7
+
8
+ let(:superclass) do
9
+ Class.new(Sinclair::Caster) do
10
+ cast_with(:string, :to_s)
11
+ cast_with(:integer, :to_i)
12
+ cast_with(:float, :to_f)
13
+ cast_with(String, :to_s)
14
+ end
15
+ end
7
16
 
8
17
  describe '.cast_with' do
9
18
  let(:value) { instance_double('value', to_p: final_value) }
@@ -120,6 +129,19 @@ describe Sinclair::Caster::ClassMethods do
120
129
  end
121
130
 
122
131
  describe '.caster_for' do
132
+ context 'when nil key is given' do
133
+ let(:value) { values.sample }
134
+ let(:values) do
135
+ [Random.rand, 'some string', { key: 10 }, Object.new, Class.new, [2, 3]]
136
+ end
137
+
138
+ it { expect(caster.caster_for(nil)).to be_a(Sinclair::Caster) }
139
+
140
+ it 'returns the default caster' do
141
+ expect(caster.caster_for(nil).cast(value)).to eq(value)
142
+ end
143
+ end
144
+
123
145
  context 'when the key has been defined with a symbol key' do
124
146
  before { caster.cast_with(:problem, :to_p) }
125
147
 
@@ -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
@@ -6,6 +6,10 @@ class SampleModel
6
6
  @age = age
7
7
  end
8
8
 
9
+ def information
10
+ "#{name}: #{age} yo"
11
+ end
12
+
9
13
  protected
10
14
 
11
15
  attr_reader :name
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.15.0
4
+ version: 1.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-29 00:00:00.000000000 Z
11
+ date: 2023-05-02 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