Sha256: 88c92741e2d9269b8b24935b39d6239d94c367a466562f2ca8103f25e571c11b

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

# encoding: utf-8

require "forwardable"

module Mixture
  # A list of attributes.  This is used instead of a hash in order to
  # add in the {#options} and {#callbacks}.
  class AttributeList
    extend Forwardable
    # If it looks like a duck...
    include Enumerable
    # If it quacks like a duck...
    include Comparable
    # Then it must be a duck.
    delegate [:fetch, :[], :[]=, :key?, :each, :<=>] => :@list

    # Returns a set of options used for the attributes.  This isn't
    # used at the moment.
    #
    # @return [Hash{Symbol => Object}]
    attr_reader :options

    # Returns a set of callbacks.  This is used for coercion, but may
    # be used for other things.
    #
    # @return [Hash{Symbol => Array<Proc>}]
    attr_reader :callbacks

    # Initializes the attribute list.
    def initialize
      @list = {}
      @options = {}
      @callbacks = Hash.new { |h, k| h[k] = [] }
    end

    # Creates a new attribute with the given name and options.
    #
    # @param name [Symbol] The name of the attribute.
    # @param options [Hash] The options for the attribute.
    # @return [Attribute]
    def create(name, options = {})
      @list[name] = Attribute.new(name, self, options)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mixture-0.3.2 lib/mixture/attribute_list.rb
mixture-0.3.1 lib/mixture/attribute_list.rb
mixture-0.3.0 lib/mixture/attribute_list.rb
mixture-0.2.0 lib/mixture/attribute_list.rb