Class: Ahora::Representation

Inherits:
Nibbler
  • Object
show all
Extended by:
Definition
Defined in:
lib/ahora/representation.rb

Defined Under Namespace

Modules: Definition

Constant Summary collapse

INTEGER_PARSER =
lambda { |node| Integer(node.content) if node.content.present? }
FLOAT_PARSER =
lambda { |node| Float(node.content) if node.content.present? }
DATE_PARSER =
lambda { |node| Date.parse(node.content) if node.content.present? }
TIME_PARSER =
lambda { |node| Time.parse(node.content) if node.content.present? }
BOOL_PARSER =
lambda { |node| node.content.to_s.downcase == 'true' if node.content.present? }

Instance Method Summary collapse

Methods included from Definition

attribute, boolean, date, element, float, integer, string, time

Constructor Details

#initialize(doc_or_atts = {}) ⇒ Representation

Returns a new instance of Representation.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ahora/representation.rb', line 72

def initialize(doc_or_atts = {})
  if doc_or_atts.is_a? Hash
    super(nil)
    doc_or_atts.each do |key, val|
      send("#{key}=", val)
    end
  else
    doc = doc_or_atts
    doc = XmlParser.parse(doc) unless doc.respond_to?(:search)
    if doc.node_type == Nokogiri::XML::Node::DOCUMENT_NODE
      # immediately scope to root element
      doc = doc.at('/*')
    end
    super(doc)
  end
end