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
33
34
# 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({}) do |r| 
      r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) 
    end

    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



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

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



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

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


  buffer = PolyrexSchema.new(child_schema[/^[^\/]+/]).to_s
  record = Rexle.new buffer     

  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(',')

  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



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

def id() @@id end

#id=(s) ⇒ Object



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

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

#record=(node) ⇒ Object



39
40
41
# File 'lib/polyrex-createobject.rb', line 39

def record=(node)
  @parent_node = node
end

#xpath_to_rpath(xpath) ⇒ Object



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

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