Sha256: f5ece3cda5c5517fd571e127ee6086d71e753cf9b5e64c0104d9a615b23393f0

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 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)
            options[:of] = reference.name if reference
            validates_nested = target.respond_to?(:validates_nested) && !target.validates_nested?(options[:of])
            target.validates_nested(options[:of]) if validates_nested

            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

9 entries across 9 versions & 1 rubygems

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