Class: PolyrexCreateObject

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, id = '1') ⇒ PolyrexCreateObject

Returns a new instance of PolyrexCreateObject.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/polyrex-createobject.rb', line 13

def initialize(schema, id='1')
  @@id = id

  @schema = schema

  if @schema then
    a = @schema.split('/')        
    @rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }

    values = @rpaths.values.reverse

    @rpaths.keys.reverse.each.with_index do |key,i |
      @rpaths[key] = values[i..-1].join('/')
    end

    names = @rpaths.to_a[0..-2].map {|k,v| [v[/[^\[]+/], k]}
    
    attach_create_handlers(names)
  end
end

Instance Attribute Details

#parent_nodeObject

Returns the value of attribute parent_node.



11
12
13
# File 'lib/polyrex-createobject.rb', line 11

def parent_node
  @parent_node
end

Instance Method Details

#attach_create_handlers(names) ⇒ Object



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
# File 'lib/polyrex-createobject.rb', line 41

def attach_create_handlers(names)
  methodx = names[0..-2].map do |name, xpath|

# nested records
%Q(
def #{name}(params={}, id=nil,&blk) 

  id ||= @@id
  orig_parent = @parent_node
  new_parent = create_node(@parent_node, @rpaths['#{xpath}'], params, id).element('records')

  if block_given? then
    self.record = new_parent
    blk.call(self) 
  end

  self.record = orig_parent

  self
end
)
  end

  name, xpath = names[-1]

# top record    
  methodx << %Q(
def #{name}(params={}, id=nil,&blk)
id ||= @@id
orig_parent = @parent_node
self.record = @parent_node.element('records') unless @parent_node.name == 'records'
self.record = create_node(@parent_node, @rpaths['#{xpath}'], params, id).element('records')        
blk.call(self) if block_given?
self.record = orig_parent

self
end
)

  self.instance_eval(methodx.join("\n"))
  
end

#create_node(parent_node, child_schema, params = {}, id = nil) ⇒ Object



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
# File 'lib/polyrex-createobject.rb', line 84

def create_node(parent_node, child_schema, params={}, id=nil)

  #puts 'create_node... child_schema : ' + child_schema
  record = Rexle.new PolyrexSchema.new(child_schema).to_s
  
  #puts '@parent_node class: %s; xml: %s : ' % [@parent_node.class, @parent_node.xml]

  if id then
    @@id.succ!
  else
    if @@id.to_i.to_s == @@id.to_s then
      @@id.succ!
    else
      @@id = @parent_node.element('count(//@id)').to_i + 2
    end
  end

  record.root.add_attribute({'id' => @@id.to_s.clone})

  a = child_schema[/[^\[]+(?=\])/].split(',')
  #puts 'record : ' + record.xml
  summary = record.root.element('summary')
  a.each do |field_name|  
    field = summary.element(field_name.strip)
    field.text = params[field_name.strip.to_sym]
  end

  parent_node.add record.root

end

#idObject



35
# File 'lib/polyrex-createobject.rb', line 35

def id() @@id end

#id=(s) ⇒ Object



34
# File 'lib/polyrex-createobject.rb', line 34

def id=(s)  @@id = s; self end

#record=(node) ⇒ Object



37
38
39
# File 'lib/polyrex-createobject.rb', line 37

def record=(node)
  @parent_node = node
end

#xpath_to_rpath(xpath) ⇒ Object



116
117
118
# File 'lib/polyrex-createobject.rb', line 116

def xpath_to_rpath(xpath)
  xpath.split('/').each_slice(2).map(&:last).join('/').gsub(/\[[^\]]+\]/,'')
end