shale-builder 0.1.1 → 0.1.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: 6d245e494eafdbe7a026e2f9201840f844b2e351dfdf3168632bfd825d0d1bbb
4
- data.tar.gz: db76f630562faabca941d8522fbe1deb3cd59abcb0664e7843e295e3b22435bd
3
+ metadata.gz: 5173a85d5d039ce2e3207ce624bd40d15eea0910f4ee07d24bafa0a73306e6ae
4
+ data.tar.gz: 9a703d6acb6d821a57254bca2af2716ead533d650a74d3e6b87e09a18fb380b2
5
5
  SHA512:
6
- metadata.gz: 67fb45e984b9c75e111f3a13aea88e5f6cb225de1d62a5e3e5bdc6c414b15cbc601d294b40e59a127b8861c2f8031ddf3da3377597acecf0dc33a2789b48d20c
7
- data.tar.gz: d02c3045bd8cdba9d08ec65fc2a7014da13ece5c8c5803e2970d97fcf1c53415c3d91ac4258556232aaad56eb5d87512f2a2f530168467fa67d7598da9070ef8
6
+ metadata.gz: db1d70904eb858eb841b10373ae2c14889552eeda5764785d70c8e430955e659c259ca91499a772a885653201526f2e229b11dcd4ce0d4449217d7c77b83b1e1
7
+ data.tar.gz: e5e744a3a88ad1feb0a0f8606392403d8b2c15a06d9d5e133bea8d8c8e8c4eacc4edf204f4f2677044ff12980340ccadc9d8f94949f51819fab7e29f2ea5eb86
data/.rubocop.yml CHANGED
@@ -2,4 +2,4 @@ inherit_gem:
2
2
  rubocop-espago: rubocop.yml
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 2.7
5
+ TargetRubyVersion: 3.0
data/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
- ## [Unreleased]
1
+ ## [0.1.2] - 2023-10-11
2
+
3
+ - Add support for collections
4
+ - Drop support for Ruby 2.7
2
5
 
3
6
  ## [0.1.1] - 2023-02-22
4
7
 
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in diggable.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'byebug', '~> 11.1' # debugger
8
9
  gem 'minitest', '~> 5.0' # test framework
9
10
  gem 'rake', '~> 13.0' # automation tasks
10
11
  gem 'rubocop-espago', '~> 1.0' # ruby linter
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shale-builder (0.1.1)
4
+ shale-builder (0.1.2)
5
5
  shale (< 1.0)
6
6
 
7
7
  GEM
@@ -10,6 +10,7 @@ GEM
10
10
  ast (2.4.2)
11
11
  backport (1.2.0)
12
12
  benchmark (0.2.1)
13
+ byebug (11.1.3)
13
14
  diff-lcs (1.5.0)
14
15
  e2mmap (0.1.0)
15
16
  jaro_winkler (1.5.4)
@@ -77,6 +78,7 @@ PLATFORMS
77
78
  x86_64-linux
78
79
 
79
80
  DEPENDENCIES
81
+ byebug (~> 11.1)
80
82
  minitest (~> 5.0)
81
83
  rake (~> 13.0)
82
84
  rubocop-espago (~> 1.0)
data/README.md CHANGED
@@ -90,7 +90,7 @@ class PaymentInstrument < Shale::Mapper
90
90
  attribute :expiration_month, ::Shale::Type::Integer
91
91
  end
92
92
 
93
- class Transaction < ::Shale::Mapper
93
+ class Transaction < Shale::Mapper
94
94
  include Shale::Builder
95
95
 
96
96
  attribute :cvv_code, Shale::Type::String
@@ -142,6 +142,52 @@ non-primitive types have been overridden to accept blocks.
142
142
  When a block is given to such a getter, it instantiates an empty object
143
143
  of its type and yields it to the block.
144
144
 
145
+ ### Collections
146
+
147
+ Whenever you call a getter with a block for a collection attribute, the built object will be appended to the array.
148
+
149
+ Let's define a schema like this.
150
+
151
+ ```rb
152
+ class Client < Shale::Mapper
153
+ include Shale::Builder
154
+
155
+ attribute :first_name, Shale::Type::String
156
+ attribute :last_name, Shale::Type::String
157
+ attribute :email, Shale::Type::String
158
+ end
159
+
160
+ class Transaction < Shale::Mapper
161
+ include Shale::Builder
162
+
163
+ attribute :clients, Client, collection: true
164
+ end
165
+ ```
166
+
167
+ You can easily build add new clients to the collection like so:
168
+
169
+ ```rb
170
+ transaction = Transaction.build do |t|
171
+ # this will be added as the first element of the collection
172
+ t.clients do |c|
173
+ c.first_name = 'Foo'
174
+ c.last_name = 'Bar'
175
+ end
176
+
177
+ # this will be added as the second element of the collection
178
+ t.clients do |c|
179
+ c.first_name = 'Grant'
180
+ c.last_name = 'Taylor'
181
+ end
182
+ end
183
+
184
+ p transaction.clients
185
+ # [
186
+ # #<Client:0x00000001066c2828 @first_name="Foo", @last_name="Bar", @email=nil>,
187
+ # #<Client:0x00000001066c24b8 @first_name="Grant", @last_name="Taylor", @email=nil>
188
+ # ]
189
+ ```
190
+
145
191
  ### Conditional building
146
192
 
147
193
  This DSL makes it extremely easy to build nested
@@ -3,6 +3,6 @@
3
3
  module Shale
4
4
  module Builder
5
5
  # @return [String]
6
- VERSION = '0.1.1'
6
+ VERSION = '0.1.2'
7
7
  end
8
8
  end
data/lib/shale/builder.rb CHANGED
@@ -89,10 +89,25 @@ module Shale
89
89
  # @param name [String, Symbol]
90
90
  # @param type [Class]
91
91
  # @return [void]
92
- def attribute(name, type, *args, **kwargs, &block)
92
+ def attribute(name, type, *args, collection: false, **kwargs, &block)
93
93
  super
94
94
  return unless type < ::Shale::Mapper
95
95
 
96
+ if collection
97
+ @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
98
+ def #{name} # def clients
99
+ return super unless block_given? # return super unless block_given?
100
+ #
101
+ arr = self.#{name} ||= [] # arr = self.clients ||= []
102
+ object = #{type}.new # object = Client.new
103
+ yield(object) # yield(object)
104
+ arr << object # arr << object
105
+ object # object
106
+ end # end
107
+ RUBY
108
+ return
109
+ end
110
+
96
111
  @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
97
112
  def #{name} # def amount
98
113
  return super unless block_given? # return super unless block_given?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shale-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-22 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shale
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 2.7.0
62
+ version: 3.0.0
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - ">="