polyrex-createobject 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/polyrex-createobject.rb +90 -0
- metadata +74 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# file: polyrex-createobject.rb
|
4
|
+
|
5
|
+
require 'polyrex-schema'
|
6
|
+
require 'backtrack-xpath'
|
7
|
+
require 'rexml/document'
|
8
|
+
|
9
|
+
class PolyrexCreateObject
|
10
|
+
include REXML
|
11
|
+
|
12
|
+
attr_accessor :id
|
13
|
+
|
14
|
+
def initialize(schema)
|
15
|
+
|
16
|
+
@schema = schema
|
17
|
+
a = @schema.split('/')
|
18
|
+
|
19
|
+
@rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }
|
20
|
+
names = @rpaths.to_a[0..-2].map {|k,v| [v[/[^\[]+/], k]}
|
21
|
+
|
22
|
+
attach_create_handlers(names)
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def record=(node)
|
27
|
+
@parent_node = node
|
28
|
+
end
|
29
|
+
|
30
|
+
def attach_create_handlers(names)
|
31
|
+
methodx = names[0..-2].map do |name, xpath|
|
32
|
+
%Q(
|
33
|
+
def #{name}(params)
|
34
|
+
create_node(@parent_node, @rpaths['#{xpath}'], params)
|
35
|
+
self
|
36
|
+
end
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
name, xpath = names[-1]
|
41
|
+
|
42
|
+
methodx << %Q(
|
43
|
+
def #{name}(params)
|
44
|
+
@parent_node = XPath.first(@doc.root,'records')
|
45
|
+
record = create_node(@parent_node, @rpaths['#{xpath}'], params)
|
46
|
+
@parent_node = XPath.first(record.root, 'records')
|
47
|
+
self
|
48
|
+
end
|
49
|
+
)
|
50
|
+
|
51
|
+
self.instance_eval(methodx.join("\n"))
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def create_node(parent_node, child_schema, params)
|
56
|
+
raise "create_node error: can't create record" unless valid_creation?
|
57
|
+
record = Document.new PolyrexSchema.new(child_schema).to_s
|
58
|
+
@id = (@id.to_i + 1).to_s
|
59
|
+
|
60
|
+
record.root.add_attribute('id', @id)
|
61
|
+
|
62
|
+
a = child_schema[/[^\[]+(?=\])/].split(',')
|
63
|
+
a.each do |field_name|
|
64
|
+
field = XPath.first(record.root, 'summary/' + field_name)
|
65
|
+
field.text = params[field_name.to_sym]
|
66
|
+
end
|
67
|
+
|
68
|
+
parent_node.add record
|
69
|
+
record
|
70
|
+
end
|
71
|
+
|
72
|
+
def valid_creation?()
|
73
|
+
|
74
|
+
xpath = BacktrackXPath.new(@parent_node).to_s.gsub('//','/')
|
75
|
+
path = xpath_to_rpath(xpath).sub(/\/?records$/,'')
|
76
|
+
root_name = @schema[/^[^\/]+/]
|
77
|
+
rpath = root_name + (path.length > 0 ? '/' + path : path)
|
78
|
+
|
79
|
+
schema_rpath = @schema.gsub(/\[[^\]]+\]/,'')
|
80
|
+
local_path = (schema_rpath.split('/') - rpath.split('/')).join('/')
|
81
|
+
child_rpath = rpath + '/' + local_path
|
82
|
+
|
83
|
+
@rpaths.has_key? child_rpath
|
84
|
+
end
|
85
|
+
|
86
|
+
def xpath_to_rpath(xpath)
|
87
|
+
xpath.split('/').each_slice(2).map(&:last).join('/').gsub(/\[[^\]]+\]/,'')
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polyrex-createobject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-05-30 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: backtrack-xpath
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: polyrex-schema
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email:
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/polyrex-createobject.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: polyrex-createobject
|
73
|
+
test_files: []
|
74
|
+
|