module Persistable::ClassMethods

Public Instance Methods

attributes_names_insert_sql() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 150
def attributes_names_insert_sql
  #same idea as self.create_sql only it's returning the 'key' for sql insertions
  self.attributes.keys[1..-1].join(", ")
end
create(attributes_hash) click to toggle source

this method will dynamically create a new instance with the assigned attrs and values by doing mass assignment of the hash's key/value pairs

# File lib/mtg_card_finder/concerns/persistable.rb, line 12
def create(attributes_hash)
  #the tap method allows preconfigured methods and values to
  #be associated with the instance during instantiation while also automatically returning
  #the object after its creation is concluded.
  self.new.tap do |card|
    attributes_hash.each do |key, value|
      #sending the new instance the key name as a setter with the value
      card.send("#{key}=", value)
      #string interpolation is used as the method doesn't know the key name yet
      #but an = sign is implemented into the string in order to asssociate it as a setter
    end
    #saves the new instance into the database
    card.save
  end
end
create_sql() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 144
def create_sql
  #will apply the column names ('key') and their schemas ('value') into sql strings without having to hard code them
  #the collect method returns the revised array and then we concatenate it into a string separating the contents with a comma
  self.attributes.collect {|key, value| "#{key} #{value}"}.join(", ")
end
create_table() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 28
    def create_table
      sql = <<-SQL
          CREATE TABLE IF NOT EXISTS #{self.table_name} ( #{self.create_sql} )
      SQL

      DB[:conn].execute(sql)
    end
find(id) click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 113
    def find(id)
      sql = <<-SQL
          SELECT * FROM #{self.table_name} WHERE id=(?)
      SQL

      row = DB[:conn].execute(sql, id)
      #if a row is actually returned i.e. the id actually exists
      if row.first
        self.reify_from_row(row.first)
        #using .first array method to return only the first nested array
        #that is taken from self.reify_from_row(row) which is the resulting id of the query
      else
        puts "This card does not exist"
      end
    end
make_csv_file() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 66
def make_csv_file
  #collects everything in sql table
  rows = DB[:conn].execute("SELECT * FROM #{self.table_name}")
  date = "#{Time.now}"[0..9].gsub!("-", "_")
  #naming the csv file with today's date
  fname = "#{self}_#{date}.csv"
  #collecting the table's column names
  col_names = "#{self.attributes.keys.join(", ")} \n"
  unless File.exists? fname
    #opening the csv file to write data into
    File.open(fname, 'w') do |ofile|
      #first row will be column names
      ofile << col_names
      rows.each_with_index do |value, index|
        #iterates through all the rows values to replace commas so as to avoid line breaking errors
        value.each { |find| find.gsub!(", ", "_") if find.is_a?(String) }
        #pushing each array within rows as a newline into csv while removing nil values
        ofile << "#{rows[index].compact.join(", ")} \n"
      end
      sleep(1 + rand)
    end
  end
end
question_marks_insert_sql() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 155
def question_marks_insert_sql
  #returns the number of key-value pairs in the hash minus one for the 'id'
  questions = self.attributes.keys.size-1
  #converts them into '?' array that is then turned into comma separated string
  questions.times.collect {"?"}.join(", ")
end
reify_from_row(row) click to toggle source

opposite of abstraction is reification i.e. I'm getting the raw data of these variables

# File lib/mtg_card_finder/concerns/persistable.rb, line 130
def reify_from_row(row)
  #the tap method allows preconfigured methods and values to
  #be associated with the instance during instantiation while also automatically returning
  #the object after its creation is concluded.
  self.new.tap do |card|
    self.attributes.keys.each.with_index do |key, index|
      #sending the new instance the key name as a setter with the value located at the 'row' index
      card.send("#{key}=", row[index])
      #string interpolation is used as the method doesn't know the key name yet
      #but an = sign is implemented into the string in order to asssociate it as a setter
    end
  end
end
remove_table() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 36
    def remove_table
      sql = <<-SQL
          DROP TABLE IF EXISTS #{self.table_name}
      SQL

      DB[:conn].execute(sql)
    end
sql_columns_to_update() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 162
def sql_columns_to_update
  #returns the number of keys in the hash minus one for the 'id'
  columns = self.attributes.keys[1..-1]
  #converts them into 'attribute=(?)' array that is then turned into comma separated string
  columns.collect {|attr| "#{attr}=(?)"}.join(", ")
end
table_empty?() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 44
    def table_empty?
      sql = <<-SQL
          SELECT * FROM #{self.table_name}
      SQL

      table = DB[:conn].execute(sql)
      check = table.empty?
      if check == true
        true
      end
    end
table_name() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 6
def table_name
  "#{self.to_s.downcase}s"
end
table_rows() click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 56
    def table_rows
      sql = <<-SQL
      SELECT COUNT(*) FROM #{self.table_name}
      SQL

      table = DB[:conn].execute(sql)
      rows = table.flatten.join.to_i
      rows
    end