module DocumentBuilder::Model
Public Class Methods
included(base)
click to toggle source
# File lib/document_builder/model.rb, line 33 def self.included(base) base.extend(ClassMethods) base.class_eval do include Coercion end end
new(document)
click to toggle source
# File lib/document_builder/model.rb, line 40 def initialize(document) @document = document end
Public Instance Methods
[](key)
click to toggle source
# File lib/document_builder/model.rb, line 72 def [](key) get_attribute(key) end
add_attribute(name, attribute)
click to toggle source
# File lib/document_builder/model.rb, line 68 def add_attribute(name, attribute) self.class.add_attribute(name, attribute) end
attributes()
click to toggle source
# File lib/document_builder/model.rb, line 56 def attributes self.class.attributes end
document()
click to toggle source
# File lib/document_builder/model.rb, line 44 def document if root && @document.name != root @document.at_xpath(root) else @document end end
get_attribute(name)
click to toggle source
# File lib/document_builder/model.rb, line 60 def get_attribute(name) if respond_to?(name) public_send(name) else attributes[name].call(document) end end
inspect()
click to toggle source
# File lib/document_builder/model.rb, line 86 def inspect "#<#{self.class}:0x#{self.object_id.to_s(16)}> Attributes: " + JSON.pretty_generate(to_hash) end
method_missing(name, *args)
click to toggle source
# File lib/document_builder/model.rb, line 76 def method_missing(name, *args) get_attribute(name) rescue NoMethodError => e raise NoMethodError.new("undefined method '#{name}' for #{self.class}") end
root()
click to toggle source
# File lib/document_builder/model.rb, line 52 def root self.class.instance_variable_get(:@root) end
to_hash()
click to toggle source
# File lib/document_builder/model.rb, line 94 def to_hash attributes.inject({}) do |acc, (key, value)| obj = get_attribute(key) acc[key] = obj.respond_to?(:to_hash) ? obj.to_hash : obj acc end end
to_json(*args)
click to toggle source
# File lib/document_builder/model.rb, line 90 def to_json(*args) JSON.generate(to_hash) end
to_s(*args)
click to toggle source
# File lib/document_builder/model.rb, line 82 def to_s(*args) JSON.pretty_generate(to_hash) end