beefcake 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/beefcake.rb +23 -2
  2. data/test/message_test.rb +7 -0
  3. 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.constants.any? {|name| mod.const_get(name) == val }
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
- flds = set.map {|fld| "#{fld.name}: #{self[fld.name].inspect}" }
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
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Blake Mizerany