Sha256: 2d9ab3bf80a39ab78c2157fcf04ef346fe505a7652033488cadcb2ef11aa1e00

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

module Toy
  class Attribute
    attr_reader :model, :name, :type, :options

    def initialize(model, name, type, options={})
      options.assert_valid_keys(:default, :virtual, :abbr)

      @model, @name, @type, @options = model, name.to_s, type, options
      @virtual = options.fetch(:virtual, false)

      if abbr?
        options[:abbr] = abbr.to_s
        model.alias_attribute(abbr, name)
      end

      model.attributes[name.to_s] = self
    end

    def from_store(value)
      value = default if default? && value.nil?
      type.from_store(value, self)
    end

    def to_store(value)
      value = default if default? && value.nil?
      type.to_store(value, self)
    end

    def default
      if options.key?(:default)
        default = options[:default]
        if default.respond_to?(:call)
          backwards_compatible_call(default)
        else
          default
        end
      else
        type.respond_to?(:store_default) ? type.store_default : nil
      end
    end

    def default?
      options.key?(:default) || type.respond_to?(:store_default)
    end

    def virtual?
      @virtual
    end

    def persisted?
      !virtual?
    end

    def abbr?
      options.key?(:abbr)
    end

    def abbr
      options[:abbr]
    end

    def persisted_name
      abbr? ? abbr : name
    end

    def eql?(other)
      self.class.eql?(other.class) &&
        model == other.model &&
        name  == other.name
    end
    alias :== :eql?

    private

    def backwards_compatible_call(block)
      if block.arity == 1
        block.call(model)
      else
        block.call
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
toystore-0.13.2 lib/toy/attribute.rb
toystore-0.13.1 lib/toy/attribute.rb
toystore-0.13.0 lib/toy/attribute.rb
toystore-0.12.0 lib/toy/attribute.rb
toystore-0.11.0 lib/toy/attribute.rb
toystore-0.10.4 lib/toy/attribute.rb
toystore-0.10.3 lib/toy/attribute.rb
toystore-0.10.2 lib/toy/attribute.rb
toystore-0.10.1 lib/toy/attribute.rb
toystore-0.10.0 lib/toy/attribute.rb