shale-builder 0.9.3 → 0.11.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd7c9d8f63daeb33c5e8b6dc06b50adb16f874b07af739379d8e5002f44d5333
4
- data.tar.gz: 2717e64843cb2d186fbe0fa386603db74731bfc2f1accdbc435a0622020ada23
3
+ metadata.gz: a02fee32262830d9097598807f3970bdcdda038a8999117d67b4a8c62a7f983a
4
+ data.tar.gz: 626247ddb399131860b0247ccea68c62d50184dec19262ca0b68dda2b27d3ba3
5
5
  SHA512:
6
- metadata.gz: 6d178bd49564f2c17c299d9229d47cf83b9ec00d22be4c5b13941db48466a3cbe348dca7a38116a44faf9f1e3d75e2d7a23c14d42368b230c0710565c75598d7
7
- data.tar.gz: a6733f21ff64dcfc9d8d24a4608e96ace323ae277ba5b70245d335200e87d68f06b7a627c8f7a93470da94df26785850cf469aa38faedf7f96fd4f321ce86ddb
6
+ metadata.gz: ba0443c6efe508d3484800075f21ea7384522e29f1965400675443f6297f9fdf97253440158486046a9069e60c97800eac1e60fe284af64844da9b2fcd3ce021
7
+ data.tar.gz: 388709416524377082439f4da712f829722dfeb672545c297bcfb88e80444a95c8cae5c1f77f1d78e2ab3f2fb505b311526e8ecdcf3d4229f8a5513d5e2489e2
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ inherit_gem:
2
2
  rubocop-espago: sorbet.yml
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 3.1
5
+ TargetRubyVersion: 3.2
6
6
  Exclude:
7
7
  - lib/tapioca/**/*.rb
8
8
 
@@ -32,3 +32,9 @@ Sorbet/ForbidTHelpers:
32
32
 
33
33
  Style/DocumentDynamicEvalDefinition:
34
34
  Enabled: false
35
+
36
+ Style/HashLookupMethod:
37
+ Enabled: false
38
+
39
+ Style/OneClassPerFile:
40
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.4.5
1
+ ruby-4.0.9
data/CHANGELOG.md CHANGED
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.11.0] - 2026-11-12
9
+
10
+ [Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.10.0...v0.11.0)
11
+
12
+ ### Changes
13
+ - Add support the `def self.builder` method to specify custom builder objects
14
+
15
+ ## [0.10.0] - 2026-04-28
16
+
17
+ [Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.9.3...v0.10.0)
18
+
19
+ ### Changes
20
+ - Update ruby, sorbet and tapioca
21
+
8
22
  ## [0.9.2] - 2026-04-24
9
23
 
10
24
  [Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.8.5...v0.9.2)
data/Gemfile CHANGED
@@ -5,16 +5,17 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in diggable.gemspec
6
6
  gemspec
7
7
 
8
- gem 'activemodel', '~> 8.0' # validations
8
+ gem 'activemodel', '~> 8.1' # validations
9
9
  gem 'base64', '~> 0.3' # Base64
10
- gem 'byebug', '~> 11.1' # debugger
11
- gem 'minitest', '~> 5.0' # test framework
10
+ gem 'byebug', '~> 13.0' # debugger
11
+ gem 'irb', '~> 1.19.0' # REPL
12
+ gem 'minitest', '~> 6.0' # test framework
12
13
  gem 'mutex_m', '~> 0.3' # Mutexes
13
14
  gem 'racc', '~> 1.8' # parser
14
- gem 'rake', '~> 13.0' # automation tasks
15
- gem 'rubocop', '~> 1.81' # ruby linter
15
+ gem 'rake', '~> 13.4' # automation tasks
16
+ gem 'rubocop', '~> 1.86' # ruby linter
16
17
  gem 'rubocop-espago', '~> 1.2' # Espago rubocop config
17
- gem 'rubocop-sorbet', '~> 0.11' # sorbet rubocop config
18
+ gem 'rubocop-sorbet', '~> 0.12' # sorbet rubocop config
18
19
  gem 'shoulda-context', '~> 2.0' # more pleasant test syntax
19
- gem 'sorbet', '>= 0.5' # static typechecker
20
- gem 'tapioca', '> 0.13' # RBI generator for sorbet
20
+ gem 'sorbet', '>= 0.6' # static typechecker
21
+ gem 'tapioca', '> 0.19' # RBI generator for sorbet
data/Gemfile.lock CHANGED
@@ -1,24 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shale-builder (0.9.3)
4
+ shale-builder (0.11.0)
5
5
  booleans (>= 0.1)
6
6
  shale (< 2.0)
7
- sorbet-runtime (> 0.5)
7
+ sorbet-runtime (> 0.6)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (8.0.3)
13
- activesupport (= 8.0.3)
14
- activesupport (8.0.3)
12
+ activemodel (8.1.3)
13
+ activesupport (= 8.1.3)
14
+ activesupport (8.1.3)
15
15
  base64
16
- benchmark (>= 0.3)
17
16
  bigdecimal
18
17
  concurrent-ruby (~> 1.0, >= 1.3.1)
19
18
  connection_pool (>= 2.2.5)
20
19
  drb
21
20
  i18n (>= 1.6, < 2)
21
+ json
22
22
  logger (>= 1.4.2)
23
23
  minitest (>= 5.1)
