Sha256: 2692a568343afa911e8c83dab132c5e939a8621cea94f56a7f06cb17e1e204e4

Contents?: true

Size: 1.06 KB

Versions: 33

Compression:

Stored size: 1.06 KB

Contents

require 'active_support/concern'

module Protobuf
  module ActiveRecord
    module AttributeMethods
      extend ActiveSupport::Concern

      module ClassMethods
        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_record field_alias, fetch_field_alias_from_record(attribute, field_alias)
        end

        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

        def fetch_field_alias_from_record(attribute, field_aliasd)
          lambda do |record|
            value = record.__send__(field_alias)

            self._protobuf_convert_attributes_to_fields(attribute, value)
          end
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
protobuf-activerecord-3.6.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.5.1 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.5.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.4 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.4.pre lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.3 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.2 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.1 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.4.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.8 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.7 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.6 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.5 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.4 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.3 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.2 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.1 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.3.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.2.2 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.2.1 lib/protobuf/active_record/attribute_methods.rb