polyrex 0.8.7 → 0.8.9

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 +38 -11
  2. metadata +4 -4
data/lib/polyrex.rb CHANGED
@@ -12,15 +12,19 @@ require 'polyrex-xslt'
12
12
  require 'rexle'
13
13
 
14
14
  class Polyrex
15
- attr_accessor :summary_fields, :xslt_schema, :id_counter
15
+ attr_accessor :summary_fields, :xslt_schema, :id_counter, :schema
16
16
 
17
- def initialize(location, id_counter='1')
17
+ def initialize(location=nil, id_counter='1')
18
18
 
19
19
  @id_counter = id_counter
20
- open(location)
21
- summary_h = Hash[*@doc.xpath("summary/*").map {|x| [x.name, x.text]}.flatten]
22
- @summary = OpenStruct.new summary_h
23
- @summary_fields = summary_h.keys.map(&:to_sym)
20
+
21
+ if location then
22
+ open(location)
23
+ summary_h = Hash[*@doc.xpath("summary/*").map {|x| [x.name, x.text]}.flatten]
24
+ @summary = OpenStruct.new summary_h
25
+ @summary_fields = summary_h.keys.map(&:to_sym)
26
+ end
27
+
24
28
  @polyrex_xslt = PolyrexXSLT.new
25
29
  end
26
30
 
@@ -85,6 +89,14 @@ class Polyrex
85
89
  @objects_a[0].new(record)
86
90
  end
87
91
  end
92
+
93
+ def schema=(s)
94
+ open s
95
+ summary_h = Hash[*@doc.xpath("summary/*").map {|x| [x.name, x.text]}.flatten]
96
+ @summary = OpenStruct.new summary_h
97
+ @summary_fields = summary_h.keys.map(&:to_sym)
98
+ self
99
+ end
88
100
 
89
101
  def summary
90
102
  @summary
@@ -120,11 +132,12 @@ class Polyrex
120
132
  doc = Rexle.new(PolyrexSchema.new(schema).to_s)
121
133
  @format_masks = doc.xpath('//format_mask/text()')
122
134
  schema_rpath = schema.gsub(/\[[^\]]+\]/,'')
135
+
123
136
  @recordx = schema_rpath.split('/')
124
137
 
125
138
  summary = ''
126
139
  if @format_masks.length == @recordx.length then
127
- root_format_mask = @format_masks.shift
140
+ root_format_mask = @format_masks.shift
128
141
  field_names = root_format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
129
142
  summary = field_names.map {|x| "<%s/>" % x}.join
130
143
  end
@@ -159,7 +172,17 @@ class Polyrex
159
172
  end
160
173
 
161
174
  def string_parse(lines)
162
- format_line!(@parent_node, LineTree.new(lines).to_a)
175
+ raw_header = lines.slice!(/<\?polyrex[^>]+>/)
176
+
177
+ if raw_header then
178
+ header = raw_header[/<?polyrex (.*)?>/,1]
179
+ header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); \
180
+ [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each do |name, value|
181
+ self.method(name).call(value)
182
+ end
183
+ end
184
+
185
+ format_line!(@parent_node, LineTree.new(lines.strip).to_a)
163
186
  end
164
187
 
165
188
  def load_handlers(schema)
@@ -168,6 +191,7 @@ class Polyrex
168
191
  objects = PolyrexObjects.new(schema, @id_counter)
169
192
  @objects = objects.to_h
170
193
  @objects_a = objects.to_a
194
+
171
195
  attach_create_handlers(@objects.keys)
172
196
  attach_edit_handlers(@objects)
173
197
  end
@@ -191,6 +215,7 @@ class Polyrex
191
215
  @field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
192
216
 
193
217
  t = regexify_fmask(@format_masks[i]) #.sub(/\[/,'\[').sub(/\]/,'\]')
218
+
194
219
  a = t.reverse.split(/(?=\)[^\(]+\()/).reverse.map &:reverse
195
220
  patterns = tail_map(a)
196
221
 
@@ -290,15 +315,17 @@ class Polyrex
290
315
  id = @doc.xpath('max(//@id)')
291
316
  @id_counter = id.to_s.succ if id
292
317
 
293
- load_handlers(schema)
294
- load_find_by(schema)
318
+ if schema then
319
+ load_handlers(schema)
320
+ load_find_by(schema)
321
+ end
295
322
 
296
323
  @parent_node = @doc.element('records')
297
324
 
298
325
  end
299
326
 
300
327
  def regexify_fmask(f)
301
-
328
+
302
329
  a = f.split(/(?=\[!\w+\])/).map do |x|
303
330
 
304
331
  aa = x.split(/(?=[^\]]+$)/)
metadata CHANGED
@@ -2,15 +2,15 @@
2
2
  name: polyrex
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.7
5
+ version: 0.8.9
6
6
  platform: ruby
7
- authors: []
8
-
7
+ authors:
8
+ - James Robertson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-23 00:00:00 +00:00
13
+ date: 2011-08-25 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency