Skip to content

Commit

Permalink
Set deconstruct to fix return order by mebers definition order.
Browse files Browse the repository at this point in the history
Remove to_a
  • Loading branch information
saturnflyer committed Jan 21, 2023
1 parent 2364b0c commit ed3bfb5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- freeze after initializing
- `to_a` removed
- `deconstruct` fixed to return array in order of members as defined

## 1.0.1

### Added
Expand Down
8 changes: 2 additions & 6 deletions lib/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def members
end

def initialize(**kwargs)
@attributes = kwargs
@attributes = Hash[members.map {|m| [m,kwargs[m]] }]
end

def deconstruct
to_a
@attributes.values
end

def deconstruct_keys(array)
Expand All @@ -69,10 +69,6 @@ def deconstruct_keys(array)
@attributes.slice(*array)
end

def to_a
@attributes.values
end

def to_h(&block)
@attributes.to_h(&block)
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def test_instance_behavior
assert_equal({foo: 1}, test.deconstruct_keys(%i[foo baz]))
assert_raises(TypeError) { test.deconstruct_keys(0) }

test = klass.new(bar: 2, foo: 1)
assert_equal([1, 2], test.deconstruct)
assert_equal([1, 2], test.to_a)

assert_predicate(test, :frozen?)

Expand Down

0 comments on commit ed3bfb5

Please sign in to comment.