class Farmstead::DB

Public Class Methods

add_source(module_name, module_type, module_call) click to toggle source
# File lib/farmstead/db.rb, line 36
def self.add_source(module_name, module_type, module_call)
  self.pull_variables
  ds = @@DB[:sources]
  ds.insert([:name, :type, :module], [module_name,module_type,module_call])
end
create() click to toggle source

Create tables

# File lib/farmstead/db.rb, line 20
def self.create
  self.pull_variables
  @@DB.create_table :sources do
    primary_key(:id)
    String(:name)
    String(:type)
    String(:module)
    TrueClass(:pickedup)
    TrueClass(:processed)
  end
  @@DB.create_table :test do
    primary_key(:id)
    String(:result)
  end
end
create_table(table, hash) click to toggle source

For now keep it simple

# File lib/farmstead/db.rb, line 70
def self.create_table(table, hash)
  self.pull_variables
  string = "CREATE TABLE IF NOT EXISTS #{table} (id MEDIUMINT NOT NULL AUTO_INCREMENT primary key, "
  hash.each_with_index do |(key, value), index|
    string = string + key.to_s + " " + value
    unless index == 1
      string = string + ","
    end
  end
  string = string + ")"
  puts string
  @@DB.run string
end
insert(table, hash) click to toggle source
# File lib/farmstead/db.rb, line 48
def self.insert(table, hash)
  self.pull_variables
  string = "INSERT INTO #{table} ("
  hash.each_with_index do |(key, value), index|
    string = string + key.to_s
    unless index == 1
      string = string + ","
    end
  end
  string = string + ") VALUES ("
  hash.each_with_index do |(key, value), index|
    string = string + "'" + value + "'"
    unless index == 1
      string = string + ","
    end
  end
  string = string + ")"
  puts string
  @@DB.run string
end
insert_test(result) click to toggle source

Insert an array of values into a table

# File lib/farmstead/db.rb, line 85
def self.insert_test(result)
  self.pull_variables
  ds = @@DB[:test]
  ds.insert(result: result)
end
list(table) click to toggle source
# File lib/farmstead/db.rb, line 42
def self.list(table)
  self.pull_variables
  ds = @@DB[table]
  puts ds.all
end
pull_variables() click to toggle source
# File lib/farmstead/db.rb, line 5
def self.pull_variables
  @@mysql_host = ENV["MYSQL_HOST"]
  @@mysql_password = ENV["MYSQL_PASSWORD"]
  @@mysql_user = ENV["MYSQL_USER"]
  @@mysql_database = ENV["MYSQL_DATABASE"]
  @@mysql_port = ENV["MYSQL_PORT"]
  @@DB = Sequel.connect(adapter: "mysql2", host: @@mysql_host, port: @@mysql_port, database: @@mysql_database, user: @@mysql_user, password: @@mysql_password)
end
select_all(table) click to toggle source
# File lib/farmstead/db.rb, line 14
def self.select_all(table)
  self.pull_variables
  @@DB[:sources]
end

Public Instance Methods

mark_pickedup(source) click to toggle source

Sets the value of pickedup to true

# File lib/farmstead/db.rb, line 92
def mark_pickedup(source)
  @@DB.run("UPDATE sources SET pickedup = 1 WHERE module = #{source}")
end
mark_processed(source) click to toggle source

Sets the value of processed to true

# File lib/farmstead/db.rb, line 97
def mark_processed(source)
  @@DB.run("UPDATE sources SET processed = 1 WHERE module = #{source}")
end