Module: OrdDb

Defined in:
lib/ordlite/models/forward.rb,
lib/ordlite/base.rb,
lib/ordlite/schema.rb,
lib/ordlite/factory.rb,
lib/ordlite/importer.rb,
lib/ordlite/models/blob.rb,
lib/ordlite/models/factory.rb,
lib/ordlite/models/inscribe.rb,
lib/ordlite/models/collection.rb,
lib/ordlite/models/generative.rb

Overview

forward references

require first to resolve circular references

Defined Under Namespace

Modules: Model Classes: CreateDb, Importer

Constant Summary collapse

Models =

note: convenience alias for Model lets you use include OrdDb::Models

Model

Class Method Summary collapse

Class Method Details

.auto_migrate!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ordlite/base.rb', line 59

def self.auto_migrate!
  ### todo/fix:
  ##    check props table and versions!!!!!

  # first time? - auto-run db migratation, that is, create db tables
  unless LogDb::Model::Log.table_exists?
    LogDb.create     # add logs table
  end

  unless ConfDb::Model::Prop.table_exists?
    ConfDb.create    # add props table
  end

  unless OrdDb::Model::Inscribe.table_exists?
    OrdDb.create
  end
end

.connect(config = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ordlite/base.rb', line 88

def self.connect( config={} )

  if config.empty?
    puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"

    ### change default to ./ord.db ?? why? why not?
    db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///ord.db' )

    if db.scheme == 'postgres'
      config = {
        adapter: 'postgresql',
        host: db.host,
        port: db.port,
        username: db.user,
        password: db.password,
        database: db.path[1..-1],
        encoding: 'utf8'
      }
    else # assume sqlite3
     config = {
       adapter: db.scheme, # sqlite3
       database: db.path[1..-1] # ord.db (NB: cut off leading /, thus 1..-1)
    }
    end
  end

  puts "Connecting to db using settings: "
  pp config
  ActiveRecord::Base.establish_connection( config )
  # ActiveRecord::Base.logger = Logger.new( STDOUT )
end

.createObject



47
48
49
50
51
# File 'lib/ordlite/base.rb', line 47

def self.create
  CreateDb.new.up
  ConfDb::Model::Prop.create!( key: 'db.schema.ord.version', 
                               value: Ordlite::VERSION )
end

.create_allObject



53
54
55
56
57
# File 'lib/ordlite/base.rb', line 53

def self.create_all
  LogDb.create    # add logs table
  ConfDb.create   # add props table
  OrdDb.create
end

.import(id_or_ids, content: true) ⇒ Object



234
235
236
# File 'lib/ordlite/importer.rb', line 234

def self.import( id_or_ids, content: true )
   importer.import( id_or_ids, content: content )
end

.import_collection(path, content: true) ⇒ Object



244
245
246
# File 'lib/ordlite/importer.rb', line 244

def self.import_collection( path, content: true )
   importer.import_collection( path, content: content )
end

.import_collection_csv(path, name:, content: true) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/ordlite/importer.rb', line 256

def self.import_collection_csv( path,
          name:,
          content: true )
    importer.import_collection_csv( path,
           name: name,
           content: content )
end

.import_collection_inscriptions(path, name:, content: true) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/ordlite/importer.rb', line 248

def self.import_collection_inscriptions( path, 
          name:, 
          content: true )
   importer.import_collection_inscriptions( path, 
          name: name, 
          content: content )
end

.import_csv(path, content: true) ⇒ Object



239
240
241
# File 'lib/ordlite/importer.rb', line 239

def self.import_csv( path, content: true )
   importer.import_csv( path, content: content )
end

.importerObject

convenience helpers



230
231
232
# File 'lib/ordlite/importer.rb', line 230

def self.importer   ## "default" importer
   @importer ||= Importer.new 
end

.open(database = './ord.db') ⇒ Object

convenience helper for sqlite only



78
79
80
81
82
83
84
# File 'lib/ordlite/base.rb', line 78

def self.open( database='./ord.db' )   ## convenience helper for sqlite only
    connect( adapter:  'sqlite3',
             database: database )

    ## build schema if database new/empty
    auto_migrate!
end

.setup_in_memory_dbObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ordlite/base.rb', line 121

def self.setup_in_memory_db

  # Database Setup & Config
  ActiveRecord::Base.logger = Logger.new( STDOUT )
  ## ActiveRecord::Base.colorize_logging = false  - no longer exists - check new api/config setting?

  self.connect( adapter:  'sqlite3',
                database: ':memory:' )

  ## build schema
  OrdDb.create_all
end