Skip to content

Commit ecb4abc

Browse files
committed
Define to_s and inspect on enums
1 parent 931c7e4 commit ecb4abc

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/enum_type/enum_class.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def self.create(attributes)
1313

1414
def self.create_from_array(attributes)
1515
attributes = %i[name value] + attributes
16-
Struct.new(*attributes)
16+
klass = Struct.new(*attributes)
17+
define_inspection_methods(klass)
18+
klass
1719
end
1820

1921
def self.create_from_hash(attributes)
@@ -36,7 +38,19 @@ def self.create_from_hash(attributes)
3638
instance_variable_set("@#{name}", values[index])
3739
end
3840
end
41+
define_inspection_methods(klass)
3942
klass
4043
end
44+
45+
def self.define_inspection_methods(klass)
46+
klass.send(:define_method, :to_s) do
47+
name.to_s
48+
end
49+
50+
klass.send(:define_method, :inspect) do
51+
"\#<Enum:#{name} #{value.inspect}>"
52+
end
53+
end
54+
private_class_method :define_inspection_methods
4155
end
4256
end

spec/enum_type_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@
6565
it 'allows getting the list of names as symbols' do
6666
expect(enum.names).to eq %i[RED GREEN BLUE]
6767
end
68+
69+
it 'defines to_s as name on the enums' do
70+
expect(enum.RED.to_s).to eq 'RED'
71+
expect(enum.GREEN.to_s).to eq 'GREEN'
72+
expect(enum.BLUE.to_s).to eq 'BLUE'
73+
end
74+
75+
it 'defines inspect as a reasonable name on the enums' do
76+
expect(enum.RED.inspect).to eq '#<Enum:RED :red>'
77+
expect(enum.GREEN.inspect).to eq '#<Enum:GREEN :green>'
78+
expect(enum.BLUE.inspect).to eq '#<Enum:BLUE :blue>'
79+
end
6880
end
6981

7082
context 'with array attributes' do
@@ -115,6 +127,18 @@
115127
it 'allows getting the list of names as symbols' do
116128
expect(enum.names).to eq %i[RED GREEN BLUE]
117129
end
130+
131+
it 'defines to_s as name on the enums' do
132+
expect(enum.RED.to_s).to eq 'RED'
133+
expect(enum.GREEN.to_s).to eq 'GREEN'
134+
expect(enum.BLUE.to_s).to eq 'BLUE'
135+
end
136+
137+
it 'defines inspect as a reasonable name on the enums' do
138+
expect(enum.RED.inspect).to eq '#<Enum:RED :red>'
139+
expect(enum.GREEN.inspect).to eq '#<Enum:GREEN :green>'
140+
expect(enum.BLUE.inspect).to eq '#<Enum:BLUE :blue>'
141+
end
118142
end
119143

120144
context 'with hash attributes' do
@@ -165,6 +189,18 @@
165189
it 'allows getting the list of names as symbols' do
166190
expect(enum.names).to eq %i[RED GREEN BLUE]
167191
end
192+
193+
it 'defines to_s as name on the enums' do
194+
expect(enum.RED.to_s).to eq 'RED'
195+
expect(enum.GREEN.to_s).to eq 'GREEN'
196+
expect(enum.BLUE.to_s).to eq 'BLUE'
197+
end
198+
199+
it 'defines inspect as a reasonable string on the enums' do
200+
expect(enum.RED.inspect).to eq '#<Enum:RED "red">'
201+
expect(enum.GREEN.inspect).to eq '#<Enum:GREEN "green">'
202+
expect(enum.BLUE.inspect).to eq '#<Enum:BLUE "blue">'
203+
end
168204
end
169205

170206
context 'with hash attributes and an invalid definition' do

0 commit comments

Comments
 (0)