Class: Graft::Xml::Attribute
- Inherits:
-
Object
- Object
- Graft::Xml::Attribute
- Defined in:
- lib/graft/lib/graft/xml/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
TODO: Refactor the location / attribute logic into a Source class.
-
#sources ⇒ Object
readonly
TODO: Refactor the location / attribute logic into a Source class.
Instance Method Summary collapse
- #attribute(source) ⇒ Object
-
#initialize(name, type = :string, sources = nil) ⇒ Attribute
constructor
A new instance of Attribute.
- #location(source) ⇒ Object
- #node_for(document, source) ⇒ Object
- #split(source) ⇒ Object
- #type_class ⇒ Object
- #value_from(document) ⇒ Object
Constructor Details
#initialize(name, type = :string, sources = nil) ⇒ Attribute
Returns a new instance of Attribute.
9 10 11 12 13 14 15 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 9 def initialize(name, type = :string, sources = nil) @name = name.to_sym @type = type @sources = Array(sources) @sources << @name.to_s if @sources.empty? end |
Instance Attribute Details
#name ⇒ Object (readonly)
TODO: Refactor the location / attribute logic into a Source class
7 8 9 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 7 def name @name end |
#sources ⇒ Object (readonly)
TODO: Refactor the location / attribute logic into a Source class
7 8 9 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 7 def sources @sources end |
Instance Method Details
#attribute(source) ⇒ Object
32 33 34 35 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 32 def attribute(source) location, attribute = source.split('@') attribute || location end |
#location(source) ⇒ Object
37 38 39 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 37 def location(source) split(source).first end |
#node_for(document, source) ⇒ Object
28 29 30 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 28 def node_for(document, source) document.at(location(source)) || document.search("//[@#{attribute(source)}]").first end |
#split(source) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 21 def split(source) location, attribute = source.split('@') location = self.name.to_s if location.blank? [location, attribute] end |
#type_class ⇒ Object
17 18 19 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 17 def type_class "Graft::Xml::Type::#{@type.to_s.camelize}".constantize end |
#value_from(document) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graft/lib/graft/xml/attribute.rb', line 41 def value_from(document) values = sources.map do |source| node = node_for(document, source) if !node.nil? possible_values = [node.attributes[attribute(source)], node.inner_text] possible_values.detect {|value| !value.blank? } end end type_class.new(values.compact.first).value end |