Skip to content

Commit

Permalink
Better visibility for values in enum type (fix #460)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 5, 2025
1 parent 2e716b1 commit 55316b1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/dry/types/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ def each_value(&)

alias_method :inspect, :to_s

# @return [String]
#
# @api public
def name
"#{super}(#{joined_values})"
end

# @return [String]
#
# @api private
def joined_values
mapping.keys.map { |value|
if value.is_a?(::String)
value
else
value.inspect
end
}.join("|")
end

private

# Maps a value
Expand Down
3 changes: 1 addition & 2 deletions lib/dry/types/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ def visit_enum(enum)

visit_options(options) do |opts|
if mapping == enum.inverted_mapping
values = mapping.values.map(&:inspect).join(", ")
yield "Enum<#{type} values={#{values}}#{opts}>"
yield "Enum(#{enum.joined_values})<#{type}#{opts}>"
else
mapping_str = mapping.map { |key, value|
"#{key.inspect}=>#{value.inspect}"
Expand Down
44 changes: 42 additions & 2 deletions spec/dry/types/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,30 @@

it "returns string representation of the type" do
expect(type.to_s).to eql(
"#<Dry::Types[Enum<Constrained<Nominal<Integer> " \
"rule=[included_in?([4, 5, 6])]> values={4, 5, 6}>]>"
"#<Dry::Types[Enum(4|5|6)<Constrained<Nominal<Integer> " \
"rule=[included_in?([4, 5, 6])]>>]>"
)
end
end

context "symbols" do
subject(:type) { Dry::Types["nominal.symbol"].enum(:draft, :published, :archived) }

it "returns string representation of the type" do
expect(type.to_s).to eql(
"#<Dry::Types[Enum(:draft|:published|:archived)<Constrained<Nominal<Symbol> " \
"rule=[included_in?([:draft, :published, :archived])]>>]>"
)
end
end

context "strings" do
subject(:type) { Dry::Types["nominal.string"].enum("draft", "published", "archived") }

it "returns string representation of the type" do
expect(type.to_s).to eql(
"#<Dry::Types[Enum(draft|published|archived)<Constrained<Nominal<String> " \
"rule=[included_in?([\"draft\", \"published\", \"archived\"])]>>]>"
)
end
end
Expand Down Expand Up @@ -215,4 +237,22 @@
expect(type.constructor(&:to_i).mapping).to eql(4 => 4, 5 => 5)
end
end

describe "#name" do
subject(:type) { Dry::Types["nominal.integer"].enum(4, 5, 6) }

it "returns the name of the type" do
expect(type.name).to eql("Integer(4|5|6)")
end

context "with mapping" do
let(:mapping) { {0 => "draft", 10 => "published", 20 => "archived"} }

subject(:type) { Dry::Types["nominal.integer"].enum(mapping) }

it "returns the name of the type" do
expect(type.name).to eql("Integer(0|10|20)")
end
end
end
end

0 comments on commit 55316b1

Please sign in to comment.