24
24
  securerandom (>= 0.3)
@@ -26,118 +26,148 @@ GEM
26
26
  uri (>= 0.13.1)
27
27
  ast (2.4.3)
28
28
  base64 (0.3.0)
29
- benchmark (0.4.1)
30
- bigdecimal (3.2.3)
31
- booleans (0.1.1)
32
- byebug (11.1.3)
33
- concurrent-ruby (1.3.5)
34
- connection_pool (2.5.4)
29
+ benchmark (0.5.0)
30
+ bigdecimal (4.1.2)
31
+ booleans (0.1.3)
32
+ sorbet-runtime (>= 0.5)
33
+ byebug (13.0.0)
34
+ reline (>= 0.6.0)
35
+ concurrent-ruby (1.3.6)
36
+ connection_pool (3.0.2)
35
37
  drb (2.2.3)
38
+ erb (6.0.7)
36
39
  erubi (1.13.1)
37
- i18n (1.14.7)
40
+ i18n (1.14.8)
38
41
  concurrent-ruby (~> 1.0)
39
- json (2.15.1)
42
+ io-console (0.8.2)
43
+ irb (1.19.0)
44
+ pp (>= 0.6.0)
45
+ prism (>= 1.3.0)
46
+ rdoc (>= 4.0.0)
47
+ reline (>= 0.4.2)
48
+ json (2.19.4)
40
49
  language_server-protocol (3.17.0.5)
41
50
  lint_roller (1.1.0)
42
51
  logger (1.7.0)
43
- minitest (5.17.0)
52
+ minitest (6.0.5)
53
+ drb (~> 2.0)
54
+ prism (~> 1.5)
44
55
  mutex_m (0.3.0)
45
56
  netrc (0.11.0)
46
- parallel (1.27.0)
47
- parser (3.3.9.0)
57
+ parallel (2.1.0)
58
+ parser (3.3.11.1)
48
59
  ast (~> 2.4.1)
49
60
  racc
50
- prism (1.5.2)
61
+ pp (0.6.4)
62
+ prettyprint
63
+ prettyprint (0.2.1)
64
+ prism (1.9.0)
51
65
  racc (1.8.1)
52
66
  rainbow (3.1.1)
53
- rake (13.0.6)
54
- rbi (0.3.7)
67
+ rake (13.4.2)
68
+ rbi (0.3.10)
55
69
  prism (~> 1.0)
56
- rbs (>= 3.4.4)
57
- rbs (3.9.5)
70
+ rbs (>= 4.0.1)
71
+ rbs (4.0.2)
58
72
  logger
59
- regexp_parser (2.11.3)
60
- rubocop (1.81.1)
73
+ prism (>= 1.6.0)
74
+ tsort
75
+ rdoc (8.1.0)
76
+ erb
77
+ prism (>= 1.6.0)
78
+ rbs (>= 4.0.0)
79
+ tsort
80
+ regexp_parser (2.12.0)
81
+ reline (0.6.3)
82
+ io-console (~> 0.5)
83
+ require-hooks (0.3.0)
84
+ rexml (3.4.4)
85
+ rubocop (1.86.1)
61
86
  json (~> 2.3)
62
87
  language_server-protocol (~> 3.17.0.2)
63
88
  lint_roller (~> 1.1.0)
64
- parallel (~> 1.10)
89
+ parallel (>= 1.10)
65
90
  parser (>= 3.3.0.2)
66
91
  rainbow (>= 2.2.2, < 4.0)
67
92
  regexp_parser (>= 2.9.3, < 3.0)
68
- rubocop-ast (>= 1.47.1, < 2.0)
93
+ rubocop-ast (>= 1.49.0, < 2.0)
69
94
  ruby-progressbar (~> 1.7)
70
95
  unicode-display_width (>= 2.4.0, < 4.0)
71
- rubocop-ast (1.47.1)
96
+ rubocop-ast (1.49.1)
72
97
  parser (>= 3.3.7.2)
73
- prism (~> 1.4)
98
+ prism (~> 1.7)
74
99
  rubocop-espago (1.2.0)
75
100
  rubocop
76
- rubocop-sorbet (0.11.0)
101
+ rubocop-sorbet (0.12.0)
77
102
  lint_roller
78
103
  rubocop (>= 1.75.2)
79
104
  ruby-progressbar (1.13.0)
105
+ rubydex (0.1.0.beta13-arm64-darwin)
106
+ rubydex (0.1.0.beta13-x86_64-linux)
80
107
  securerandom (0.4.1)
81
108
  shale (1.2.2)
82
109
  bigdecimal
83
110
  shoulda-context (2.0.0)
84
- sorbet (0.6.12642)
85
- sorbet-static (= 0.6.12642)
86
- sorbet-runtime (0.6.12642)
87
- sorbet-static (0.6.12642-universal-darwin)
88
- sorbet-static (0.6.12642-x86_64-linux)
89
- sorbet-static-and-runtime (0.6.12642)
90
- sorbet (= 0.6.12642)
91
- sorbet-runtime (= 0.6.12642)
92
- spoom (1.6.1)
111
+ sorbet (0.6.13169)
112
+ sorbet-static (= 0.6.13169)
113
+ sorbet-runtime (0.6.13169)
114
+ sorbet-static (0.6.13169-universal-darwin)
115
+ sorbet-static (0.6.13169-x86_64-linux)
116
+ sorbet-static-and-runtime (0.6.13169)
117
+ sorbet (= 0.6.13169)
118
+ sorbet-runtime (= 0.6.13169)
119
+ spoom (1.7.13)
93
120
  erubi (>= 1.10.0)
