Sha256: d88fbbe1e388c20f734cbef4bd78dd5e54a17b4fcc07a4727141101d27cb7ac2

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

class Anima

  # An attribute
  class Attribute
    include Adamantium::Flat, Equalizer.new(:name)

    # Return attribute name
    #
    # @return [Symbol]
    #
    # @api private
    #
    attr_reader :name
    
    # Load attribute
    #
    # @param [Object] object
    # @param [Hash] attributes
    #
    # @return [self]
    #
    # @api private
    #
    def load(object, attributes)
      attribute_name = name

      value = attributes.fetch(attribute_name) do 
        raise Error::Missing.new(object.class, attribute_name)
      end

      set(object, value)
    end

    # Get attribute value from object
    #
    # @param [Object] object
    #
    # @return [Object] 
    #
    # @api private
    #
    def get(object)
      object.public_send(name)
    end

    # Set attribute value in object
    #
    # @param [Object] object
    # @param [Object] value
    #
    # @return [self]
    #
    # @api private
    #
    def set(object, value)
      object.instance_variable_set(instance_variable_name, value)

      self
    end

    # Return instance variable name
    #
    # @return [Symbol]
    #   returns @ prefixed name
    #
    # @api private
    #
    def instance_variable_name
      :"@#{name}"
    end
    memoize :instance_variable_name

    # Define reader
    #
    # @param [Class, Module] scope
    #
    # @return [self]
    #
    # @api private
    #
    def define_reader(scope)
      scope.send(:attr_reader, name)
      self
    end

  private


    # Initialize attribute
    #
    # @param [Symbol] name
    #
    # @api private
    #
    def initialize(name)
      @name = name
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anima-0.0.7 lib/anima/attribute.rb
anima-0.0.6 lib/anima/attribute.rb
anima-0.0.5 lib/anima/attribute.rb
anima-0.0.4 lib/anima/attribute.rb
anima-0.0.3 lib/anima/attribute.rb