Class: ApkgToCsv::Model

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, model_hash) ⇒ Model

Returns a new instance of Model.



20
21
22
23
# File 'lib/apkg_to_csv/model.rb', line 20

def initialize(id, model_hash)
  @id = id.to_s
  @model_hash = model_hash
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/apkg_to_csv/model.rb', line 18

def id
  @id
end

Class Method Details

.from_db(db) ⇒ Object



6
7
8
9
10
# File 'lib/apkg_to_csv/model.rb', line 6

def self.from_db(db)
  db.execute('SELECT models FROM col').flat_map do |row|
    from_row(row)
  end
end

.from_row(row) ⇒ Object



12
13
14
15
16
# File 'lib/apkg_to_csv/model.rb', line 12

def self.from_row(row)
  row.flat_map do |models_json|
    JSON.parse(models_json).map { |k, v| new(k, v) }
  end
end

Instance Method Details

#fieldsObject



25
26
27
# File 'lib/apkg_to_csv/model.rb', line 25

def fields
  @fields ||= model_hash['flds'].map { |f| f['name'] }
end