Class: ExistDB::Dom::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Convertible
Defined in:
lib/existdb/dom/active_record.rb

Overview

Inspired by Rails ActiveRecord Because we cannot make assumptions about the structure of XML this must inspect the dom when descending into each node. For large well structured documents a mapper pattern where the structure is defined in the class would be more efficient.

Defined Under Namespace

Modules: Convertible, NodeArray

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/existdb/dom/active_record.rb', line 49

def initialize(resource)
    @dom = resource.respond_to?(:dom) ? resource.dom : resource
    @children = Hash.new
    @raw = Hash.new
    @dom.getChildNodes.each do |child|
        name = child.getNodeName.to_s.to_sym
        next if respond_to?(name)
        @raw[name] ||= Array.new
        @raw[name] << child
    end

    @raw.each_key do |name|
        if @raw[name].size > 1 then
            @raw[name].extend(NodeArray, Convertible)
            self.instance_eval %{
                def #{name}
                    @raw[#{name.inspect}]
                end
            }
        else
            @raw[name] = @raw[name].first
            self.instance_eval %{
                def #{name}
                    convert( @raw[#{name.inspect}] )
                end
            }
        end
    end

end