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



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
120
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
149
150
151
# File 'lib/polyrex-objects.rb', line 70

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 << "  node ||= Rexle.new('<#{name}><summary/><records/></#{name}>').root"
        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 << "@fields = %i(#{fields.join(' ')})"          
        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()').clone"
          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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/polyrex-objects.rb', line 153

def make_def_records(class_name, i=0)

  eval "#{class_name}.class_eval { 
    def records()

      classes = {#{@class_names.map{|x| %Q(%s: %s) % [x[/[^:]+$/].downcase,x]}.join(',')} }

      objects = @node.xpath('records/*').map do |record| 
        classes[record.name.to_sym].new(record, @@id)
      end

      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

      def objects.remove_all()
        e = @node.element('records')
        e.insert_before Rexle::Element.new('records')
        e.delete
      end

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

end

#to_aObject



194
195
196
# File 'lib/polyrex-objects.rb', line 194

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

#to_hObject



198
199
200
# File 'lib/polyrex-objects.rb', line 198

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