polyrex 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.
Files changed (2) hide show
  1. data/lib/polyrex.rb +93 -0
  2. metadata +64 -0
data/lib/polyrex.rb ADDED
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: polyrex.rb
4
+
5
+ require 'polyrex-schema'
6
+ require 'rexml/document'
7
+
8
+ class Polyrex
9
+ include REXML
10
+
11
+ def initialize(schema)
12
+
13
+ @schema = schema
14
+
15
+ @id = '1'
16
+ a = @schema.split('/')
17
+ @root_name = a[0]
18
+
19
+ @doc = Document.new "<%s><summary/><records/></%s>" % ([@root_name] * 2)
20
+ @parent_node = XPath.first(@doc.root,'records')
21
+
22
+ @rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }
23
+
24
+ names = @rpaths.to_a[0..-2].map {|k,v| [v[/.[^\[]+/], k]}
25
+
26
+ names[0..-2].each do |name, xpath|
27
+ self.instance_eval(
28
+ %Q(
29
+ def create_#{name}(params)
30
+ create_node(@parent_node, @rpaths['#{xpath}'], params)
31
+ self
32
+ end
33
+ ))
34
+ end
35
+
36
+ name, xpath = names[-1]
37
+ self.instance_eval(
38
+ %Q(
39
+ def create_#{name}(params)
40
+
41
+ @parent_node = XPath.first(@doc.root,'records')
42
+ record = create_node(@parent_node, @rpaths['#{xpath}'], params)
43
+ @parent_node = XPath.first(record.root, 'records')
44
+ self
45
+ end
46
+ ))
47
+
48
+ end
49
+
50
+ def valid_creation?()
51
+
52
+ xpath = BacktrackXPath.new(@parent_node).to_s.gsub('//','/')
53
+ path = xpath_to_rpath(xpath).sub(/\/?records$/,'')
54
+ rpath = @root_name + (path.length > 0 ? '/' + path : path)
55
+
56
+ schema_rpath = @schema.gsub(/\[[^\]]+\]/,'')
57
+ local_path = (schema_rpath.split('/') - rpath.split('/')).join('/')
58
+ child_rpath = rpath + '/' + local_path
59
+
60
+ @rpaths.has_key? child_rpath
61
+ end
62
+
63
+ def create_node(parent_node, child_schema, params)
64
+ raise "create_node error: can't create record" unless valid_creation?
65
+ record = Document.new PolyrexSchema.new(child_schema).to_s
66
+ record.root.add_attribute('id', @id)
67
+ @id = (@id.to_i + 1).to_s
68
+
69
+ a = child_schema[/[^\[]+(?=\])/].split(',')
70
+ a.each do |field_name|
71
+ field = XPath.first(record.root, 'summary/' + field_name)
72
+ field.text = params[field_name.to_sym]
73
+ end
74
+
75
+ parent_node.add record
76
+ record
77
+ end
78
+
79
+ def xpath_to_rpath(xpath)
80
+ xpath.split('/').each_slice(2).map(&:last).join('/').gsub(/\[[^\]]+\]/,'')
81
+ end
82
+
83
+ def find_by_id(id)
84
+ @parent_node = XPath.first(@doc.root, "//[@id='#{id}']")
85
+ self
86
+ end
87
+
88
+ def to_xml()
89
+ @doc.to_s
90
+ end
91
+
92
+ end
93
+
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polyrex
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-19 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
+ description:
26
+ email:
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/polyrex.rb
35
+ has_rdoc: true
36
+ homepage:
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: polyrex
63
+ test_files: []
64
+