Skip to content

Commit

Permalink
Literal::Enum#to_h should yield members, not values (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper authored Dec 9, 2024
1 parent 43f7be5 commit 97bd071
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/literal/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ def to_proc
method(:coerce).to_proc
end

def to_h(...)
@values.to_h(...)
def to_h(&)
if block_given?
@members.to_h(&)
else
@members.to_h { |it| [it, it.value] }
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/enum.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def toggle = On
expect(Color.coerce(1)) == Color::Red
expect(Color.coerce(Color::Red)) == Color::Red
expect(Color.to_set) == Set[Color::Red, Color::Green, Color::Blue]
expect(Color.to_h) == { 1 => Color::Red, 2 => Color::Green, 3 => Color::Blue }
expect(Color.to_h) == { Color::Red => 1, Color::Green => 2, Color::Blue => 3 }
expect(Color.to_a) == [Color::Red, Color::Green, Color::Blue] if RUBY_VERSION >= "3.2"
expect(Color.values) == [1, 2, 3] if RUBY_VERSION >= "3.2"
expect([3, 2, 1].map(&Color)) == [Color::Blue, Color::Green, Color::Red]
Expand Down

0 comments on commit 97bd071

Please sign in to comment.