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, obj, log: nil) ⇒ XMLRegistryObjects

Returns a new instance of XMLRegistryObjects.



14
15
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/xmlregistry_objects.rb', line 14

def initialize(reg, obj, log: nil)

  @log = log
  log.info 'XMLRegistryObjects/initialize: active' if log
  
  polyrexdoc = if obj.is_a? Polyrex then 
  
    obj
  
  elsif obj.is_a? String then
    
    buffer, type = RXFHelper.read obj

    if type == :url then
      Polyrex.new buffer
    else
      Polyrex.new.import "<?polyrex schema='entries/object[name, regkey]/"\
          "methodx[name,subkeyname]' delimiter=' = '?>\n" + obj
    end
  end
      
  log.info 'XMLRegistryObjects/initialize: before @to_h' if log
  
  @to_h = polyrexdoc.records.inject({}) do |rtn, row|

    name, path = row.name, row.regkey[1..-2]
    class_name = name.capitalize
    klass = Object.const_set(class_name,Class.new)

    base_methods = "
      def initialize(reg)
        @reg = reg
      end
      
      def last_modified()
        lm = @reg.get_key('#{path}').attributes[:last_modified]
        Time.parse(lm) if lm
      end
      
      def set(h)          
        
        h.each {|key,value| self.method((key.to_s + '=').to_sym).call value }
        
      end
    "

    s = if row.records.any? then

      row.records.inject(base_methods) do |r, attr|
        
        attr_name, subkey = attr.name, attr.subkeyname[/\w+$/]
        key = path + '/' + subkey

        r << make_def(key, attr_name)
        r << make_setdef(key, attr_name[/\w+/])

      end

    else

      a = reg.xpath("#{path}/*")
      
      a.inject(base_methods) do |r, x| 

        methods_name = subkey = x.name

        type = x.attributes[:type]          
        key = path + '/' + subkey
        
        r << make_setdef(key, method_name=subkey)          

        method_name += '?' if type and type == 'boolean'          
        r << make_def(key, method_name)

      end
    end
    
    log.info 'XMLRegistryObjects/initialize: before class_eval ' + klass.to_s if log
    
    klass.class_eval s
    rtn.merge name.to_sym => klass.new(reg)

  end
  
  log.info 'XMLRegistryObjects/initialize: completed' if log
end

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



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

def to_h
  @to_h
end

Instance Method Details

#define_methodsObject



101
102
103
# File 'lib/xmlregistry_objects.rb', line 101

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

#define_methods2(obj = nil) ⇒ Object

to use this method the client is expect to fetch the hash object from the registry object e.g.

xro = XMLRegistryObjects.new(reg,list)
@h = xro.to_h


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/xmlregistry_objects.rb', line 110

def define_methods2(obj=nil)
  
  a = @to_h.each.map {|k,v| [k.to_s.to_sym, ->{@h[k.to_sym]} ] }
  
  if obj then
    
    a.each {|name, blk| obj.class.send(:define_method, name, blk) }
    
  else
    
    return a
    
  end
  
end