94
121
  prism (>= 0.28.0)
95
- rbi (>= 0.2.3)
122
+ rbi (>= 0.3.3)
123
+ rbs (>= 4.0.0.dev.5)
124
+ rexml (>= 3.2.6)
96
125
  sorbet-static-and-runtime (>= 0.5.10187)
97
126
  thor (>= 0.19.2)
98
- tapioca (0.16.11)
127
+ tapioca (0.19.1)
99
128
  benchmark
100
129
  bundler (>= 2.2.25)
101
130
  netrc (>= 0.11.0)
102
131
  parallel (>= 1.21.0)
103
- rbi (~> 0.2)
104
- sorbet-static-and-runtime (>= 0.5.11087)
105
- spoom (>= 1.2.0)
132
+ rbi (>= 0.3.7)
133
+ require-hooks (>= 0.2.2)
134
+ rubydex (>= 0.1.0.beta10)
135
+ sorbet-static-and-runtime (>= 0.6.12698)
136
+ spoom (>= 1.7.9)
106
137
  thor (>= 1.2.0)
107
- yard-sorbet
108
- thor (1.4.0)
138
+ tsort
139
+ thor (1.5.0)
140
+ tsort (0.2.0)
109
141
  tzinfo (2.0.6)
110
142
  concurrent-ruby (~> 1.0)
111
143
  unicode-display_width (3.2.0)
112
144
  unicode-emoji (~> 4.1)
113
- unicode-emoji (4.1.0)
114
- uri (1.0.4)
115
- yard (0.9.37)
116
- yard-sorbet (0.9.0)
117
- sorbet-runtime
118
- yard
145
+ unicode-emoji (4.2.0)
146
+ uri (1.1.1)
119
147
 
120
148
  PLATFORMS
121
149
  arm64-darwin-20
122
150
  arm64-darwin-22
123
151
  arm64-darwin-24
152
+ arm64-darwin-25
124
153
  x86_64-linux
125
154
 
126
155
  DEPENDENCIES
127
- activemodel (~> 8.0)
156
+ activemodel (~> 8.1)
128
157
  base64 (~> 0.3)
129
- byebug (~> 11.1)
130
- minitest (~> 5.0)
158
+ byebug (~> 13.0)
159
+ irb (~> 1.19.0)
160
+ minitest (~> 6.0)
131
161
  mutex_m (~> 0.3)
132
162
  racc (~> 1.8)
133
- rake (~> 13.0)
134
- rubocop (~> 1.81)
163
+ rake (~> 13.4)
164
+ rubocop (~> 1.86)
135
165
  rubocop-espago (~> 1.2)
136
- rubocop-sorbet (~> 0.11)
166
+ rubocop-sorbet (~> 0.12)
137
167
  shale-builder!
138
168
  shoulda-context (~> 2.0)
139
- sorbet (>= 0.5)
140
- tapioca (> 0.13)
169
+ sorbet (>= 0.6)
170
+ tapioca (> 0.19)
141
171
 
142
172
  BUNDLED WITH
