Sha256: 9d994f734df3e296f6887ba3259710bd74ce1f4293ec8d015c54abce9dc87290

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module ActiveData
  module Model
    module Attributes
      module Reflections
        class Represents < Attribute
          def self.build target, generated_methods, name, *args, &block
            options = args.extract_options!

            reference = target.reflect_on_association(options[:of]) if target.respond_to?(:reflect_on_association)
            reference ||= target.reflect_on_attribute(options[:of]) if target.respond_to?(:reflect_on_attribute)
            if reference
              options[:of] = reference.name
              target.validates_nested options[:of]
            end

            super(target, generated_methods, name, *args, options, &block)
          end

          def initialize name, options
            super
            raise ArgumentError, "Undefined reference for `#{name}`" if reference.blank?
          end

          def type
            Object
          end

          def reference
            @reference ||= options[:of].to_s
          end

          def column
            @column ||= options[:column].presence.try(:to_s) || name
          end

          def reader
            @reader ||= options[:reader].presence.try(:to_s) || column
          end

          def reader_before_type_cast
            @reader_before_type_cast ||= "#{reader}_before_type_cast"
          end

          def writer
            @writer ||= "#{options[:writer].presence || column}="
          end

          def inspect_reflection
            "#{name}: (represents)"
          end
        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/reflections/represents.rb