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) ⇒ 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
# File 'lib/xmlregistry_objects.rb', line 14

def initialize(reg, obj)
  
  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
      
  @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
        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

    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.



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

def to_h
  @to_h
end

Instance Method Details

#define_methodsObject



92
93
94
# File 'lib/xmlregistry_objects.rb', line 92

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


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xmlregistry_objects.rb', line 101

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