Skip to content

Commit 6544839

Browse files
committed
Improve AssignedAttributes
1 parent f02a501 commit 6544839

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ 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.6.3] - 2025-10-16
9+
10+
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.6.2...v0.6.3)
11+
12+
### Changes
13+
- Improve `Shale::Builder::AssignedAttributes` to handle assignment in methods like `from_hash`
14+
815
## [0.6.2] - 2025-10-16
916

1017
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.6.0...v0.6.2)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
shale-builder (0.6.2)
4+
shale-builder (0.6.3)
55
booleans (>= 0.1)
66
shale (< 2.0)
77
sorbet-runtime (> 0.5)

lib/shale/builder/assigned_attributes.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module AssignedAttributes
1414
extend T::Sig
1515
extend T::Helpers
1616

17+
DEFAULT = Object.new
18+
1719
class << self
1820
extend T::Sig
1921

@@ -43,14 +45,27 @@ module ClassMethods
4345
#: Module
4446
attr_reader :assigned_attributes_methods_module
4547

46-
#: (String | Symbol name, Class type, ?collection: bool, ?default: Proc?, ?doc: String?, **untyped kwargs) ?{ -> void } -> void
47-
def attribute(name, type, collection: false, default: nil, doc: nil, **kwargs, &block)
48-
super
48+
#: (
49+
#| String | Symbol name,
50+
#| Class shale_mapper,
51+
#| ?collection: bool,
52+
#| ?default: Proc?,
53+
#| **Object kwargs
54+
#| ) ?{ -> void } -> void
55+
def attribute(
56+
name,
57+
shale_mapper,
58+
collection: false,
59+
default: -> { DEFAULT },
60+
**kwargs,
61+
&block
62+
)
63+
super(name, shale_mapper, collection:, default:, **kwargs, &block) # rubocop:disable Style/SuperArguments
4964

5065
@assigned_attributes_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
5166
def #{name}=(val)
67+
return if val.equal?(::Shale::Builder::AssignedAttributes::DEFAULT)
5268
super
53-
return unless @__initialized
5469
5570
self.assigned_attribute_names << #{name.to_sym.inspect}
5671
end
@@ -59,14 +74,6 @@ def #{name}=(val)
5974
end
6075
mixes_in_class_methods ClassMethods
6176

62-
def initialize(*args, **kwargs, &block)
63-
super
64-
@__initialized = true
65-
end
66-
67-
#: bool?
68-
attr_reader :__initialized
69-
7077
# Returns a set of names of assigned shale attributes.
7178
#
7279
#: -> Set[Symbol]

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.6.2'
5+
VERSION = '0.6.3'
66
end
77
end

test/shale/builder_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class TestClientDataType < ::Shale::Mapper
5050
attribute :email, ::Shale::Type::String
5151

5252
alias_attribute :name, :first_name
53+
54+
hsh do
55+
map :first_name, to: :first_name
56+
map :name, to: :first_name
57+
map :last_name, to: :last_name
58+
map :email, to: :email
59+
end
5360
end
5461

5562
class TestEnhancedTransactionType < TestTransactionType
@@ -125,6 +132,12 @@ class TestMultipleClientTransactionType < TestTransactionType
125132

126133
obj.email = 'bar'
127134
assert_equal Set[:first_name, :email], obj.assigned_attribute_names
135+
136+
obj = TestClientDataType.from_hash({ name: 'foo', email: 'bar' })
137+
assert_equal 'foo', obj.first_name
138+
assert_equal 'bar', obj.email
139+
assert_equal 2, obj.assigned_attribute_names.length
140+
assert_equal Set[:first_name, :email], obj.assigned_attribute_names
128141
end
129142

130143
should 'correctly set up a class after including' do

0 commit comments

Comments
 (0)