Class: BomDB::Schema
- Inherits:
-
Object
- Object
- BomDB::Schema
- Defined in:
- lib/bomdb/schema.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
-
.create(db_path, tables = :all) ⇒ Object
create a new database.
Instance Method Summary collapse
- #create_tables(tables = :all) ⇒ Object
- #has_tables?(tables) ⇒ Boolean
-
#initialize(db) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(db) ⇒ Schema
Returns a new instance of Schema.
9 10 11 |
# File 'lib/bomdb/schema.rb', line 9 def initialize(db) @db = db end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
7 8 9 |
# File 'lib/bomdb/schema.rb', line 7 def db @db end |
Class Method Details
.create(db_path, tables = :all) ⇒ Object
create a new database
14 15 16 17 |
# File 'lib/bomdb/schema.rb', line 14 def self.create(db_path, tables = :all) FileUtils.rm(db_path) if File.exist?(db_path) new(Sequel.sqlite(db_path)).create_tables(tables) end |
Instance Method Details
#create_tables(tables = :all) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bomdb/schema.rb', line 23 def create_tables(tables = :all) @db.create_table(:books) do primary_key :book_id string :book_name, :unique => true string :book_group integer :book_sort end if include?(tables, :books) @db.create_table(:verses) do primary_key :verse_id foreign_key :book_id, :books integer :verse_chapter, :null => true integer :verse_number, :null => true integer :verse_heading, :null => true integer :verse_range_id, :unique => true, :null => true index [:book_id, :verse_chapter, :verse_number, :verse_heading], :unique => true end if include?(tables, :verses) @db.create_table(:editions) do primary_key :edition_id integer :edition_year string :edition_name, :unique => true end if include?(tables, :editions) @db.create_table(:contents) do primary_key :content_id foreign_key :edition_id, :editions foreign_key :verse_id, :verses string :content_body index [:edition_id, :verse_id], :unique => true end if include?(tables, :contents) @db.create_table(:refs) do primary_key :ref_id foreign_key :verse_id, :verses string :ref_name string :ref_book integer :ref_chapter integer :ref_verse integer :ref_page_start integer :ref_page_end boolean :ref_is_parallel boolean :ref_is_quotation end if include?(tables, :refs) @db.create_table(:notes) do primary_key :note_id foreign_key :verse_id, :verses string :note_highlight string :note_body end if include?(tables, :notes) self end |
#has_tables?(tables) ⇒ Boolean
19 20 21 |
# File 'lib/bomdb/schema.rb', line 19 def has_tables?(tables) Set.new(@db.tables) >= Set.new(tables) end |