Class: Jsonsql::Importer
- Inherits:
-
Object
- Object
- Jsonsql::Importer
- 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
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #import(files) ⇒ Object
- #import_jsonfile(filename) ⇒ Object
-
#initialize(database: Sequel.sqlite, table_name: "table") ⇒ Importer
constructor
A new instance of Importer.
- #table ⇒ Object
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
#database ⇒ Object (readonly)
Returns the value of attribute database.
8 9 10 |
# File 'lib/jsonsql/importer.rb', line 8 def database @database end |
#table_name ⇒ Object (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 |
#table ⇒ Object
16 17 18 |
# File 'lib/jsonsql/importer.rb', line 16 def table @table ||= @database[table_name.to_sym] end |