Sha256: 2a176528c764d836e4e2b00021d8c94b22c3ad7d5b94bc7493ce0664a49d0ab9

Contents?: true

Size: 1.06 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
protobuf-activerecord-6.1.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-6.0.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-5.2.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-5.1.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-5.0.0 lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-5.1.0.beta lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-5.0.0.beta lib/protobuf/active_record/attribute_methods.rb
protobuf-activerecord-3.6.1 lib/protobuf/active_record/attribute_methods.rb