shale-builder 0.1.1 → 0.1.3
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/.rubocop.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +8 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +5 -2
- data/README.md +47 -1
- data/lib/shale/builder/version.rb +1 -1
- data/lib/shale/builder.rb +16 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b0eb2f219fcd64c0f89f347f70e892e96f7b18490450a42557a39525e14fabe
|
4
|
+
data.tar.gz: c1f7b8a45efd094bc4f43028e56eae5d0da322357bbf0d6c1e882021a8da92e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c02cb9e08061c9d978f2e64be2ede31a3a874cc4b5f9e420f00bfe39771e69d48930fd6c063d7a8d6d066a8a3bfcd3faf7c2e58983d081d1ee852c8a2d59c377
|
7
|
+
data.tar.gz: 69bd7e9e44ae49c0f9bc2a7c19b5b7472d28a56c841b215b8a4be2bbd49dc42f99b3b43bd2d782a87b86a93d1534957af4356184a9e50926ee423b8277ef4bac
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.2.
|
1
|
+
3.2.2
|
data/CHANGELOG.md
CHANGED
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,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
shale-builder (0.1.
|
5
|
-
shale (<
|
4
|
+
shale-builder (0.1.3)
|
5
|
+
shale (< 2.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -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)
|
@@ -74,9 +75,11 @@ GEM
|
|
74
75
|
|
75
76
|
PLATFORMS
|
76
77
|
arm64-darwin-20
|
78
|
+
arm64-darwin-22
|
77
79
|
x86_64-linux
|
78
80
|
|
79
81
|
DEPENDENCIES
|
82
|
+
byebug (~> 11.1)
|
80
83
|
minitest (~> 5.0)
|
81
84
|
rake (~> 13.0)
|
82
85
|
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 <
|
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
|
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.
|
4
|
+
version: 0.1.3
|
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-
|
11
|
+
date: 2023-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shale
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "<"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
description: An addon to the shale Ruby gem which adds a simple yet powerful builder
|
28
28
|
DSL.
|
29
29
|
email:
|
@@ -59,14 +59,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
59
|
requirements:
|
60
60
|
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
62
|
+
version: 3.0.0
|
63
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - ">="
|
66
66
|
- !ruby/object:Gem::Version
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
|
-
rubygems_version: 3.4.
|
69
|
+
rubygems_version: 3.4.21
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: An addon to the shale Ruby gem which adds a simple yet powerful builder DSL.
|