sinclair 1.14.1 → 1.14.2

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: 4984ad62935fbe5a85e51b846b5124b72153b94cf9433a8a668f824c11e5eeaf
4
- data.tar.gz: 336e7c54378ccd676f59eb92fe2a1bf32de84ff9a8bc2a91c8a6dd1e6494b46d
3
+ metadata.gz: b6bd1d9896437c341e52648bac78c70d453a773c92a2fba06111d88d12be06a3
4
+ data.tar.gz: 5cb2ba761532e3db60ffc8a09b821509c1355949071c5346cef962b0d43dfe9d
5
5
  SHA512:
6
- metadata.gz: f3a84c2a80644b15e31991cb99a314378453e642c29c9864080e96f28bd34234f2476735284291ba56dd48ab9d67883a5b16709a1aa67b31116ef262dd6b1df1
7
- data.tar.gz: d1405b56826158314eb4d304b33c02dc18015e63fe58e716371f5a58c7a8f417652ea1026d5b533c844e7c5a89ffeae7990f70334e61dbf4019006f412359dfe
6
+ metadata.gz: bf2fd682ae8e6c57a8a173a5f78492bf6c7011dfef7734fd9c0c2d3d4d73fa599bac72a9317920dd0ebf5bb6c98f0f2f511c0fcc9fe5b683f766b5624359ccf3
7
+ data.tar.gz: db83a68fd9b11d0a227afca422f5a6e8e33bc64e3624a55c0d8f3b87d49235d8e17b1393c44b6ac2a67c8b9869e58d5780a3a06aad4f76429a4c4b0c48247f43
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.14.1](https://github.com/darthjee/sinclair/tree/1.14.1)
18
+ Current Release: [1.14.2](https://github.com/darthjee/sinclair/tree/1.14.2)
19
19
 
20
- [Next release](https://github.com/darthjee/sinclair/compare/1.14.1...master)
20
+ [Next release](https://github.com/darthjee/sinclair/compare/1.14.2...master)
21
21
 
22
22
  Yard Documentation
23
23
  -------------------
24
- [https://www.rubydoc.info/gems/sinclair/1.14.1](https://www.rubydoc.info/gems/sinclair/1.14.1)
24
+ [https://www.rubydoc.info/gems/sinclair/1.14.2](https://www.rubydoc.info/gems/sinclair/1.14.2)
25
25
 
26
26
  Installation
27
27
  ---------------
@@ -79,20 +79,20 @@ puts "One Hundred => #{Clazz.one_hundred_twenty}" # One Hundred Twenty => 120
79
79
  <summary>Builder in class method</summary>
80
80
 
81
81
  ```ruby
82
+ # http_json_model.rb
83
+
82
84
  class HttpJsonModel
83
85
  attr_reader :json
84
86
 
85
87
  class << self
86
88
  def parse(attribute, path: [])
87
- builder = Sinclair.new(self)
88
-
89
89
  keys = (path + [attribute]).map(&:to_s)
90
90
 
91
- builder.add_method(attribute) do
92
- keys.inject(hash) { |h, key| h[key] }
91
+ Sinclair.build(self) do
92
+ add_method(attribute) do
93
+ keys.inject(hash) { |h, key| h[key] }
94
+ end
93
95
  end
94
-
95
- builder.build
96
96
  end
97
97
  end
98
98
 
@@ -104,6 +104,10 @@ class HttpJsonModel
104
104
  @hash ||= JSON.parse(json)
105
105
  end
106
106
  end
107
+ ```
108
+
109
+ ```ruby
110
+ # http_person.rb
107
111
 
108
112
  class HttpPerson < HttpJsonModel
109
113
  parse :uid
@@ -112,7 +116,9 @@ class HttpPerson < HttpJsonModel
112
116
  parse :username, path: [:digital_information]
113
117
  parse :email, path: [:digital_information]
114
118
  end
119
+ ```
115
120
 
121
+ ```ruby
116
122
  json = <<-JSON
117
123
  {
118
124
  "uid": "12sof511",
@@ -675,6 +681,15 @@ You can use the provided matcher to check that your builder is adding a method c
675
681
  <summary>Sample of specs over adding methods</summary>
676
682
 
677
683
  ```ruby
684
+ # spec_helper.rb
685
+
686
+ RSpec.configure do |config|
687
+ config.include Sinclair::Matchers
688
+ end
689
+ ```
690
+
691
+ ```ruby
692
+ # default_value.rb
678
693
  class DefaultValue
679
694
  delegate :build, to: :builder
680
695
  attr_reader :klass, :method, :value, :class_method
@@ -698,8 +713,12 @@ class DefaultValue
698
713
  end
699
714
  end
700
715
  end
716
+ ```
717
+
718
+ ```ruby
719
+ # default_value_spec.rb
701
720
 
702
- RSpec.describe Sinclair::Matchers do
721
+ RSpec.describe DefaultValue do
703
722
  subject(:builder_class) { DefaultValue }
704
723
 
705
724
  let(:klass) { Class.new }
data/config/yardstick.yml CHANGED
@@ -1,4 +1,4 @@
1
- threshold: 100
1
+ threshold: 99.8
2
2
  require_exact_threshold: false
3
3
  rules:
4
4
  ApiTag::Presence:
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Sinclair
4
+ # @author darthjee
5
+ # @api public
6
+ #
7
+ # Class methods for {Sinclair}
8
+ module ClassMethods
9
+ # Runs build using a block for adding the methods
10
+ #
11
+ # The block is executed adding the methods and after the builder
12
+ # runs build building all the methods
13
+ #
14
+ # @param (see Sinclair#initialize)
15
+ # @param block [Proc] block to be executed by the builder
16
+ # in order to add the methods before running build
17
+ #
18
+ # @yield an instance of a builder ({Sinclair})
19
+ #
20
+ # @return (see Sinclair#build)
21
+ #
22
+ # @example Simple usage
23
+ # class MyPerson
24
+ # end
25
+ #
26
+ # Sinclair.build(model_class) do
27
+ # add_method(:random_name, cached: true) do
28
+ # "John #{Random.rand(1000)} Doe"
29
+ # end
30
+ # end
31
+ #
32
+ # model = MyPerson.new
33
+ #
34
+ # model.random_name # returns 'John 803 Doe'
35
+ def build(klass, options = {}, &block)
36
+ new(klass, options).tap do |builder|
37
+ builder.instance_eval(&block)
38
+ end.build
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sinclair
4
- VERSION = '1.14.1'
4
+ VERSION = '1.14.2'
5
5
  end
data/lib/sinclair.rb CHANGED
@@ -83,6 +83,7 @@ class Sinclair
83
83
  require 'sinclair/options_parser'
84
84
 
85
85
  autoload :VERSION, 'sinclair/version'
86
+ autoload :ClassMethods, 'sinclair/class_methods'
86
87
  autoload :Config, 'sinclair/config'
87
88
  autoload :ConfigBuilder, 'sinclair/config_builder'
88
89
  autoload :ConfigClass, 'sinclair/config_class'
@@ -100,6 +101,21 @@ class Sinclair
100
101
  autoload :Options, 'sinclair/options'
101
102
 
102
103
  include OptionsParser
104
+ extend ClassMethods
105
+
106
+ # @method self.build(klass, options = {}, &block)
107
+ # Runs build using a block for adding the methods
108
+ #
109
+ # The block is executed adding the methods and after the builder
110
+ # runs build building all the methods
111
+ #
112
+ # @see Sinclair::ClassMethods#build
113
+ #
114
+ # @param (see Sinclair::ClassMethods#build)
115
+ # @return (see Sinclair::ClassMethods#build)
116
+ # @yield (see Sinclair::ClassMethods#build)
117
+ #
118
+ # @example (see Sinclair::ClassMethods#build)
103
119
 
104
120
  # Returns a new instance of Sinclair
105
121
  #
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::ClassMethods do
6
+ describe 'yard for .build' do
7
+ before { allow(Random).to receive(:rand).and_return(803) }
8
+
9
+ it 'Simple usage' do
10
+ model_class = Class.new
11
+
12
+ Sinclair.build(model_class) do
13
+ add_method(:random_name, cached: true) do
14
+ "John #{Random.rand(1000)} Doe"
15
+ end
16
+ end
17
+
18
+ model = model_class.new
19
+
20
+ expect(model.random_name).to eq('John 803 Doe')
21
+ end
22
+ end
23
+ end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Sinclair do
6
- describe 'yarn' do
6
+ describe 'yard' do
7
7
  let(:klass) { Class.new(MyModel) }
8
8
  let(:instance) { klass.new }
9
9
  let(:builder) { described_class.new(klass) }
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sinclair::ClassMethods do
6
+ subject(:builder) { builder_class.new(dummy_class, options) }
7
+
8
+ let(:options) { {} }
9
+ let(:instance) { dummy_class.new }
10
+ let(:dummy_class) { Class.new }
11
+ let(:builder_class) { Sinclair }
12
+
13
+ describe '#build' do
14
+ let(:block) do
15
+ method_name = :some_method
16
+ value = 1
17
+
18
+ proc do
19
+ add_method(method_name) { value }
20
+ end
21
+ end
22
+
23
+ it 'executes the block and builds' do
24
+ expect { builder_class.build(dummy_class, options, &block) }
25
+ .to add_method(:some_method).to(dummy_class)
26
+ end
27
+
28
+ context 'when the method is built and called' do
29
+ before do
30
+ builder_class.build(dummy_class, options, &block)
31
+ end
32
+
33
+ it 'returns the value' do
34
+ expect(instance.some_method).to eq(1)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -5,15 +5,13 @@ class HttpJsonModel
5
5
 
6
6
  class << self
7
7
  def parse(attribute, path: [])
8
- builder = Sinclair.new(self)
9
-
10
8
  keys = (path + [attribute]).map(&:to_s)
11
9
 
12
- builder.add_method(attribute) do
13
- keys.inject(hash) { |h, key| h[key] }
10
+ Sinclair.build(self) do
11
+ add_method(attribute) do
12
+ keys.inject(hash) { |h, key| h[key] }
13
+ end
14
14
  end
15
-
16
- builder.build
17
15
  end
18
16
  end
19
17
 
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.14.1
4
+ version: 1.14.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-04-25 00:00:00.000000000 Z
11
+ date: 2023-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -273,6 +273,7 @@ files:
273
273
  - config/yardstick.yml
274
274
  - docker-compose.yml
275
275
  - lib/sinclair.rb
276
+ - lib/sinclair/class_methods.rb
276
277
  - lib/sinclair/comparable.rb
277
278
  - lib/sinclair/comparable/class_methods.rb
278
279
  - lib/sinclair/config.rb
@@ -338,6 +339,7 @@ files:
338
339
  - spec/integration/yard/my_builder_spec.rb
339
340
  - spec/integration/yard/sinclair/add_class_method_spec.rb
340
341
  - spec/integration/yard/sinclair/add_method_spec.rb
342
+ - spec/integration/yard/sinclair/class_methods/build_spec.rb
341
343
  - spec/integration/yard/sinclair/comparable_spec.rb
342
344
  - spec/integration/yard/sinclair/config_builder_spec.rb
343
345
  - spec/integration/yard/sinclair/config_class_spec.rb
@@ -359,6 +361,7 @@ files:
359
361
  - spec/integration/yard/sinclair/options_parser_spec.rb
360
362
  - spec/integration/yard/sinclair/options_spec.rb
361
363
  - spec/integration/yard/sinclair_spec.rb
364
+ - spec/lib/sinclair/class_methods_spec.rb
362
365
  - spec/lib/sinclair/comparable_spec.rb
363
366
  - spec/lib/sinclair/config/methods_builder_spec.rb
364
367
  - spec/lib/sinclair/config_builder_spec.rb