Module: Protobuf::Mongoid::AttributeMethods::ClassMethods

Defined in:
lib/protobuf/mongoid/attribute_methods.rb

Instance Method Summary collapse

Instance Method Details

#alias_field(field_alias, attribute) ⇒ Object



7
8
9
10
11
12
# File 'lib/protobuf/mongoid/attribute_methods.rb', line 7

def alias_field(field_alias, attribute)
  alias_attribute field_alias, attribute

  attribute_from_proto attribute, fetch_attribute_alias_from_proto(attribute, field_alias)
  field_from_document field_alias, fetch_field_alias_from_document(attribute, field_alias)
end

#fetch_attribute_alias_from_proto(attribute, field_alias) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/protobuf/mongoid/attribute_methods.rb', line 14

def fetch_attribute_alias_from_proto(attribute, field_alias)
  lambda do |proto|
    value = proto.__send__(:"#{field_alias}!")
    value ||= proto.__send__(:"#{attribute}!") if proto.respond_to?(attribute)

    self._protobuf_convert_fields_to_attributes(attribute, value)
  end
end

#fetch_field_alias_from_document(attribute, _field_aliasd) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/protobuf/mongoid/attribute_methods.rb', line 23

def fetch_field_alias_from_document(attribute, _field_aliasd)
  lambda do |document|
    value = document.__send__(field_alias)

    self._protobuf_convert_attributes_to_fields(attribute, value)
  end
end