Class: BomDB::Import::Base
- Inherits:
-
Object
- Object
- BomDB::Import::Base
- Defined in:
- lib/bomdb/import/base.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
- #ensure_parsed_json(data) ⇒ Object
- #import(data, format: 'json') ⇒ Object
-
#initialize(db, **opts) ⇒ Base
constructor
A new instance of Base.
- #schema ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(db, **opts) ⇒ Base
Returns a new instance of Base.
8 9 10 11 |
# File 'lib/bomdb/import/base.rb', line 8 def initialize(db, **opts) @db = db @opts = opts end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
6 7 8 |
# File 'lib/bomdb/import/base.rb', line 6 def db @db end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/bomdb/import/base.rb', line 6 def opts @opts end |
Class Method Details
.tables(*tables) ⇒ Object
13 14 15 |
# File 'lib/bomdb/import/base.rb', line 13 def self.tables(*tables) @tables = tables end |
Instance Method Details
#ensure_parsed_json(data) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/bomdb/import/base.rb', line 46 def ensure_parsed_json(data) if data.is_a?(String) JSON.parse(data) else data end end |
#import(data, format: 'json') ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bomdb/import/base.rb', line 21 def import(data, format: 'json') if !schema.has_tables?(tables) return Import::Result.new( success: false, error: "Database table(s) not present: [#{tables.join(', ')}]" ) end import_before() if respond_to?(:import_before) result = case format when 'json' then import_json(ensure_parsed_json(data)) when 'text' then import_text(data) else return Import::Result.new( success: false, error: "Unknown format: #{format}" ) end import_after() if respond_to?(:import_after) result end |
#schema ⇒ Object
54 55 56 |
# File 'lib/bomdb/import/base.rb', line 54 def schema BomDB::Schema.new(@db) end |
#tables ⇒ Object
17 18 19 |
# File 'lib/bomdb/import/base.rb', line 17 def tables self.class.instance_variable_get("@tables") end |