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.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/polyrex-createobject.rb', line 12

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

  @schema = schema
  a = @schema.split('/')        

  @rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }
  names = @rpaths.to_a[0..-2].map {|k,v| [v[/[^\[]+/], k]}
  
  attach_create_handlers(names)

end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#parent_nodeObject

Returns the value of attribute parent_node.



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

def parent_node
  @parent_node
end

Instance Method Details

#attach_create_handlers(names) ⇒ Object



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

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

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

  create_node(@parent_node, @rpaths['#{xpath}'], params, id).element('records')
  blk.call(self) if block_given?

  self
end
)
  end

  name, xpath = names[-1]
  
  methodx << %Q(
def #{name}(params={}, id=nil,&blk)
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
end
)

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

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/polyrex-createobject.rb', line 59

def create_node(parent_node, child_schema, params={}, id=nil)
  record = Rexle.new PolyrexSchema.new(child_schema).to_s
  @id = id if id

  record.root.add_attribute({'id' => @id.to_s.clone})
  if @id.to_s[/[0-9]/] then
    @id.succ!
  else
    @id = @parent_node.element('count(//@id)').to_i + 2
  end
  
  a = child_schema[/[^\[]+(?=\])/].split(',')
  a.each do |field_name|  
    field = record.element('summary/' + field_name)
    field.text = params[field_name.to_sym]
  end

  parent_node.add record.root

end

#record=(node) ⇒ Object



25
26
27
# File 'lib/polyrex-createobject.rb', line 25

def record=(node)
  @parent_node = node
end

#xpath_to_rpath(xpath) ⇒ Object



81
82
83
# File 'lib/polyrex-createobject.rb', line 81

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