Sha256: ee6179e9b36f4a223338f6149e072cb885e2eae03f21a2075a73288ed7e5db9d

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

module ActiveData
  module Model
    module Attributes
      module Reflections
        class Base
          attr_reader :name, :options
          class << self
            def build(_target, _generated_methods, name, *args, &block)
              options = args.extract_options!
              options[:type] = args.first if args.first
              options[:default] = block if block
              new(name, options)
            end

            def generate_methods(name, target) end

            def attribute_class
              @attribute_class ||= "ActiveData::Model::Attributes::#{name.demodulize}".constantize
            end
          end

          def initialize(name, options = {})
            @name = name.to_s
            @options = options
          end

          def build_attribute(owner, raw_value = ActiveData::UNDEFINED)
            attribute = self.class.attribute_class.new(name, owner)
            attribute.write_value(raw_value, origin: :persistence) unless raw_value == ActiveData::UNDEFINED
            attribute
          end

          def type
            @type ||= case options[:type]
            when Class, Module
              options[:type]
            when nil
              raise "Type is not specified for `#{name}`"
            else
              options[:type].to_s.camelize.constantize
            end
          end

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

          def readonly
            @readonly ||= options[:readonly]
          end

          def inspect_reflection
            "#{name}: #{type}"
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/attributes/reflections/base.rb
active_data-1.1.7 lib/active_data/model/attributes/reflections/base.rb
active_data-1.1.6 lib/active_data/model/attributes/reflections/base.rb
active_data-1.1.5 lib/active_data/model/attributes/reflections/base.rb
active_data-1.1.4 lib/active_data/model/attributes/reflections/base.rb
active_data-1.1.3 lib/active_data/model/attributes/reflections/base.rb