Class: Poefy::Database

Inherits:
Object
  • Object
show all
Includes:
StringManipulation
Defined in:
lib/poefy/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, local = false) ⇒ Database

Returns a new instance of Database.



21
22
23
24
25
26
27
# File 'lib/poefy/database.rb', line 21

def initialize name, local = false
  @local = local
  @name = name.to_s
  @sproc = {}
  type
  db
end

Instance Attribute Details

#localObject (readonly)

Returns the value of attribute local.



19
20
21
# File 'lib/poefy/database.rb', line 19

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/poefy/database.rb', line 19

def name
  @name
end

Instance Method Details

#closeObject

Close the database file.



57
58
59
60
61
# File 'lib/poefy/database.rb', line 57

def close
  @sproc.each { |k, v| v.close rescue nil }
  @db.close if @db
  @db = nil
end

#dbObject

Open instance database session, if not already existing.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/poefy/database.rb', line 43

def db
  if not @db and exists?
    begin
      open_connection
      create_sprocs
    rescue
      @db = nil
      raise Poefy::StructureInvalid
    end
  end
  @db
end

#lines_by_rhyme(rhyme, syllable_min_max = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/poefy/database.rb', line 105

def lines_by_rhyme rhyme, syllable_min_max = nil
  db
  @la = Hash.new { |h,k| h[k] = {} } if @la.nil?
  if @la[rhyme][syllable_min_max].nil?
    @la[rhyme][syllable_min_max] = if syllable_min_max
      sproc_lines_by_rhyme_syllables(rhyme, syllable_min_max)
    else
      sproc_lines_by_rhyme(rhyme)
    end
  end
  @la[rhyme][syllable_min_max].dup
end

#make_new(lines, description = nil) ⇒ Object

Creates a database with the correct format.

Convert input lines array to SQL import format file.
Delete database if already exists.
Create database using SQL import file.
Delete both files.


68
69
70
# File 'lib/poefy/database.rb', line 68

def make_new lines, description = nil
  make_new!(lines, description) if !exists?
end

#make_new!(lines, description = nil) ⇒ Object

Force new database, overwriting existing.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/poefy/database.rb', line 73

def make_new! lines, description = nil

  # Create a new database.
  new_connection

  # Create the lines table and the index.
  create_table table, description

  # Convert the lines array into an expanded array of rhyme metadata.
  import_data =  lines

  # Import the data.
  insert_lines table, import_data

  # Recreate the stored procedures.
  create_sprocs
end

#rhymes_by_count(rhyme_count, syllable_min_max = nil) ⇒ Object

Public interfaces for private stored procedure methods. Use instance variables to keep a cache of the results.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/poefy/database.rb', line 93

def rhymes_by_count rhyme_count, syllable_min_max = nil
  db
  @rbc = Hash.new { |h,k| h[k] = {} } if @rbc.nil?
  if @rbc[rhyme_count][syllable_min_max].nil?
    @rbc[rhyme_count][syllable_min_max] = if syllable_min_max
      sproc_rhymes_by_count_syllables(rhyme_count, syllable_min_max)
    else
      sproc_rhymes_by_count(rhyme_count)
    end
  end
  @rbc[rhyme_count][syllable_min_max].dup
end

#typeObject

Validate that a database type has been required. This will be overwritten by a database-specific method,

so raise an error if no database has been specified yet.

Due to the way ‘bin/poefy’ is set up, that code will fail before

this point is reached, so this error is only from Ruby calls.

Raises:

  • (LoadError)


36
37
38
39
40
# File 'lib/poefy/database.rb', line 36

def type
  msg = "No database interface specified. " +
        "Please require 'poefy/sqlite3' or 'poefy/pg'"
  raise LoadError, msg
end