polyrex 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/polyrex.rb +47 -65
  2. metadata +15 -5
data/lib/polyrex.rb CHANGED
@@ -3,8 +3,9 @@
3
3
  # file: polyrex.rb
4
4
 
5
5
  require 'polyrex-schema'
6
- require 'backtrack-xpath'
7
6
  require 'line-tree'
7
+ require 'polyrex-objects'
8
+ require 'polyrex-createobject'
8
9
  require 'rexml/document'
9
10
 
10
11
  class Polyrex
@@ -26,14 +27,13 @@ class Polyrex
26
27
  #----
27
28
 
28
29
  @schema = schema
29
-
30
30
  @id = '0'
31
- a = @schema.split('/')
32
31
 
33
- @rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }
32
+ @create = PolyrexCreateObject.new(schema)
34
33
 
35
- names = @rpaths.to_a[0..-2].map {|k,v| [v[/[^\[]+/], k]}
36
- attach_create_handlers(names)
34
+ @objects = PolyrexObjects.new(schema).to_h
35
+ attach_create_handlers(@objects.keys)
36
+ attach_edit_handlers(@objects)
37
37
 
38
38
  @root_name = @recordx.shift
39
39
 
@@ -42,69 +42,36 @@ class Polyrex
42
42
 
43
43
  end
44
44
 
45
- def attach_create_handlers(names)
46
- names[0..-2].each do |name, xpath|
47
- self.instance_eval(
48
- %Q(
49
- def create_#{name}(params)
50
- create_node(@parent_node, @rpaths['#{xpath}'], params)
51
- self
45
+ def create()
46
+ @create.id = @id
47
+ @create.record = @parent_node
48
+ @create
52
49
  end
53
- ))
54
- end
55
50
 
56
- name, xpath = names[-1]
57
- self.instance_eval(
58
- %Q(
59
- def create_#{name}(params)
60
-
61
- @parent_node = XPath.first(@doc.root,'records')
62
- record = create_node(@parent_node, @rpaths['#{xpath}'], params)
63
- @parent_node = XPath.first(record.root, 'records')
64
- self
65
- end
66
- ))
67
-
51
+ def delete()
52
+ @parent_node.parent.delete @parent_node
68
53
  end
69
-
70
- # -- start of crud methods --
71
- def valid_creation?()
72
-
73
- xpath = BacktrackXPath.new(@parent_node).to_s.gsub('//','/')
74
- path = xpath_to_rpath(xpath).sub(/\/?records$/,'')
75
- rpath = @root_name + (path.length > 0 ? '/' + path : path)
76
54
 
77
- schema_rpath = @schema.gsub(/\[[^\]]+\]/,'')
78
- local_path = (schema_rpath.split('/') - rpath.split('/')).join('/')
79
- child_rpath = rpath + '/' + local_path
80
-
81
- @rpaths.has_key? child_rpath
55
+ def record()
56
+ @parent_node
82
57
  end
83
58
 
84
- def create_node(parent_node, child_schema, params)
85
- raise "create_node error: can't create record" unless valid_creation?
86
- record = Document.new PolyrexSchema.new(child_schema).to_s
87
- record.root.add_attribute('id', @id)
88
- @id = (@id.to_i + 1).to_s
89
-
90
- a = child_schema[/[^\[]+(?=\])/].split(',')
91
- a.each do |field_name|
92
- field = XPath.first(record.root, 'summary/' + field_name)
93
- field.text = params[field_name.to_sym]
94
- end
95
-
96
- parent_node.add record
97
- record
59
+ def to_xml()
60
+ @doc.to_s
98
61
  end
99
62
 
100
- def xpath_to_rpath(xpath)
101
- xpath.split('/').each_slice(2).map(&:last).join('/').gsub(/\[[^\]]+\]/,'')
102
- end
63
+ def save(filepath)
64
+ File.open(filepath,'w'){|f| @doc.write f}
65
+ end
66
+
67
+ # -- start of crud methods --
103
68
 
104
69
  def find_by_id(id)
105
70
  @parent_node = XPath.first(@doc.root, "//[@id='#{id}']")
106
71
  self
107
72
  end
73
+
74
+ alias id find_by_id
108
75
  # -- end of crud methods --
109
76
 
110
77
  # -- start of full text edit methods
@@ -117,6 +84,8 @@ end
117
84
  self
118
85
  end
119
86
 
87
+ private
88
+
120
89
  def format_line!(records, a, i=0)
121
90
 
122
91
  a.each do |x|
@@ -151,17 +120,30 @@ end
151
120
  end
152
121
 
153
122
  # -- end of full text edit methods
154
-
155
- def record()
156
- @parent_node
157
- end
158
123
 
159
- def to_xml()
160
- @doc.to_s
124
+ def attach_create_handlers(names)
125
+ methodx = names.map do |name|
126
+ %Q(
127
+ def create_#{name}(params)
128
+ self.create.'#{name.downcase}'
161
129
  end
130
+ )
131
+ end
162
132
 
163
- def save(filepath)
164
- File.open(filepath,'w'){|f| @doc.write f}
133
+ self.instance_eval(methodx.join("\n"))
134
+
135
+ end
136
+
137
+ def attach_edit_handlers(objects)
138
+ objects.keys.each do |name|
139
+ self.instance_eval(
140
+ %Q(
141
+ def #{name.downcase}()
142
+ @objects['#{name}'].new(@parent_node)
143
+ end
144
+ ))
145
+ end
146
+
165
147
  end
166
-
148
+
167
149
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyrex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-29 00:00:00 +01:00
12
+ date: 2010-05-30 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: backtrack-xpath
16
+ name: polyrex-schema
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
@@ -23,7 +23,7 @@ dependencies:
23
23
  version: "0"
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
- name: polyrex-schema
26
+ name: line-tree
27
27
  type: :runtime
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
@@ -33,7 +33,17 @@ dependencies:
33
33
  version: "0"
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
- name: line-tree
36
+ name: polyrex-objects
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: polyrex-createobject
37
47
  type: :runtime
38
48
  version_requirement:
39
49
  version_requirements: !ruby/object:Gem::Requirement