polyrex 0.4.2 → 0.4.3

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 +57 -30
  2. metadata +2 -2
data/lib/polyrex.rb CHANGED
@@ -11,35 +11,8 @@ require 'rexml/document'
11
11
  class Polyrex
12
12
  include REXML
13
13
 
14
- def initialize(schema)
15
-
16
- # -- required for the parsing feature
17
- doc = Document.new(PolyrexSchema.new(schema).to_s)
18
- @format_masks = XPath.match(doc.root, '//format_mask/text()').map &:to_s
19
- schema_rpath = schema.gsub(/\[[^\]]+\]/,'')
20
- @recordx = schema_rpath.split('/')
21
-
22
- if @format_masks.length == @recordx.length then
23
- root_format_mask = @format_masks.shift
24
- field_names = root_format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
25
- summary = field_names.map {|x| "<%s/>" % x}.join
26
- end
27
- #----
28
-
29
- @schema = schema
30
- @id = '0'
31
-
32
- @create = PolyrexCreateObject.new(schema)
33
-
34
- @objects = PolyrexObjects.new(schema).to_h
35
- attach_create_handlers(@objects.keys)
36
- attach_edit_handlers(@objects)
37
-
38
- @root_name = @recordx.shift
39
-
40
- @doc = Document.new("<%s><summary>%s</summary><records/></%s>" % [@root_name, (summary || '') , @root_name])
41
- @parent_node = XPath.first(@doc.root,'records')
42
-
14
+ def initialize(location)
15
+ open(location)
43
16
  end
44
17
 
45
18
  def create()
@@ -87,6 +60,38 @@ class Polyrex
87
60
 
88
61
  private
89
62
 
63
+ def polyrex_new(schema)
64
+ # -- required for the parsing feature
65
+ doc = Document.new(PolyrexSchema.new(schema).to_s)
66
+ @format_masks = XPath.match(doc.root, '//format_mask/text()').map &:to_s
67
+ schema_rpath = schema.gsub(/\[[^\]]+\]/,'')
68
+ @recordx = schema_rpath.split('/')
69
+
70
+ summary = ''
71
+ if @format_masks.length == @recordx.length then
72
+ root_format_mask = @format_masks.shift
73
+ field_names = root_format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
74
+ summary = field_names.map {|x| "<%s/>" % x}.join
75
+ end
76
+
77
+ summary << "<schema>#{schema}</schema>"
78
+ #----
79
+
80
+ @schema = schema
81
+ @id = '0'
82
+
83
+ root_name = @recordx.shift
84
+
85
+ ("<%s><summary>%s</summary><records/></%s>" % [root_name, (summary || '') , root_name])
86
+ end
87
+
88
+ def load_handlers(schema)
89
+ @create = PolyrexCreateObject.new(schema)
90
+ @objects = PolyrexObjects.new(schema).to_h
91
+ attach_create_handlers(@objects.keys)
92
+ attach_edit_handlers(@objects)
93
+ end
94
+
90
95
  def format_line!(records, a, i=0)
91
96
 
92
97
  a.each do |x|
@@ -145,6 +150,28 @@ class Polyrex
145
150
  ))
146
151
  end
147
152
 
148
- end
153
+ end
154
+
155
+ def open(s)
156
+ if s[/\[/] then # schema
157
+ buffer = polyrex_new s
158
+ elsif s[/^https?:\/\//] then # url
159
+ buffer = Kernel.open(s, 'UserAgent' => 'Polyrex-Reader').read
160
+ elsif s[/\</] # xml
161
+ buffer = s
162
+ else # local file
163
+ buffer = File.open(s,'r').read
164
+ end
165
+
166
+ puts '*' + buffer + '*'
167
+ @doc = Document.new buffer
168
+ @id = XPath.match(@doc.root, '//@id').map{|x| x.value.to_i}.max.to_i + 1
169
+
170
+ schema = @doc.root.text('summary/schema')
171
+ #puts 'schema : ' + schema
172
+ load_handlers(schema)
173
+ @parent_node = XPath.first(@doc.root,'records')
174
+
175
+ end
149
176
 
150
177
  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.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-30 00:00:00 +01:00
12
+ date: 2010-05-31 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency