Class: XMLRegistryObjects

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlregistry_objects.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reg, list) ⇒ XMLRegistryObjects

Returns a new instance of XMLRegistryObjects.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xmlregistry_objects.rb', line 16

def initialize(reg, list)

  patterns = [
    [:root, /(\w+)\s+=\s+\[([^\]]+)\]/, :object],  
      [:object, /([\w\?]+)\s+=\s+(.*)/, :attribute]
  ]

  lp = LineParser.new patterns
  r = lp.parse list

  @to_h = r.inject({}) do |rtn, row|

    name, path = row[1][:captures]
    class_name = name.capitalize
    klass = Object.const_set(class_name,Class.new)

    s = "
      def initialize(reg)
        @reg = reg
      end
    "

    s = if row[3].any? then

      row[3].inject(s) do |r, attr|

        attr_name, subkey = attr[1][:captures]
        r << make_def(path, subkey, attr_name)

      end

    else

      names = reg.xpath("#{path}/*/name()")
      
      names.inject(s) do |r, subkey| 
        r << make_def(path, subkey)
      end
    end

    klass.class_eval s
    rtn.merge name.to_sym => klass.new(reg)

  end
end

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



13
14
15
# File 'lib/xmlregistry_objects.rb', line 13

def to_h
  @to_h
end

Instance Method Details

#define_methodsObject



62
63
64
# File 'lib/xmlregistry_objects.rb', line 62

def define_methods()
  @to_h.each.map {|k,v| "define_method :#{k.to_s}, ->{h[:#{k}]}"}.join("\n")
end