Module: GEXF::Attribute::Assignable
Class Method Summary collapse
-
.included(base) ⇒ Object
Delegates calls for the ‘defined_attributes’ method to the @collection instance variable.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetches an attribute by title.
- #[]=(key, value) ⇒ Object
-
#attr_values ⇒ Object
Fetches the attribute hash, and sets it to an empty array if this is not defined.
-
#attributes ⇒ Object
Reconstructs a hash of attiribute titles/values for the current node/edge.
-
#set_attr_by_id(attr_id, value) ⇒ Object
low level setter, suitable to be used when parsing (see GEXF::Document).
Class Method Details
.included(base) ⇒ Object
Delegates calls for the ‘defined_attributes’ method to the @collection instance variable
Returns nothing.
7 8 9 10 11 12 |
# File 'lib/gexf/attribute/assignable.rb', line 7 def self.included(base) base.extend(Forwardable) unless base.ancestors.include?(Forwardable) base.def_delegator :collection, :attribute_definitions, :defined_attributes base.def_delegator :attr_values, :[], :attr_value end |
Instance Method Details
#[](key) ⇒ Object
Fetches an attribute by title.
Returns the attribute value. Raises a warning if the attribute has not been defined..
48 49 50 51 52 53 54 55 |
# File 'lib/gexf/attribute/assignable.rb', line 48 def [](key) if attr = attribute_by_title(key) value = @attr_values[attr.id] !value.nil? ? value : attr.default else Kernel.warn "undefined attribute '#{key}'" end end |
#[]=(key, value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gexf/attribute/assignable.rb', line 62 def []=(key, value) attr = attribute_by_title(key) value = attr && attr.coherce(value) || value if attr && attr.is_valid?(value) attr_values[attr.id] = value else Kernel.warn "undefined attribute '#{key}'" end end |
#attr_values ⇒ Object
Fetches the attribute hash, and sets it to an empty array if this is not defined.
Returns the ‘attr_values’ hash.
37 38 39 |
# File 'lib/gexf/attribute/assignable.rb', line 37 def attr_values @attr_values ||= {} end |
#attributes ⇒ Object
Reconstructs a hash of attiribute titles/values for the current node/edge
Example:
node.attributes
=> {:site => 'http://www.archive.org', :name => 'The internet archive'}
Returns
The attributes hash
25 26 27 28 29 |
# File 'lib/gexf/attribute/assignable.rb', line 25 def attributes() Hash[*defined_attributes.map do |_, attr| [attr.title, attr_value(attr.id)] end.flatten] end |
#set_attr_by_id(attr_id, value) ⇒ Object
low level setter, suitable to be used when parsing (see GEXF::Document)
58 59 60 |
# File 'lib/gexf/attribute/assignable.rb', line 58 def set_attr_by_id(attr_id, value) @attr_values[attr_id] = value end |