Class: Mixture::AttributeList
- Inherits:
-
Object
- Object
- Mixture::AttributeList
- 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
-
#callbacks ⇒ Hash{Symbol => Array<Proc>}
readonly
Returns a set of callbacks.
-
#options ⇒ Hash{Symbol => Object}
readonly
Returns a set of options used for the attributes.
-
#parent ⇒ AttributeList?
The parent of this attribute list.
Instance Method Summary collapse
-
#[](key) ⇒ Attribute
Looks up the given key.
-
#create(name, options = {}) ⇒ Attribute
Creates a new attribute with the given name and options.
-
#each(&block) ⇒ void
Iterates over the attribute list.
-
#fetch(key, value = Undefined, &block) ⇒ Attribute
Fetches the given key from the attribute list.
-
#initialize(parent = nil) ⇒ AttributeList
constructor
Initializes the attribute list.
-
#key?(key) ⇒ ::Boolean
Returns if the attribute exists.
Constructor Details
#initialize(parent = nil) ⇒ AttributeList
Initializes the attribute list.
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
#callbacks ⇒ Hash{Symbol => Array<Proc>} (readonly)
Returns a set of callbacks. This is used for coercion, but may be used for other things.
58 59 60 |
# File 'lib/mixture/attribute_list.rb', line 58 def callbacks @callbacks end |
#options ⇒ Hash{Symbol => Object} (readonly)
Returns a set of options used for the attributes. This isn't used at the moment.
52 53 54 |
# File 'lib/mixture/attribute_list.rb', line 52 def @options end |
#parent ⇒ AttributeList?
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.
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
).
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.
83 84 85 |
# File 'lib/mixture/attribute_list.rb', line 83 def create(name, = {}) @list[name] = Attribute.new(name, self, ) 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.
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.
46 |
# File 'lib/mixture/attribute_list.rb', line 46 delegate [:fetch, :[], :key?, :keys, :values, :each, :has_key?] => :with_parent |