Module: Beefcake::Message

Defined Under Namespace

Modules: Decode, Dsl, Encode Classes: DuplicateFieldNumber, Field, InvalidValueError, RequiredFieldNotSetError, WrongTypeError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(o) ⇒ Object


221
222
223
224
225
# File 'lib/beefcake.rb', line 221

def self.included(o)
  o.extend Dsl
  o.extend Decode
  o.send(:include, Encode)
end

Instance Method Details

#==(o) ⇒ Object


245
246
247
248
249
# File 'lib/beefcake.rb', line 245

def ==(o)
  return false if (o == nil) || (o == false)
  return false unless o.is_a? self.class
  __beefcake_fields__.values.all? {|fld| self[fld.name] == o[fld.name] }
end

#[](k) ⇒ Object


237
238
239
# File 'lib/beefcake.rb', line 237

def [](k)
  __send__(k)
end

#[]=(k, v) ⇒ Object


241
242
243
# File 'lib/beefcake.rb', line 241

def []=(k, v)
  __send__("#{k}=", v)
end

#__beefcake_fields__Object


233
234
235
# File 'lib/beefcake.rb', line 233

def __beefcake_fields__
  self.class.fields
end

#initialize(attrs = {}) ⇒ Object


227
228
229
230
231
# File 'lib/beefcake.rb', line 227

def initialize(attrs={})
  __beefcake_fields__.values.each do |fld|
    self[fld.name] = attrs[fld.name]
  end
end

#inspectObject


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/beefcake.rb', line 251

def inspect
  set = __beefcake_fields__.values.select {|fld| self[fld.name] != nil }

  flds = set.map do |fld|
    val = self[fld.name]

    case fld.type
    when Class
      "#{fld.name}: #{val.inspect}"
    when Module
      title = name_for(fld.type, val) || "-NA-"
      "#{fld.name}: #{title}(#{val.inspect})"
    else
      "#{fld.name}: #{val.inspect}"
    end
  end

  "<#{self.class.name} #{flds.join(", ")}>"
end

#to_hashObject


271
272
273
274
275
276
277
278
279
# File 'lib/beefcake.rb', line 271

def to_hash
  __beefcake_fields__.values.inject({}) do |h, fld|
    value = self[fld.name]
    unless value.nil?
      h[fld.name] = value
    end
    h
  end
end