Sha256: 49d7bb403bb85b4b8d9a1839216380a16845580341271db2294b2c51a35d6584

Contents?: true

Size: 1.67 KB

Versions: 12

Compression:

Stored size: 1.67 KB

Contents

# encoding: utf-8

module Mixture
  # An attribute for a mixture object.
  class Attribute
    # The name of the attribute.
    #
    # @return [Symbol]
    attr_reader :name

    # The options for the attribute.  This is mainly used for coercion
    # and validation.
    #
    # @return [Hash]
    attr_reader :options

    # Initialize the attribute.
    #
    # @param name [Symbol] The name of the attribute.
    # @param list [AttributeList] The attribute list this attribute is
    #   a part of.
    # @param options [Hash] The optiosn for the attribute.
    def initialize(name, list, options = {})
      @name = name
      @list = list
      @options = options
    end

    # Update the attribute with the given value.  It runs the value
    # through the callbacks, and returns a new value given by the
    # callbacks.
    #
    # @param value [Object] The new value.
    # @return [Object] The new new value.
    def update(value)
      @list.callbacks[:update].inject(value) { |a, e| e.call(self, a) }
    end

    # @!attribute [r] ivar
    # The instance variable for this attribute.
    #
    # @example Retrieve the instance variable.
    #   attribute.ivar # => :@name
    # @return [Symbol]
    def ivar
      @_ivar ||= :"@#{@name}"
    end

    # @!attribute [r] getter
    # The getter method for this attribute.
    #
    # @example Retrieve the getter.
    #   attribute.getter # => :name
    # @return [Symbol]
    def getter
      @name
    end

    # @!attribute [r] setter
    # The setter method for this attribute.
    #
    # @example Retrieve the setter.
    #   attribute.setter # :name=
    # @return [Symbol]
    def setter
      @_setter ||= :"#{@name}="
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mixture-0.6.2 lib/mixture/attribute.rb
mixture-0.6.1 lib/mixture/attribute.rb
mixture-0.6.0 lib/mixture/attribute.rb
mixture-0.5.1 lib/mixture/attribute.rb
mixture-0.5.0 lib/mixture/attribute.rb
mixture-0.4.0 lib/mixture/attribute.rb
mixture-0.3.6 lib/mixture/attribute.rb
mixture-0.3.5 lib/mixture/attribute.rb
mixture-0.3.4 lib/mixture/attribute.rb
mixture-0.3.2 lib/mixture/attribute.rb
mixture-0.3.1 lib/mixture/attribute.rb
mixture-0.3.0 lib/mixture/attribute.rb