Class: DxLite

Inherits:
Object
  • Object
show all
Defined in:
lib/dxlite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s = nil, autosave: false, debug: false) ⇒ DxLite

Returns a new instance of DxLite.



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
# File 'lib/dxlite.rb', line 17

def initialize(s=nil, autosave: false, debug: false)

  @autosave, @debug = autosave, debug

  return unless s
  buffer, type = RXFHelper.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 :file
    
    read buffer
    
  when :text
    
    @summary = {schema: s}

    
  when :url
    
    read buffer      
    
  end
  
  puts '@summary: ' + @summary.inspect
  @schema = @summary[:schema]
  
  summary_attributes = {
    recordx_type: 'dynarex',
    default_key: @schema[/(?<=\()\w+/]
  }

  puts 'before merge' if @debug
  @summary.merge!(summary_attributes)

  summary = @summary[:schema][/(?<=\[)[^\]]+/]
  
  if summary then

    summary.split(/ *, */).each do |x|
      @summary[x] = nil unless @summary[x]
    end
    
  end    
  
  make_methods()    

end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



15
16
17
# File 'lib/dxlite.rb', line 15

def records
  @records
end

#summaryObject

Returns the value of attribute summary.



14
15
16
# File 'lib/dxlite.rb', line 14

def summary
  @summary
end

Instance Method Details

#allObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/dxlite.rb', line 73

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/dxlite.rb', line 94

def create(rawh, id: nil, custom_attributes: {created: Time.now})
  
  id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
  h2 = custom_attributes
  h = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
  @records << {id: id.to_s, created: h2[:created], last_modified: nil, 
               body: h}
  
  save() if @autosave
end

#delete(id) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/dxlite.rb', line 84

def delete(id)
  found = @records.find {|x| x[:id] == id}
  
  if found then
    @records.delete found 
    save() if @autosave
  end
  
end

#fieldsObject



105
106
107
# File 'lib/dxlite.rb', line 105

def fields()
  @fields
end

#inspectObject



109
110
111
112
# File 'lib/dxlite.rb', line 109

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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dxlite.rb', line 116

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 do |x|
      #puts 'x: ' + x.inspect if @debug
      self.create x, id: nil
    end
    
    return self 
    
  end  
end

#parse_xml(buffer) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dxlite.rb', line 137

def parse_xml(buffer)
  
  doc = Rexle.new(buffer)
  
  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
  
  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
    
  end

  @summary = summary
  
end

#save(file = @filepath) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/dxlite.rb', line 167

def save(file=@filepath)
  
  return unless file
  
  s = File.extname(file) == '.json' ? to_json() : to_xml()
  File.write file, s
end

#to_aObject



175
176
177
# File 'lib/dxlite.rb', line 175

def to_a()
  @records.map {|x| x[:body]}
end

#to_hObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dxlite.rb', line 179

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



194
195
196
# File 'lib/dxlite.rb', line 194

def to_json(pretty: true)
  pretty ? JSON.pretty_generate(to_h()) : to_h()
end

#to_xmlObject



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
# File 'lib/dxlite.rb', line 198

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


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/dxlite.rb', line 229

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