Module: Beefcake::Message

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(o) ⇒ Object



186
187
188
189
190
# File 'lib/beefcake.rb', line 186

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

Instance Method Details

#==(o) ⇒ Object



210
211
212
213
# File 'lib/beefcake.rb', line 210

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

#[](k) ⇒ Object



202
203
204
# File 'lib/beefcake.rb', line 202

def [](k)
  __send__(k)
end

#[]=(k, v) ⇒ Object



206
207
208
# File 'lib/beefcake.rb', line 206

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

#fieldsObject



198
199
200
# File 'lib/beefcake.rb', line 198

def fields
  self.class.fields
end

#initialize(attrs = {}) ⇒ Object



192
193
194
195
196
# File 'lib/beefcake.rb', line 192

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

#inspectObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/beefcake.rb', line 215

def inspect
  set = 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



235
236
237
238
239
240
241
242
# File 'lib/beefcake.rb', line 235

def to_hash
  fields.values.inject({}) do |h, fld|
    if v = self[fld.name]
      h[fld.name] = v
    end
    h
  end
end