Skip to content

Commit 2c9fe6a

Browse files
committed
v0.4.1
1 parent bb30cfd commit 2c9fe6a

4 files changed

Lines changed: 36 additions & 49 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.4.1] - 2025-10-14
9+
10+
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.4.0...v0.4.1)
11+
12+
### Changes
13+
- Fix `Shale::Builder::alias_attribute`, improve changelog
14+
815
## [0.4.0] - 2025-10-14
916

10-
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.3.0...v0.2.1)
17+
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.3.0...v0.4.0)
1118

1219
### Changes
1320
- Add `Shale::Builder::NestedValidations`

README.md

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -270,43 +270,25 @@ You can easily create aliases for attributes using `alias_attribute`
270270
Then you can use it like so
271271

272272
```rb
273-
class AmountType < ::Shale::Mapper
274-
include ::Shale::Builder
275-
include ::ActiveModel::Validations
276-
include ::Shale::Builder::NestedValidations
277-
278-
attribute :value, ::Shale::Type::Float
279-
attribute :currency, ::Shale::Type::String
280-
281-
validates :value, presence: true
282-
end
273+
require 'shale/builder'
283274

284-
class TransactionType < ::Shale::Mapper
285-
include ::Shale::Builder
286-
include ::ActiveModel::Validations
287-
include ::Shale::Builder::NestedValidations
275+
class Amount < Shale::Mapper
276+
include Shale::Builder
288277

289-
attribute :cvv_code, ::Shale::Type::String
290-
attribute :amount, AmountType
278+
attribute :value, Shale::Type::Float
279+
attribute :currency, Shale::Type::String
291280

292-
validates :cvv_code, presence: true
293-
validates :amount, presence: true
281+
alias_attribute :val, :value
294282
end
295283

296-
obj = TransactionType.build do |t|
297-
t.amount do |a|
298-
a.currency = 'USD'
299-
end
284+
a = Amount.build do |a|
285+
a.val = 3.2
300286
end
301287

302-
obj.valid? #=> false
303-
obj.errors #=> #<ActiveModel::Errors [#<ActiveModel::Error attribute=cvv_code, type=blank, options={}>, #<ActiveModel::NestedError attribute=amount.value, type=blank, options={}>]>
304-
obj.errors.messages #=> {cvv_code: ["can't be blank"], "amount.value": ["can't be blank"]}
288+
a.val #=> 3.2
289+
a.value #=> 3.2
305290
```
306291

307-
You MUST include `ActiveModel::Validations` before `Shale::Builder::NestedValidations`.
308-
309-
310292
### Sorbet support
311293

312294
Shale-builder adds support for sorbet and tapioca.

lib/shale/builder.rb

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,25 @@ def #{name} # def amount
140140
RUBY
141141
end
142142

143-
end
144-
mixes_in_class_methods(ClassMethods)
145-
146-
# Create an alias for the getter and setter of an attribute.
147-
sig { params(new_name: Symbol, old_name: Symbol).void }
148-
def alias_attribute(new_name, old_name)
149-
klass = T.unsafe(self).class
150-
151-
attr = klass.attributes[old_name]
152-
(attr.aliases ||= []) << new_name
153-
154-
klass.builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
155-
def #{new_name}
156-
#{old_name}
157-
end
143+
# Create an alias for the getter and setter of an attribute.
144+
sig { params(new_name: Symbol, old_name: Symbol).void }
145+
def alias_attribute(new_name, old_name)
146+
attr = attributes.fetch(old_name)
147+
(attr.aliases ||= []) << new_name
148+
149+
builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
150+
def #{new_name}
151+
#{old_name}
152+
end
153+
154+
def #{new_name}=(val)
155+
self.#{old_name} = val
156+
end
157+
RUBY
158+
end
158159

159-
def #{new_name}=(val)
160-
self.#{old_name} = val
161-
end
162-
RUBY
163160
end
161+
mixes_in_class_methods(ClassMethods)
164162

165163
end
166164
end

lib/shale/builder/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Shale
44
module Builder
5-
VERSION = '0.4.0'
5+
VERSION = '0.4.1'
66
end
77
end

0 commit comments

Comments
 (0)