Class: PolyrexCreateObject
- Inherits:
-
Object
- Object
- PolyrexCreateObject
- Defined in:
- lib/polyrex-createobject.rb
Instance Attribute Summary collapse
-
#parent_node ⇒ Object
Returns the value of attribute parent_node.
Instance Method Summary collapse
- #attach_create_handlers(names) ⇒ Object
- #create_node(parent_node, child_schema, params = {}, id = nil) ⇒ Object
- #id ⇒ Object
- #id=(s) ⇒ Object
-
#initialize(schema, id = '1') ⇒ PolyrexCreateObject
constructor
A new instance of PolyrexCreateObject.
- #record=(node) ⇒ Object
- #xpath_to_rpath(xpath) ⇒ Object
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
#parent_node ⇒ Object
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
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/polyrex-createobject.rb', line 32 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
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 |
# File 'lib/polyrex-createobject.rb', line 75 def create_node(parent_node, child_schema, params={}, id=nil) record = Rexle.new PolyrexSchema.new(child_schema).to_s 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 |
#id ⇒ Object
26 |
# File 'lib/polyrex-createobject.rb', line 26 def id() @@id end |
#id=(s) ⇒ Object
25 |
# File 'lib/polyrex-createobject.rb', line 25 def id=(s) @@id = s; self end |
#record=(node) ⇒ Object
28 29 30 |
# File 'lib/polyrex-createobject.rb', line 28 def record=(node) @parent_node = node end |
#xpath_to_rpath(xpath) ⇒ Object
103 104 105 |
# File 'lib/polyrex-createobject.rb', line 103 def xpath_to_rpath(xpath) xpath.split('/').each_slice(2).map(&:last).join('/').gsub(/\[[^\]]+\]/,'') end |