Class: Mixture::AttributeList

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/mixture/attribute_list.rb

Overview

A list of attributes. This is used instead of a hash in order to add in the #options and #callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ AttributeList

Initializes the attribute list.

Parameters:



71
72
73
74
75
76
# File 'lib/mixture/attribute_list.rb', line 71

def initialize(parent = nil)
  @list = {}
  @options = {}
  @parent = parent
  @callbacks = Hash.new { |h, k| h[k] = Set.new }
end

Instance Attribute Details

#callbacksHash{Symbol => Array<Proc>} (readonly)

Returns a set of callbacks. This is used for coercion, but may be used for other things.

Returns:

  • (Hash{Symbol => Array<Proc>})


58
59
60
# File 'lib/mixture/attribute_list.rb', line 58

def callbacks
  @callbacks
end

#optionsHash{Symbol => Object} (readonly)

Returns a set of options used for the attributes. This isn't used at the moment.

Returns:

  • (Hash{Symbol => Object})


52
53
54
# File 'lib/mixture/attribute_list.rb', line 52

def options
  @options
end

#parentAttributeList?

The parent of this attribute list. This is "merged" into this attribute list. This shouldn't be set since it is automatically assumed; however, sometimes it can be assumed wrong.

Returns:



65
66
67
# File 'lib/mixture/attribute_list.rb', line 65

def parent
  @parent
end

Instance Method Details

#[](key) ⇒ Attribute

Looks up the given key. If the current attribute list does not have the key, it passes it up to the parent, if there is one. Otherwise, it returns the default value for the list (nil).

Parameters:

  • key (::Symbol)

Returns:



46
# File 'lib/mixture/attribute_list.rb', line 46

delegate [:fetch, :[], :key?, :keys, :values, :each, :has_key?] => :with_parent

#create(name, options = {}) ⇒ Attribute

Creates a new attribute with the given name and options.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

    The options for the attribute.

Returns:



83
84
85
# File 'lib/mixture/attribute_list.rb', line 83

def create(name, options = {})
  @list[name] = Attribute.new(name, self, options)
end

#each(&block) ⇒ void

This method returns an undefined value.

Iterates over the attribute list. If there is a parent, the current attribute list is merged into the parent, then iterated over; otherwise, it iterates over the current.



46
# File 'lib/mixture/attribute_list.rb', line 46

delegate [:fetch, :[], :key?, :keys, :values, :each, :has_key?] => :with_parent

#fetch(key, value = Undefined, &block) ⇒ Attribute

Fetches the given key from the attribute list. If the current attribute list does not have the key, it passes it up to the parent, if there is one. If there is no parent, it behaves as normal.

Parameters:

  • key (::Symbol)
  • value (::Object) (defaults to: Undefined)

Returns:



46
# File 'lib/mixture/attribute_list.rb', line 46

delegate [:fetch, :[], :key?, :keys, :values, :each, :has_key?] => :with_parent

#key?(key) ⇒ ::Boolean

Returns if the attribute exists. If it doesn't exist on this list, it passes it up to the parent.

Parameters:

  • key (::Symbol)

Returns:

  • (::Boolean)


46
# File 'lib/mixture/attribute_list.rb', line 46

delegate [:fetch, :[], :key?, :keys, :values, :each, :has_key?] => :with_parent