Class: PolyrexObjects

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrex-objects.rb

Defined Under Namespace

Classes: PolyrexObject

Instance Method Summary collapse

Constructor Details

#initialize(schema, id = '0', node = nil) ⇒ PolyrexObjects

Returns a new instance of PolyrexObjects.



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/polyrex-objects.rb', line 45

def initialize(schema, id='0', node=nil)

  @node = node
  @@id = id

  if schema then
    a = schema.split('/')
    a.shift
    @class_names = []

    a.each do |x|
      name, raw_fields = x.split('[')
      if raw_fields then
        fields = raw_fields.chop.split(',').map &:strip
        @class_names << name.capitalize

        classx = []  
        classx << "class #{name.capitalize} < PolyrexObject"
        classx << "def initialize(node=nil, id='0')"
        classx << "super(node,id)"

        classx << "a = node.xpath('summary/*',&:name)"
        classx << "yaml_fields = a - (#{fields}  + %w(format_mask))"
        classx << "yaml_fields.each do |field|"
        classx << %q(instance_eval "def #{field}; YAML.load(@node.element('summary/#{field}/text()')); end")
        classx << "end"
        
        classx << "@create = PolyrexCreateObject.new('#{schema}', @@id)"
        classx << "end"
        fields.each do |field|
          classx << "def #{field}"
          classx << "  if @node.element('summary/#{field}').nil? then"
          classx << "    @node.element('summary').add Rexle::Element.new('#{field}')"
          classx << "  end"
          classx << "  @node.element('summary/#{field}/text()')"
          classx << "end"
          classx << "def #{field}=(text)"
          classx << "  if @node.element('summary/#{field}').nil? then"
          classx << "    @node.element('summary').add Rexle::Element.new('#{field}', text)"
          classx << "  else"
          classx << "    @node.element('summary/#{field}').text = text"
          classx << "  end"
          classx << "end"
        end
        classx << "end"          

        eval classx.join("\n")
      end
    end

    if @class_names.length < 2 then
      make_def_records(@class_names.first)
    else
      # implement the child_object within each class object
      @class_names[0..-2].reverse.each_with_index do |class_name, k|    

        i = @class_names.length - (k + 1)
        make_def_records(class_name,i)
      end
    end      
    @class_names[1..-1].each_with_index do |class_name, k|    
      eval "#{class_name}.class_eval {        
        def parent()
          #{@class_names[k]}.new(@node.parent.parent, @@id)
        end                
      }"
    end
    
    methodx = @class_names.map do |name|
      %Q(def #{name.downcase}(); #{name}.new(@node, @@id); end)
    end
    
    self.instance_eval(methodx.join("\n"))
  end
end

Instance Method Details

#make_def_records(class_name, i = 0) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/polyrex-objects.rb', line 121

def make_def_records(class_name, i=0)

  eval "#{class_name}.class_eval { 
    def records()
      objects = @node.xpath('records/*').map {|record| #{@class_names[i]}.new(record, @@id)}

      def objects.records=(node); @node = node; end
      def objects.records(); @node; end

      def objects.sort_by!(&element_blk)
        a = @node.xpath('records/*').sort_by &element_blk
        records = @node.xpath('records')
        records.delete
        records = Rexle::Element.new 'records'
        a.each {|record| records.add record}
        @node.add records
        @node.xpath('records/*').map {|record| #{@class_names[i]}.new(record)}
      end

      objects.records = @node
      objects
    end        
            
    alias #{@class_names[i].downcase} records
    
  }"

end

#to_aObject



150
151
152
# File 'lib/polyrex-objects.rb', line 150

def to_a
  @class_names.map {|x| eval(x)}
end

#to_hObject



154
155
156
# File 'lib/polyrex-objects.rb', line 154

def to_h
  Hash[self.to_a.map {|x| [x.name[/\w+$/], x]}]
end