Class: DxLite
Overview
DxLite can perform the following:
-
read or write an XML file
-
read or write a JSON file (in Dynarex format)
-
import an Array of records (Hash objects)
note: It cannot read a Dynarex file in
the raw Dynarex format (.txt plain text)
Instance Attribute Summary collapse
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#summary ⇒ Object
Returns the value of attribute summary.
Instance Method Summary collapse
- #all ⇒ Object
- #create(rawh, id: nil, custom_attributes: {created: Time.now}) ⇒ Object
- #delete(id) ⇒ Object
- #fields ⇒ Object
-
#initialize(s = nil, autosave: false, debug: false) ⇒ DxLite
constructor
A new instance of DxLite.
- #inspect ⇒ Object
-
#parse(obj = nil) ⇒ Object
(also: #import)
Parses 1 or more lines of text to create or update existing records.
- #parse_xml(buffer) ⇒ Object
- #save(file = @filepath) ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_json(pretty: true) ⇒ Object
- #to_xml ⇒ Object
-
#update(id, obj) ⇒ Object
Updates a record from an id and a hash containing field name and field value.
Constructor Details
#initialize(s = nil, autosave: false, debug: false) ⇒ DxLite
Returns a new instance of DxLite.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dxlite.rb', line 28 def initialize(s=nil, autosave: false, debug: false) @autosave, @debug = autosave, debug return unless s buffer, type = RXFReader.read(s) @filepath = s if type == :file or type == :dfs puts 'type: ' + type.inspect if @debug puts 'buffer: ' + buffer.inspect if @debug @records = [] case type when :dfs read buffer when :file read buffer when :text new_summary(s) when :url read buffer end end |
Instance Attribute Details
#filepath ⇒ Object
Returns the value of attribute filepath.
25 26 27 |
# File 'lib/dxlite.rb', line 25 def filepath @filepath end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
26 27 28 |
# File 'lib/dxlite.rb', line 26 def records @records end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
26 27 28 |
# File 'lib/dxlite.rb', line 26 def schema @schema end |
#summary ⇒ Object
Returns the value of attribute summary.
25 26 27 |
# File 'lib/dxlite.rb', line 25 def summary @summary end |
Instance Method Details
#all ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dxlite.rb', line 64 def all() @records.map do |h| puts 'h: ' + h.inspect if @debug RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified]) end end |
#create(rawh, id: nil, custom_attributes: {created: Time.now}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dxlite.rb', line 85 def create(rawh, id: nil, custom_attributes: {created: Time.now}) if @debug then puts 'create:: rawh: ' + rawh.inspect puts 'custom_attributes: ' + custom_attributes.inspect end key = @summary[:default_key] if key then r = records.find {|x| x[:body][key.to_sym] == rawh[key.to_sym]} if r then r[:last_modified] = Time.now.to_s return false end end if id.nil? then puts '@records: ' + @records.inspect if @debug if @records then id = @records.map {|x| x[:id].to_i}.max.to_i + 1 else @records = [] id = 1 end end h2 = custom_attributes fields = rawh.keys puts 'fields: ' + fields.inspect if @debug h3 = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh) h = {id: id.to_s, created: h2[:created], last_modified: nil, body: h3} @records << h save() if @autosave RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified]) end |
#delete(id) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/dxlite.rb', line 75 def delete(id) found = @records.find {|x| x[:id] == id} if found then @records.delete found save() if @autosave end end |
#fields ⇒ Object
132 133 134 |
# File 'lib/dxlite.rb', line 132 def fields() @fields end |
#inspect ⇒ Object
136 137 138 139 |
# File 'lib/dxlite.rb', line 136 def inspect() "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug, @summary.inspect] end |
#parse(obj = nil) ⇒ Object Also known as: import
Parses 1 or more lines of text to create or update existing records.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/dxlite.rb', line 143 def parse(obj=nil) @records ||= [] if obj.is_a? Array then unless schema() then puts 'obj.first: ' + obj.first.inspect if @debug cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c} puts 'after cols' if @debug new_summary "items/item(%s)" % cols.join(', ') end obj.each do |x| #puts 'x: ' + x.inspect if @debug self.create x, id: nil end return self end end |
#parse_xml(buffer) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dxlite.rb', line 168 def parse_xml(buffer) doc = Rexle.new(buffer.force_encoding('UTF-8')) asummary = doc.root.xpath('summary/*').map do |node| puts 'node: ' + node.xml.inspect if @debug [node.name, node.text.to_s] end summary = Hash[asummary] summary[:schema] = summary['schema'] %w(recordx_type format_mask schema).each {|x| summary.delete x} @schema = summary[:schema] puts 'schema: ' + schema.inspect if @debug @fields = @schema[/\(([^\)]+)/,1].split(/ *, */) puts 'fields: ' + @fields.inspect if @debug @summary = summary a = doc.root.xpath('records/*').each do |node| h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }] self.create h, id: nil, custom_attributes: node.attributes end end |
#save(file = @filepath) ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'lib/dxlite.rb', line 198 def save(file=@filepath) return unless file @filepath = file s = File.extname(file) == '.json' ? to_json() : to_xml() FileX.write file, s end |
#to_a ⇒ Object
207 208 209 |
# File 'lib/dxlite.rb', line 207 def to_a() @records.map {|x| x[:body]} end |
#to_h ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/dxlite.rb', line 211 def to_h() root_name = schema()[/^\w+/] record_name = schema()[/(?<=\/)[^\(]+/] h = { root_name.to_sym => { summary: @summary, records: @records.map {|h| {record_name.to_sym => h} } } } end |
#to_json(pretty: true) ⇒ Object
226 227 228 |
# File 'lib/dxlite.rb', line 226 def to_json(pretty: true) pretty ? JSON.pretty_generate(to_h()) : to_h() end |
#to_xml ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/dxlite.rb', line 230 def to_xml() root_name = schema()[/^\w+/] record_name = schema()[/(?<=\/)[^\(]+/] a = RexleBuilder.build do |xml| xml.send(root_name.to_sym) do xml.summary({}, @summary) xml.records do all().each do |x| h = {id: x.id, created: x.created, last_modified: x.last_modified} puts 'x.to_h: ' + x.to_h.inspect if @debug xml.send(record_name.to_sym, h, x.to_h) end end end end Rexle.new(a).xml pretty: true end |
#update(id, obj) ⇒ Object
Updates a record from an id and a hash containing field name and field value.
dynarex.update 4, name: Jeff, age: 38
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/dxlite.rb', line 261 def update(id, obj) if @debug then puts 'inside update'.info puts ('id: ' + id.inspect).debug puts ('obj.class: ' + obj.class.inspect).debug end r = @records.find {|x| x[:id] == id} if r then r[:body].merge!(obj) save() if @autosave end end |