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



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

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

Instance Method Details

#==(o) ⇒ Object



318
319
320
321
322
# File 'lib/beefcake.rb', line 318

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



310
311
312
# File 'lib/beefcake.rb', line 310

def [](k)
  __send__(k)
end

#[]=(k, v) ⇒ Object



314
315
316
# File 'lib/beefcake.rb', line 314

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

#__beefcake_fields__Object



306
307
308
# File 'lib/beefcake.rb', line 306

def __beefcake_fields__
  self.class.fields
end

#assign(attrs) ⇒ Object

Handles filling a protobuf message from a hash. Embedded messages can be passed in two ways, by a pure hash or as an instance of embedded class(es).

Examples:

By a pure hash.

{:field1 => 2, :embedded => {:embedded_f1 => 'lala'}}

Repeated embedded message by a pure hash.

{:field1 => 2, :embedded => [
  {:embedded_f1 => 'lala'},
  {:embedded_f1 => 'lulu'}
]}

As an instance of embedded class.

{:field1 => 2, :embedded => EmbeddedMsg.new({:embedded_f1 => 'lala'})}

Parameters:

  • data (Hash)

    to fill a protobuf message with.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/beefcake.rb', line 265

def assign(attrs)
  __beefcake_fields__.values.each do |fld|
    attribute = attrs[fld.name]

    if attribute.nil?
      self[fld.name] = nil
      next
    end

    unless fld.is_protobuf?
      self[fld.name] = attribute
      next
    end

    if fld.repeated? && attribute.is_a?(Hash)
      self[fld.name] = fld.type.new(attribute)
      next
    end

    if fld.repeated? && attribute.is_a?(fld.type)
      self[fld.name] = [attribute]
      next
    end

    if fld.repeated?
      self[fld.name] = attribute.map do |i|
        fld.matches_type?(i) ? i : fld.type.new(i)
      end
      next
    end

    if fld.matches_type? attribute
      self[fld.name] = attribute
      next
    end

    self[fld.name] = fld.type.new(attribute)
  end
  self
end

#initialize(attrs = {}) ⇒ Object

Handles filling a protobuf message from a hash. Embedded messages can be passed in two ways, by a pure hash or as an instance of embedded class(es).

Examples:

By a pure hash.

{:field1 => 2, :embedded => {:embedded_f1 => 'lala'}}

Repeated embedded message by a pure hash.

{:field1 => 2, :embedded => [
  {:embedded_f1 => 'lala'},
  {:embedded_f1 => 'lulu'}
]}

As an instance of embedded class.

{:field1 => 2, :embedded => EmbeddedMsg.new({:embedded_f1 => 'lala'})}

Parameters:

  • data (Hash)

    to fill a protobuf message with.



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

def initialize(attrs={})
  assign attrs
end

#inspectObject



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/beefcake.rb', line 324

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/beefcake.rb', line 344

def to_hash
  __beefcake_fields__.values.inject({}) do |h, fld|
    v = self[fld.name]
    next h if v.nil?

    h[fld.name] =
      case
      when v.respond_to?(:to_hash)
        # A nested protobuf message, so let's call its 'to_hash' method.
        v.to_hash
      when v.is_a?(Array)
        # There can be two field types stored in array.
        # Primitive type or nested another protobuf message.
        # The later one has got a 'to_hash' method.
        v.map { |i| i.respond_to?(:to_hash) ? i.to_hash : i }
      else
        v
      end
    h
  end
end