143
- 2.7.2
173
+ 4.0.10
data/README.md CHANGED
@@ -23,16 +23,16 @@ Build your [shale](https://github.com/kgiszczak/shale) classes like a boss.
23
23
 
24
24
  ```rb
25
25
  transaction = Transaction.build do |t|
26
- t.cvv_code = '123'
27
- t.amount do |a|
28
- a.value = 2.3
29
- a.currency = 'PLN'
30
- end
31
- t.payment_instrument do |p|
32
- p.number = '4242424242424242'
33
- p.expiration_year = 2045
34
- p.expiration_month = 12
35
- end
26
+ t.cvv_code = '123'
27
+ t.amount do |a|
28
+ a.value = 2.3
29
+ a.currency = 'PLN'
30
+ end
31
+ t.payment_instrument do |p|
32
+ p.number = '4242424242424242'
33
+ p.expiration_year = 2045
34
+ p.expiration_month = 12
35
+ end
36
36
  end
37
37
  ```
38
38
 
@@ -48,10 +48,10 @@ You can use it like that:
48
48
  require 'shale/builder'
49
49
 
50
50
  class Amount < Shale::Mapper
51
- include Shale::Builder
51
+ include Shale::Builder
52
52
 
53
- attribute :value, Shale::Type::Float
54
- attribute :currency, Shale::Type::String
53
+ attribute :value, Shale::Type::Float
54
+ attribute :currency, Shale::Type::String
55
55
  end
56
56
  ```
57
57
 
@@ -65,8 +65,8 @@ You can do it like that:
65
65
 
66
66
  ```rb
67
67
  amount = Amount.build do |a|
68
- a.value = 2.3
69
- a.currency = 'PLN'
68
+ a.value = 2.3
69
+ a.currency = 'PLN'
70
70
  end
71
71
  ```
72
72
 
@@ -79,26 +79,26 @@ Let's say that you've got a structure like this:
79
79
 
80
80
  ```rb
81
81
  class Amount < Shale::Mapper
82
- include Shale::Builder
82
+ include Shale::Builder
83
83
 
84
- attribute :value, Shale::Type::Float
85
- attribute :currency, Shale::Type::String
84
+ attribute :value, Shale::Type::Float
85
+ attribute :currency, Shale::Type::String
86
86
  end
87
87
 
88
88
  class PaymentInstrument < Shale::Mapper
89
- include Shale::Builder
89
+ include Shale::Builder
90
90
 
91
- attribute :number, Shale::Type::String
92
- attribute :expiration_year, ::Shale::Type::Integer
93
- attribute :expiration_month, ::Shale::Type::Integer
91
+ attribute :number, Shale::Type::String
92
+ attribute :expiration_year, ::Shale::Type::Integer
93
+ attribute :expiration_month, ::Shale::Type::Integer
94
94
  end
95
95
 
96
96
  class Transaction < Shale::Mapper
97
- include Shale::Builder
97
+ include Shale::Builder
98
98
 
99
- attribute :cvv_code, Shale::Type::String
100
- attribute :amount, Amount
101
- attribute :payment_instrument, PaymentInstrument
99
+ attribute :cvv_code, Shale::Type::String
100
+ attribute :amount, Amount
101
+ attribute :payment_instrument, PaymentInstrument
102
102
  end
103
103
  ```
104
104
 
@@ -106,16 +106,16 @@ Normally you would instantiate it like that:
106
106
 
107
107
  ```rb
108
108
  transaction = Transaction.new(
109
- cvv_code: '123',
110
- amount: Amount.new(
111
- value: 2.3,
112
- currency: 'PLN'
113
- ),
114
- payment_instrument: PaymentInstrument.new(
115
- number: '4242424242424242',
116
- expiration_year: 2045,
117
- expiration_month: 12
118
- )
109
+ cvv_code: '123',
110
+ amount: Amount.new(
111
+ value: 2.3,
112
+ currency: 'PLN'
113
+ ),
114
+ payment_instrument: PaymentInstrument.new(
115
+ number: '4242424242424242',
116
+ expiration_year: 2045,
117
+ expiration_month: 12
118
+ )
119
119
  )
120
120
  ```
121
121
 
@@ -127,16 +127,16 @@ This gem provides a better way:
127
127
 
128
128
  ```rb
129
129
  transaction = Transaction.build do |t|
130
- t.cvv_code = '123'
131
- t.amount do |a|
132
- a.value = 2.3
133
- a.currency = 'PLN'
134
- end
135
- t.payment_instrument do |p|
136
- p.number = '4242424242424242'
137
- p.expiration_year = 2045
138
- p.expiration_month = 12
139
- end
130
+ t.cvv_code = '123'
131
+ t.amount do |a|
132
+ a.value = 2.3
133
+ a.currency = 'PLN'
134
+ end
135
+ t.payment_instrument do |p|
136
+ p.number = '4242424242424242'
137
+ p.expiration_year = 2045
138
+ p.expiration_month = 12
139
+ end
140
140
  end
141
141
  ```
142
142
 
@@ -152,13 +152,13 @@ it will override the previous object with a new one
152
152
 
153
153
  ```rb
154
154
  transaction = Transaction.build do |t|
155
- t.amount do |a|
156
- a.value = 2.3
157
- a.currency = 'PLN'
158
- end
159
- t.amount do |a|
160
- a.value = 10
161
- end
155
+ t.amount do |a|
156
+ a.value = 2.3
157
+ a.currency = 'PLN'
158
+ end
159
+ t.amount do |a|
160
+ a.value = 10
161
+ end
162
162
  end
163
163
 
164
164
  transaction.amount.value #=> 10
@@ -173,13 +173,13 @@ You do that by prepending the method name with `memo_`.
173
173
 
174
174
  ```rb
175
175
  transaction = Transaction.build do |t|
176
- t.amount do |a|
177
- a.value = 2.3
178
- a.currency = 'PLN'
179
- end
180
- t.memo_amount do |a|
181
- a.value = 10
182
- end
176
+ t.amount do |a|
177
+ a.value = 2.3
178
+ a.currency = 'PLN'
179
+ end
180
+ t.memo_amount do |a|
181
+ a.value = 10
182
+ end
183
183
  end
184
184
 
185
185
  transaction.amount.value #=> 10
@@ -194,17 +194,17 @@ Let's define a schema like this.
194
194
 
195
195
  ```rb
196
196
  class Client < Shale::Mapper
197
- include Shale::Builder
197
+ include Shale::Builder
198
198
 
199
- attribute :first_name, Shale::Type::String
200
- attribute :last_name, Shale::Type::String
201
- attribute :email, Shale::Type::String
199
+ attribute :first_name, Shale::Type::String
200
+ attribute :last_name, Shale::Type::String
201
+ attribute :email, Shale::Type::String
202
202
  end
203
203
 
204
204
  class Transaction < Shale::Mapper
205
- include Shale::Builder
205
+ include Shale::Builder
206
206
 
207
- attribute :clients, Client, collection: true
207
+ attribute :clients, Client, collection: true
208
208
  end
209
209
  ```
210
210
 
@@ -239,18 +239,18 @@ objects conditionally.
239
239
 
240
240
  ```rb
241
241
  transaction = Transaction.build do |t|
242
- t.cvv_code = '123'
243
- t.amount do |a|
244
- a.value = 2.3 if some_flag?
245
- a.currency = 'PLN'
246
- end
247
- t.payment_instrument do |p|
248
- p.number = '4242424242424242'
249
- if some_condition?
250
- p.expiration_year = 2045
251
- p.expiration_month = 12
252
- end
242
+ t.cvv_code = '123'
243
+ t.amount do |a|
244
+ a.value = 2.3 if some_flag?
245
+ a.currency = 'PLN'
246
+ end
247
+ t.payment_instrument do |p|
248
+ p.number = '4242424242424242'
249
+ if some_condition?
250
+ p.expiration_year = 2045
251
+ p.expiration_month = 12
253
252
  end
253
+ end
254
254
  end
255
255
  ```
256
256
 
@@ -269,32 +269,32 @@ Then you can use it like so
269
269
 
270
270
  ```rb
271
271
  class AmountType < ::Shale::Mapper
272
- include ::Shale::Builder
273
- include ::ActiveModel::Validations
274
- include ::Shale::Builder::NestedValidations
272
+ include ::Shale::Builder
273
+ include ::ActiveModel::Validations
274
+ include ::Shale::Builder::NestedValidations
275
275
 
276
- attribute :value, ::Shale::Type::Float
277
- attribute :currency, ::Shale::Type::String
276
+ attribute :value, ::Shale::Type::Float
277
+ attribute :currency, ::Shale::Type::String
278
278
 
279
- validates :value, presence: true
279
+ validates :value, presence: true
280
280
  end
281
281
 
282
282
  class TransactionType < ::Shale::Mapper
283
- include ::Shale::Builder
284
- include ::ActiveModel::Validations
285
- include ::Shale::Builder::NestedValidations
283
+ include ::Shale::Builder
284
+ include ::ActiveModel::Validations
285
+ include ::Shale::Builder::NestedValidations
286
286
 
287
- attribute :cvv_code, ::Shale::Type::String
288
- attribute :amount, AmountType
287
+ attribute :cvv_code, ::Shale::Type::String
288
+ attribute :amount, AmountType
289
289
 
290
- validates :cvv_code, presence: true
291
- validates :amount, presence: true
290
+ validates :cvv_code, presence: true
291
+ validates :amount, presence: true
292
292
  end
293
293
 
294
294
  obj = TransactionType.build do |t|
295
- t.amount do |a|
296
- a.currency = 'USD'
297
- end
295
+ t.amount do |a|
296
+ a.currency = 'USD'
297
+ end
298
298
  end
299
299
 
300
300
  obj.valid? #=> false
@@ -308,14 +308,14 @@ The attribute name separator can be changed like so:
308
308
 
309
309
  ```rb
310
310
  class TransactionType < ::Shale::Mapper
311
- self.nested_attr_name_separator = ':'
311
+ self.nested_attr_name_separator = ':'
312
312
  end
313
313
 
314
314
 
315
315
  obj = TransactionType.build do |t|
316
- t.amount do |a|
317
- a.currency = 'USD'
318
- end
316
+ t.amount do |a|
317
+ a.currency = 'USD'
318
+ end
319
319
  end
320
320
 
321
321
  obj.valid? #=> false
@@ -338,11 +338,11 @@ Then you can use it like so
338
338
 
339
339
  ```rb
340
340
  class AmountType < ::Shale::Mapper
341
- include ::Shale::Builder
342
- include ::Shale::Builder::AssignedAttributes
341
+ include ::Shale::Builder
342
+ include ::Shale::Builder::AssignedAttributes
343
343
 
344
- attribute :value, ::Shale::Type::Float
345
- attribute :currency, ::Shale::Type::String
344
+ attribute :value, ::Shale::Type::Float
345
+ attribute :currency, ::Shale::Type::String
346
346
  end
347
347
 
348
348
  obj = AmountType.new
@@ -368,16 +368,16 @@ Then you can use it like so
368
368
  require 'shale/builder'
369
369
 
370
370
  class Amount < Shale::Mapper
371
- include Shale::Builder
371
+ include Shale::Builder
372
372
 
373
- attribute :value, Shale::Type::Float
374
- attribute :currency, Shale::Type::String
373
+ attribute :value, Shale::Type::Float
374
+ attribute :currency, Shale::Type::String
375
375
 
376
- alias_attribute :val, :value
376
+ alias_attribute :val, :value
377
377
  end
378
378
 
379
379
  a = Amount.build do |a|
380
- a.val = 3.2
380
+ a.val = 3.2
381
381
  end
382
382
 
383
383
  a.val #=> 3.2
@@ -395,14 +395,14 @@ It will be used to generate a comment in the RBI file.
395
395
  require 'shale/builder'
396
396
 
397
397
  class Amount < Shale::Mapper
398
- include Shale::Builder
399
-
400
- attribute :value, Shale::Type::Float
401
- attribute :currency, Shale::Type::String, doc: <<~DOC
402
- This is some custom documentation that can be used by sorbet.
403
- It will be used by the tapioca DSL compiler
404
- to generate the RBI documentation for this attribute.
405
- DOC
398
+ include Shale::Builder
399
+
400
+ attribute :value, Shale::Type::Float
401
+ attribute :currency, Shale::Type::String, doc: <<~DOC
402
+ This is some custom documentation that can be used by sorbet.
403
+ It will be used by the tapioca DSL compiler
404
+ to generate the RBI documentation for this attribute.
405
+ DOC
406
406
  end
407
407
  ```
408
408
 
@@ -464,40 +464,40 @@ require 'shale/builder'
464
464
  # Cast from XML string to BigDecimal.
465
465
  # And from BigDecimal to XML string.
466
466
  class BigDecimalShaleType < Shale::Type::Value
467
- class << self
468
- extend T::Sig
467
+ class << self
468
+ extend T::Sig
469
469
 
470
- # the return type of the field that uses this class as its type
471
- def return_type = T.nilable(BigDecimal)
472
- # the type of the argument given to a setter of the field
473
- # that uses this class as its type
474
- def setter_type = T.any(BigDecimal, String, NilClass)
470
+ # the return type of the field that uses this class as its type
471
+ def return_type = T.nilable(BigDecimal)
472
+ # the type of the argument given to a setter of the field
473
+ # that uses this class as its type
474
+ def setter_type = T.any(BigDecimal, String, NilClass)
475
475
 
476
- # Decode from XML.
477
- sig { params(value: T.any(BigDecimal, String, NilClass)).returns(T.nilable(BigDecimal)) }
478
- def cast(value)
479
- return if value.nil?
476
+ # Decode from XML.
477
+ sig { params(value: T.any(BigDecimal, String, NilClass)).returns(T.nilable(BigDecimal)) }
478
+ def cast(value)
479
+ return if value.nil?
480
480
 
481
- BigDecimal(value)
482
- end
481
+ BigDecimal(value)
482
+ end
483
483
 
484
- # Encode to XML.
485
- #
486
- # @param value: Value to convert to XML
487
- sig { params(value: T.nilable(BigDecimal)).returns(T.nilable(String)) }
488
- def as_xml_value(value)
489
- return if value.nil?
484
+ # Encode to XML.
485
+ #
486
+ # @param value: Value to convert to XML
487
+ sig { params(value: T.nilable(BigDecimal)).returns(T.nilable(String)) }
488
+ def as_xml_value(value)
489
+ return if value.nil?
490
490
 
491
- value.to_s('F')
492
- end
491
+ value.to_s('F')
493
492
  end
493
+ end
494
494
  end
495
495
 
496
496
  class Amount < Shale::Mapper
497
- include Shale::Builder
497
+ include Shale::Builder
498
498
 
499
- # `value` uses BigDecimalShaleType as its type
500
- attribute :value, BigDecimalShaleType
499
+ # `value` uses BigDecimalShaleType as its type
500
+ attribute :value, BigDecimalShaleType
501
501
  end
502
502
  ```
503
503
 
@@ -523,6 +523,244 @@ class Amount
523
523
  end
524
524
  ```
525
525
 
526
+ #### Parsing strings or other primitive values to shale objects
527
+
528
+ If you want to parse a primitive value like a string, integer, float etc
529
+ to a shale mapper object you can use a primitive shale type as a bridge.
530
+
531
+
532
+ By defining the `builder` method that returns a shale mapper class
533
+ of the parsed value, you can make this composite type usable with the builder DSL.
534
+
535
+ ```rb
536
+ def self.builder = ResourceURI::Body
537
+ ```
538
+
539
+ Here is a full example.
540
+
541
+ ```rb
542
+ # typed: true
543
+ require 'shale/builder'
544
+
545
+ # Resource URI is a string serialization format eg. `"pan:4242424242424242;expiry=2027-04;cvc=123"`
546
+ # - schema is `"pan"`
547
+ # - id is `"4242424242424242"`
548
+ # - properties are:
549
+ # - `expiry` is `"2027-04"`
550
+ # - `cvc` is `"123"`
551
+ module ResourceURI
552
+ class Properties < Shale::Type::Value
553
+ class << self
554
+ def return_type = T.nilable(T::Hash[String, String])
555
+ def setter_type = T.any(T::Hash[String, String], NilClass)
556
+ def builder = Hash
557
+
558
+ # Decode from JSON.
559
+ #
560
+ #: (Hash[String, String]?) -> (Hash[String, String]?)
561
+ def cast(value)
562
+ value
563
+ end
564
+
565
+ # Encode to JSON.
566
+ #
567
+ #: (Hash[String, String]?, **untyped) -> (Hash[String, String]?)
568
+ def as_json(value, **_)
569
+ return if value.nil?
570
+
571
+ value.as_json
572
+ end
573
+ end
574
+ end
575
+
576
+ class Body < Shale::Mapper
577
+ include Shale::Builder
578
+
579
+ attribute :schema, S::String, doc: <<~DOC
580
+ required
581
+
582
+ String identifying the resource.
583
+ DOC
584
+
585
+ attribute :id, S::String, doc: <<~DOC
586
+ optional
587
+
588
+ Main identifier of the resource.
589
+ DOC
590
+
591
+ attribute :properties, Properties, doc: <<~DOC
592
+ optional
593
+
594
+ Additional properties of the resource
595
+ DOC
596
+
597
+ #: -> String
598
+ def to_resource_uri
599
+ buf = String.new
600
+ buf << schema
601
+ buf << ':'
602
+ buf << id if id
603
+
604
+ properties.each do |hkey, hval|
605
+ buf << ';'
606
+ buf << hkey
607
+ buf << '='
608
+ buf << hval
609
+ end
610
+
611
+ buf
612
+ end
613
+ end
614
+
615
+ class Type < Shale::Type::Value
616
+ class << self
617
+ def return_type = T.nilable(Body)
618
+ def setter_type = T.any(Body, String, NilClass)
619
+ def builder = Body
620
+
621
+ # Decode from JSON.
622
+ #
623
+ #: (Body | String?) -> (Body?)
624
+ def cast(value)
625
+ return if value.nil?
626
+ return value unless value.is_a?(String)
627
+
628
+ hash = {}
629
+ schema, rest = value.split(':', 2)
630
+ if rest
631
+ pairs = rest.split(';')
632
+ id = pairs.shift
633
+ end
634
+
635
+ properties = T.unsafe(pairs).to_h { |str| str.split('=', 2) }
636
+ hash = {
637
+ 'schema' => schema,
638
+ 'id' => id,
639
+ 'properties' => properties,
640
+ }
641
+
642
+ Body.from_hash(hash)
643
+ end
644
+
645
+ # Encode to JSON.
646
+ #
647
+ #: (Body | String?, **untyped) -> String?
648
+ def as_json(value, **_)
649
+ return value if value.nil?
650
+ return value if value.is_a?(String)
651
+
652
+ value.to_resource_uri
653
+ end
654
+ alias as_hash as_json
655
+ end
656
+ end
657
+ end
658
+
659
+ class Payment < Shale::Mapper
660
+ include Shale::Builder
661
+
662
+ attribute :account_uri, ResourceURI::Type
663
+ end
664
+
665
+ payment = Payment.build do |p|
666
+ p.account_uri do |a|
667
+ a.schema = 'pan'
668
+ a.id = "4242424242424242"
669
+ a.properties do |p|
670
+ p['expiry'] = '2027-04'
671
+ p['cvc'] = '123'
672
+ end
673
+ end
674
+ end
675
+
676
+ payment.to_json
677
+ #=> {"account_uri":"pan:4242424242424242;expiry=2027-04;cvc=123"}
678
+
679
+ payment = Payment.from_json('{"account_uri":"pan:4242424242424242;expiry=2027-04;cvc=123"}')
680
+ #=> #<Payment:0x00000001215413f8 @account_uri=#<ResourceURI::Body:0x00000001216aff28 @id="4242424242424242", @schema="pan", @properties={"expiry" => "2027-04", "cvc" => "123"}>>
681
+ ```
682
+
683
+ After running `bundle exec tapioca dsl` you'll get the following RBI.
684
+
685
+ ```rb
686
+ # typed: true
687
+
688
+ # DO NOT EDIT MANUALLY
689
+ # This is an autogenerated file for dynamic methods in `Amount`.
690
+ # Please instead update this file by running `bin/tapioca dsl Amount`.
691
+
692
+ class ResourceURI::Body
693
+ include ShaleAttributeMethods
694
+
695
+ module ShaleAttributeMethods
696
+ # optional
697
+ #
698
+ # Main identifier of the resource.
699
+ sig { returns(T.nilable(String)) }
700
+ def id; end
701
+
702
+ # optional
703
+ #
704
+ # Main identifier of the resource.
705
+ sig { params(value: T.nilable(String)).returns(T.nilable(String)) }
706
+ def id=(value); end
707
+
708
+ # Version of `properties` that memoizes the previous object if it was already present.
709
+ # optional
710
+ #
711
+ # Additional properties of the resource
712
+ sig { params(block: T.proc.params(arg0: Hash).void).returns(Hash) }
713
+ def memo_properties(&block); end
714
+
715
+ # optional
716
+ #
717
+ # Additional properties of the resource
718
+ sig { params(block: NilClass).returns(T.nilable(T::Hash[::String, ::String])) }
719
+ sig { params(block: T.proc.params(arg0: Hash).void).returns(Hash) }
720
+ def properties(&block); end
721
+
722
+ # optional
723
+ #
724
+ # Additional properties of the resource
725
+ sig { params(value: T.nilable(T::Hash[::String, ::String])).returns(T.nilable(T::Hash[::String, ::String])) }
726
+ def properties=(value); end
727
+
728
+ # required
729
+ #
730
+ # String identifying the resource.
731
+ sig { returns(T.nilable(String)) }
732
+ def schema; end
733
+
734
+ # required
735
+ #
736
+ # String identifying the resource.
737
+ sig { params(value: T.nilable(String)).returns(T.nilable(String)) }
738
+ def schema=(value); end
739
+ end
740
+ end
741
+
742
+ class Payment
743
+ include ShaleAttributeMethods
744
+
745
+ module ShaleAttributeMethods
746
+ sig { params(block: NilClass).returns(T.nilable(::ResourceURI::Body)) }
747
+ sig { params(block: T.proc.params(arg0: ResourceURI::Body).void).returns(ResourceURI::Body) }
748
+ def account_uri(&block); end
749
+
750
+ sig do
751
+ params(
752
+ value: T.nilable(T.any(::ResourceURI::Body, ::String))
753
+ ).returns(T.nilable(T.any(::ResourceURI::Body, ::String)))
754
+ end
755
+ def account_uri=(value); end
756
+
757
+ # Version of `account_uri` that memoizes the previous object if it was already present.
758
+ sig { params(block: T.proc.params(arg0: ResourceURI::Body).void).returns(ResourceURI::Body) }
759
+ def memo_account_uri(&block); end
760
+ end
761
+ end
762
+ ```
763
+
526
764
  ## Development
527
765
 
528
766
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -13,6 +13,9 @@ module Shale
13
13
  #: untyped
14
14
  attr_accessor :return_type
15
15
 
16
+ #: untyped
17
+ attr_accessor :builder
18
+
16
19
  # Contains the documentation comment for the shale attribute
17
20
  # in a Ruby String.
18
21
  #: String?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Shale
4
4
  module Builder
5
- VERSION = '0.9.3'
5
+ VERSION = '0.11.0'
6
6
  end
7
7
  end
data/lib/shale/builder.rb CHANGED
@@ -72,7 +72,7 @@ module Shale
72
72
  extend T::Generic
73
73
 
74
74
  abstract!
75
- has_attached_class!
75
+ has_attached_class!(:out)
76
76
 
77
77
  #: (Class subclass) -> void
78
78
  def inherited(subclass)
@@ -107,6 +107,7 @@ module Shale
107
107
  #| ?doc: String?,
108
108
  #| ?return_type: untyped,
109
109
  #| ?setter_type: untyped,
110
+ #| ?builder: untyped,
110
111
  #| **Object kwargs
111
112
  #| ) ?{ -> void } -> void
112
113
  def attribute(
@@ -117,43 +118,66 @@ module Shale
117
118
  doc: nil,
118
119
  return_type: nil,
119
120
  setter_type: nil,
121
+ builder: nil,
120
122
  **kwargs,
121
123
  &block
122
124
  )
123
125
  super(name, shale_mapper, collection:, default:, **kwargs, &block)
126
+
127
+ if shale_mapper < ::Shale::Mapper
128
+ builder ||= shale_mapper
129
+ end
130
+ if shale_mapper.respond_to?(:builder)
131
+ builder ||= T.unsafe(shale_mapper).builder
132
+ end
133
+
124
134
  if (attr_def = attributes[name.to_sym])
125
135
  attr_def.doc = doc
126
136
  attr_def.return_type = return_type
127
137
  attr_def.setter_type = setter_type
138
+ attr_def.builder = builder
128
139
  end
129
- return unless shale_mapper < ::Shale::Mapper
130
140
 
141
+ def_getter(name, builder, collection) if builder
142
+ end
143
+
144
+ #: (String | Symbol, untyped, bool) -> void
145
+ def def_getter(name, builder, collection)
131
146
  if collection
132
- @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
133
- def #{name}
134
- return super unless block_given?
135
-
136
- arr = self.#{name} ||= []
137
- object = #{shale_mapper}.new
138
- yield(object)
139
- arr << object
140
- object
141
- end
142
- RUBY
143
- return
147
+ return def_collection_getter(name, builder)
144
148
  end
145
149
 
150
+ def_regular_getter(name, builder)
151
+ end
152
+
153
+ #: (String | Symbol, untyped) -> void
154
+ def def_collection_getter(name, builder)
155
+ @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
156
+ def #{name}
157
+ return super unless block_given?
158
+
159
+ arr = self.#{name} ||= []
160
+ object = #{builder}.new
161
+ yield(object)
162
+ arr << object
163
+ object
164
+ end
165
+ RUBY
166
+ end
167
+
168
+ #: (String | Symbol, untyped) -> void
169
+ def def_regular_getter(name, builder)
146
170
  @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
147
171
  def #{name}
148
172
  return super unless block_given?
149
173
 
150
- object = #{shale_mapper}.new
174
+ object = #{builder}.new
151
175
  yield(object)
152
176
  self.#{name} = object
153
177
  end
154
178
 
155
179
  def memo_#{name}
156
- object = self.#{name} || #{shale_mapper}.new
180
+ object = self.#{name} || #{builder}.new
157
181
  yield(object)
158
182
  self.#{name} = object
159
183
  end
@@ -75,8 +75,8 @@ module Tapioca
75
75
 
76
76
  attribute.all_names.each do |name|
77
77
  name_str = name.to_s
78
- if has_shale_builder && attribute.type < ::Shale::Mapper
79
- generate_mapper_getter(mod, name_str, return_type, getter_without_block_type, comments)
78
+ if has_shale_builder && attribute.builder
79
+ generate_mapper_getter(mod, name_str, attribute.builder, getter_without_block_type, comments)
80
80
  else
81
81
  mod.create_method(name_str, return_type: getter_without_block_type, comments: comments)
82
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shale-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
@@ -43,14 +43,14 @@ dependencies:
43
43
  requirements:
44
44
  - - ">"
45
45
  - !ruby/object:Gem::Version
46
- version: '0.5'
46
+ version: '0.6'
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.5'
53
+ version: '0.6'
54
54
  description: An addon to the shale Ruby gem which adds a simple yet powerful builder
55
55
  DSL.
56
56
  email:
@@ -90,14 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
92
92
  - !ruby/object:Gem::Version
93
- version: 3.1.0
93
+ version: 3.2.0
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  requirements: []
100
- rubygems_version: 3.7.2
100
+ rubygems_version: 4.0.20
101
101
  specification_version: 4
102
102
  summary: An addon to the shale Ruby gem which adds a simple yet powerful builder DSL.
103
103
  test_files: []