Class: Jsonsql::Importer

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

Constant Summary collapse

SEQUEL_SUPPORTED_CLASSES =
[Integer, String, Fixnum, Bignum, Float, BigDecimal, Date, DateTime, Time, Numeric, TrueClass, FalseClass]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database: Sequel.sqlite, table_name: "table") ⇒ Importer

Returns a new instance of Importer.



10
11
12
13
14
# File 'lib/jsonsql/importer.rb', line 10

def initialize(database: Sequel.sqlite, table_name: "table")
  @table_name = table_name
  @database   = database
  @table_created = false
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



8
9
10
# File 'lib/jsonsql/importer.rb', line 8

def database
  @database
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



8
9
10
# File 'lib/jsonsql/importer.rb', line 8

def table_name
  @table_name
end

Instance Method Details

#import(files) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/jsonsql/importer.rb', line 20

def import(files)
  database.transaction do
    files.each do |filename|
      import_jsonfile(filename)
    end
  end
end

#import_jsonfile(filename) ⇒ Object



28
29
30
31
32
# File 'lib/jsonsql/importer.rb', line 28

def import_jsonfile(filename)
  row = JSON.parse(open(filename).read)
  create_table_if_needed(row)
  table.insert(row)
end

#tableObject



16
17
18
# File 'lib/jsonsql/importer.rb', line 16

def table
  @table ||= @database[table_name.to_sym]
end