beefcake 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/beefcake.rb +23 -2
- data/test/message_test.rb +7 -0
- metadata +3 -3
data/lib/beefcake.rb
CHANGED
@@ -104,7 +104,16 @@ module Beefcake
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def valid_enum?(mod, val)
|
107
|
-
mod
|
107
|
+
!!name_for(mod, val)
|
108
|
+
end
|
109
|
+
|
110
|
+
def name_for(mod, val)
|
111
|
+
mod.constants.each do |name|
|
112
|
+
if mod.const_get(name) == val
|
113
|
+
return name
|
114
|
+
end
|
115
|
+
end
|
116
|
+
nil
|
108
117
|
end
|
109
118
|
|
110
119
|
def validate!
|
@@ -204,7 +213,19 @@ module Beefcake
|
|
204
213
|
|
205
214
|
def inspect
|
206
215
|
set = fields.values.select {|fld| self[fld.name] != nil }
|
207
|
-
|
216
|
+
|
217
|
+
flds = set.map do |fld|
|
218
|
+
val = self[fld.name]
|
219
|
+
|
220
|
+
case fld.type
|
221
|
+
when Module
|
222
|
+
title = name_for(fld.type, val) || "-NA-"
|
223
|
+
"#{fld.name}: #{title}(#{val.inspect})"
|
224
|
+
else
|
225
|
+
"#{fld.name}: #{val.inspect}"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
208
229
|
"<#{self.class.name} #{flds.join(", ")}>"
|
209
230
|
end
|
210
231
|
|
data/test/message_test.rb
CHANGED
@@ -318,4 +318,11 @@ class MessageTest < Test::Unit::TestCase
|
|
318
318
|
assert_equal "<SimpleMessage b: \"testing\">", msg.inspect
|
319
319
|
end
|
320
320
|
|
321
|
+
def test_inspect_enums
|
322
|
+
msg = EnumsMessage.new :a => 1
|
323
|
+
assert_equal "<EnumsMessage a: A(1)>", msg.inspect
|
324
|
+
msg.a = 2
|
325
|
+
assert_equal "<EnumsMessage a: -NA-(2)>", msg.inspect
|
326
|
+
end
|
327
|
+
|
321
328
|
end
|