Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/attr_struct' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Jan 15, 2021
2 parents fe4d32d + a16ed8a commit aaf5c03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/jsi/util/attr_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def [](*attribute_keys)

Class.new(AttrStruct).tap do |klass|
klass.define_singleton_method(:attribute_keys) { attribute_keys }
klass.define_method(:attribute_keys) { attribute_keys }
klass.send(:define_method, :attribute_keys) { attribute_keys }
attribute_keys.each do |attribute_key|
# reader
klass.define_method(attribute_key) do
klass.send(:define_method, attribute_key) do
@attributes[attribute_key]
end

# writer
klass.define_method("#{attribute_key}=") do |value|
klass.send(:define_method, "#{attribute_key}=") do |value|
@attributes[attribute_key] = value
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@
assert_equal(expected, actual)
end
end
describe 'AttrStruct' do
Foo = JSI::Util::AttrStruct[*%w(bar)]
it 'structs' do
foo = Foo.new(bar: 'bar')
assert_equal('bar', foo.bar)
foo.bar = 'baar'
assert_equal('baar', foo.bar)
assert_equal(foo, Foo.new(bar: 'baar'))
refute_equal(foo, Foo.new(bar: 'baaar'))
end
end
end

0 comments on commit aaf5c03

Please sign in to comment.