Class: DxLite
- Inherits:
-
Object
- Object
- DxLite
- Defined in:
- lib/dxlite.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
(also: #to_a)
readonly
Returns the value of attribute records.
-
#summary ⇒ Object
Returns the value of attribute summary.
Instance Method Summary collapse
- #all ⇒ Object
- #create(h) ⇒ Object
-
#initialize(s, filepath: nil, debug: false) ⇒ DxLite
constructor
A new instance of DxLite.
-
#parse(obj = nil) ⇒ Object
(also: #import)
Parses 1 or more lines of text to create or update existing records.
- #save(file = @filepath) ⇒ 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, filepath: nil, debug: false) ⇒ DxLite
Returns a new instance of DxLite.
16 17 18 19 20 21 22 23 24 25 26 27 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dxlite.rb', line 16 def initialize(s, filepath: nil, debug: false) @filepath, @debug = filepath, debug buffer, type = RXFHelper.read(s) puts 'type: ' + type.inspect if @debug case type when :file h = JSON.parse(buffer, symbolize_names: true) @filepath = s @summary = h[:summary] @records = h[:records] when :text @summary = {schema: s} @records = [] end @schema = @summary[:schema] summary = @summary[:schema][/(?<=\[)[^\]]+/] if summary then @summary.merge! summary.split(',').map {|x|[x.strip, nil] }.to_h end # for each summary item create get and set methods @summary.each do |key, value| define_singleton_method(key) { @summary[key] } define_singleton_method (key.to_s + '=').to_sym do |value| @summary[key] = value end end @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip) @fields.each do |x| define_singleton_method ('find_all_by_' + x).to_sym do |value| @records.select do |rec| value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value end end define_singleton_method ('find_by_' + x).to_sym do |value| @records.find do |rec| value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value end end end end |
Instance Attribute Details
#records ⇒ Object (readonly) Also known as: to_a
Returns the value of attribute records.
14 15 16 |
# File 'lib/dxlite.rb', line 14 def records @records end |
#summary ⇒ Object
Returns the value of attribute summary.
13 14 15 |
# File 'lib/dxlite.rb', line 13 def summary @summary end |
Instance Method Details
#all ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/dxlite.rb', line 84 def all() @records.map do |h| RecordX.new(h, self, h.object_id, h[:created], h[:last_modified]) end end |
#create(h) ⇒ Object
92 93 94 |
# File 'lib/dxlite.rb', line 92 def create(h) @records << h end |
#parse(obj = nil) ⇒ Object Also known as: import
Parses 1 or more lines of text to create or update existing records.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dxlite.rb', line 98 def parse(obj=nil) if obj.is_a? Array then unless schema() then cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c} self.schema = "items/item(%s)" % cols.join(', ') end obj.each {|x| self.create x } return self end end |
#save(file = @filepath) ⇒ Object
115 116 117 |
# File 'lib/dxlite.rb', line 115 def save(file=@filepath) File.write file, {summary: @summary, records: @records}.to_json end |
#to_xml ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/dxlite.rb', line 121 def to_xml() root_name = schema()[/^\w+/] record_name = schema()[/(?<=\/)[^\(]+/] h = { root_name.to_sym => { summary: @summary, records: @records.map {|h| {record_name.to_sym => h} } } } Rexle.new(RexleBuilder.new(h, debug: false).to_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
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dxlite.rb', line 141 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.object_id == id} r.merge!(obj) end |