Sha256: 8d6edd0709e30c12e537352c6b5408c745688d71bb457a5b3ccb3a67a3e34e49

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module ActiveData
  module Model
    module Attributes
      class Represents < Attribute
        delegate :reader, :reader_before_type_cast, :writer, to: :reflection

        def write value
          return if readonly?
          pollute do
            reset
            reference.send(writer, value)
          end
        end

        def reset
          super
          remove_variable(:cached_value, :cached_value_before_type_cast)
        end

        def read
          reset if cached_value != read_value
          variable_cache(:value) do
            normalize(enumerize(defaultize(cached_value, read_before_type_cast)))
          end
        end

        def read_before_type_cast
          reset if cached_value_before_type_cast != read_value_before_type_cast
          variable_cache(:value_before_type_cast) do
            defaultize(cached_value_before_type_cast)
          end
        end

      private

        def reference
          owner.send(reflection.reference)
        end

        def read_value
          ref = reference
          ref.public_send(reader) if ref
        end

        def cached_value
          variable_cache(:cached_value) { read_value }
        end

        def read_value_before_type_cast
          ref = reference
          if ref
            ref.respond_to?(reader_before_type_cast) ?
              ref.public_send(reader_before_type_cast) :
              ref.public_send(reader)
          end
        end

        def cached_value_before_type_cast
          variable_cache(:cached_value_before_type_cast) { read_value_before_type_cast }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_data-1.0.0 lib/active_data/model/attributes/represents.rb