Class: Polyrex
- Inherits:
-
Object
- Object
- Polyrex
- Defined in:
- lib/polyrex.rb
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#id_counter ⇒ Object
Returns the value of attribute id_counter.
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#summary_fields ⇒ Object
Returns the value of attribute summary_fields.
-
#type ⇒ Object
Returns the value of attribute type.
-
#xslt_schema ⇒ Object
Returns the value of attribute xslt_schema.
Instance Method Summary collapse
- #add(pxobj) ⇒ Object
- #build(records, indent = 0) ⇒ Object
- #content(options = {}) ⇒ Object
- #create(id: nil) ⇒ Object
- #delete(x = nil) ⇒ Object
- #element(s) ⇒ Object
-
#find_by_id(id) ⇒ Object
– start of crud methods –.
-
#format_masks ⇒ Object
– start of full text edit methods.
- #id(id) ⇒ Object
-
#initialize(location = nil, opt = {}) ⇒ Polyrex
constructor
A new instance of Polyrex.
- #parse(x = nil, options = {}) ⇒ Object (also: #import)
- #record ⇒ Object
- #records ⇒ Object
- #rxpath(s) ⇒ Object
- #save(filepath = nil, options = {}) ⇒ Object
- #summary ⇒ Object
- #to_a ⇒ Object
- #to_dynarex ⇒ Object
- #to_s ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #to_xslt ⇒ Object
- #xpath(s, &blk) ⇒ Object
Constructor Details
#initialize(location = nil, opt = {}) ⇒ Polyrex
Returns a new instance of Polyrex.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/polyrex.rb', line 43 def initialize(location=nil, opt={}) = {id_counter: '1'}.merge opt @id_counter = [:id_counter] @format_masks = [] @delimiter = '' if location then self.method(:schema=).call([:schema]) if [:schema] openx(location) if [:schema] then fields = @schema[/\/.*/].scan(/\[([^\]]+)/).map \ {|x| x.first.split(',').map(&:strip)} refresh_records self.records, fields, 0 end @summary = RecordX.new @doc.root.xpath("summary/*") @summary_fields = @summary.keys end @polyrex_xslt = RecordxXSLT.new end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def delimiter @delimiter end |
#id_counter ⇒ Object
Returns the value of attribute id_counter.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def id_counter @id_counter end |
#schema ⇒ Object
Returns the value of attribute schema.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def schema @schema end |
#summary_fields ⇒ Object
Returns the value of attribute summary_fields.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def summary_fields @summary_fields end |
#type ⇒ Object
Returns the value of attribute type.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def type @type end |
#xslt_schema ⇒ Object
Returns the value of attribute xslt_schema.
40 41 42 |
# File 'lib/polyrex.rb', line 40 def xslt_schema @xslt_schema end |
Instance Method Details
#add(pxobj) ⇒ Object
73 74 75 |
# File 'lib/polyrex.rb', line 73 def add(pxobj) self.record.add pxobj.node end |
#build(records, indent = 0) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/polyrex.rb', line 237 def build(records, indent=0) records.map do |item| summary = item.element 'summary' format_mask = summary.text('format_mask').to_s line = format_mask.gsub(/\[![^\]]+\]/){|x| summary.text(x[2..-2]).to_s} records = item.element('records').elements.to_a line = line + "\n" + build(records, indent + 1).join("\n") if records.length > 0 (' ' * indent) + line end end |
#content(options = {}) ⇒ Object
77 78 79 |
# File 'lib/polyrex.rb', line 77 def content(={}) CGI.unescapeHTML(to_xml()) end |
#create(id: nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/polyrex.rb', line 81 def create(id: nil) # @create is a PolyrexCreateObject, @parent_node is a Rexle::Element pointing to the current record @create.id = id || @id_counter @create.record = @parent_node.name == 'records' ? @parent_node.root : @parent_node.root.element('records') @create end |
#delete(x = nil) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/polyrex.rb', line 89 def delete(x=nil) if x.to_i.to_s == x.to_s then @doc.root.delete("//[@id='#{x}'") else @doc.root.xpath(x).each(&:delete) end end |
#element(s) ⇒ Object
155 156 157 |
# File 'lib/polyrex.rb', line 155 def element(s) @doc.root.element(s) end |
#find_by_id(id) ⇒ Object
– start of crud methods –
127 128 129 130 |
# File 'lib/polyrex.rb', line 127 def find_by_id(id) @parent_node = @doc.root.element("//[@id='#{id}']") @objects[@parent_node.name].new(@parent_node, id: @id) end |
#format_masks ⇒ Object
– start of full text edit methods
140 141 142 |
# File 'lib/polyrex.rb', line 140 def format_masks @format_masks end |
#id(id) ⇒ Object
132 133 134 135 |
# File 'lib/polyrex.rb', line 132 def id(id) @parent_node = @doc.root.element("//[@id='#{id}']") self end |
#parse(x = nil, options = {}) ⇒ Object Also known as: import
144 145 146 147 148 149 150 151 |
# File 'lib/polyrex.rb', line 144 def parse(x=nil, ={}) buffer, type = RXFHelper.read(x) buffer = yield if block_given? string_parse buffer.clone, self end |
#record ⇒ Object
107 108 109 |
# File 'lib/polyrex.rb', line 107 def record() @parent_node end |
#records ⇒ Object
159 160 161 162 163 |
# File 'lib/polyrex.rb', line 159 def records @doc.root.xpath("records/*").map do |record| @objects_a[0].new(record) end end |
#rxpath(s) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/polyrex.rb', line 165 def rxpath(s) a = @doc.root.xpath s.split('/').map \ {|x| x.sub('[','[summary/').prepend('records/')}.join('/') a.map {|x| @objects[x.name].new(x, id: x.attributes[:id]) } end |
#save(filepath = nil, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/polyrex.rb', line 116 def save(filepath=nil, ={}) refresh_summary filepath ||= @local_filepath @local_filepath = filepath xml = @doc.to_s() buffer = block_given? ? yield(xml) : xml File.open(filepath,'w'){|f| f.write buffer} end |
#summary ⇒ Object
185 186 187 |
# File 'lib/polyrex.rb', line 185 def summary @summary end |
#to_a ⇒ Object
189 190 191 |
# File 'lib/polyrex.rb', line 189 def to_a() recordx_map @doc.root end |
#to_dynarex ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/polyrex.rb', line 193 def to_dynarex() root = @doc.root.deep_clone summary = root.element('summary') e = summary.element('schema') e.text = e.text[/[^\/]+\/[^\/]+/].sub(/(\/\w+)\[([^\]]+)\]/,'\1(\2)') summary.delete('format_mask') summary.element('recordx_type').text = 'dynarex' summary.add root.element('records/*/summary/format_mask').clone root.xpath('records/*/summary/format_mask').each(&:delete) xsl_buffer =<<EOF <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:element name="summary"> <xsl:for-each select="summary/*"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:element> <xsl:element name="records"> <xsl:for-each select="records/*"> <xsl:element name="{name()}"> <xsl:copy-of select="summary/*"/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet> EOF xslt = Nokogiri::XSLT(xsl_buffer) buffer = xslt.transform(Nokogiri::XML(root.xml)).to_s Dynarex.new buffer end |
#to_s ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/polyrex.rb', line 235 def to_s() def build(records, indent=0) records.map do |item| summary = item.element 'summary' format_mask = summary.text('format_mask').to_s line = format_mask.gsub(/\[![^\]]+\]/){|x| summary.text(x[2..-2]).to_s} records = item.element('records').elements.to_a line = line + "\n" + build(records, indent + 1).join("\n") if records.length > 0 (' ' * indent) + line end end sumry = '' summary_fields = self.summary.to_h.keys %w(recordx_type schema format_mask).each {|x| summary_fields.delete x} sumry = summary_fields.map {|x| x + ': ' + \ self.summary.method(x.to_sym).call}.join("\n") + "\n" if @raw_header then declaration = @raw_header else smry_fields = %i(schema) if self.delimiter.length > 0 then smry_fields << :delimiter else smry_fields << :format_mask end s = smry_fields.map {|x| "%s=\"%s\"" % \ [x, self.summary.send(x).gsub('"', '\"') ]}.join ' ' declaration = %Q(<?polyrex %s?>\n) % s end header = declaration + sumry out = build(self.records).join("\n") header + "\n" + out end |
#to_xml(options = {}) ⇒ Object
111 112 113 114 |
# File 'lib/polyrex.rb', line 111 def to_xml(={}) refresh_summary @doc.to_s() end |
#to_xslt ⇒ Object
282 283 284 285 |
# File 'lib/polyrex.rb', line 282 def to_xslt() @polyrex_xslt.schema = @schema @polyrex_xslt.to_xslt end |