Sha256: 0f76493689ebd96229d1848f9dfba3da0b2916821301afd5eefe4efac4857a1f

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module ActiveData
  module Model
    module Attributes
      class ReferenceOne < Base
        def write(value)
          pollute do
            previous = type_casted_value
            result = write_value value
            changed = (!value.nil? && type_casted_value.nil?) || type_casted_value != previous

            association.reset if changed
            result
          end
        end

        def read
          if association.target
            association.identify
          else
            type_casted_value
          end
        end

        def type_casted_value
          variable_cache(:value) do
            typecast(read_before_type_cast)
          end
        end

        def read_before_type_cast
          @value_cache
        end

        def type
          @type ||= association.reflection.persistence_adapter.primary_key_type
        end

        def typecaster
          @typecaster ||= ActiveData.typecaster(type.ancestors.grep(Class))
        end

      private

        def association
          @association ||= owner.association(reflection.association)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.7 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.6 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.5 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.4 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.3 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.2 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.1 lib/active_data/model/attributes/reference_one.rb
active_data-1.1.0 lib/active_data/model/attributes/reference_one.rb