module Persistable::InstanceMethods
Public Instance Methods
attribute_values_for_sql_check()
click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 178 def attribute_values_for_sql_check self.class.attributes.keys[1..-1].collect {|attr_names| self.send(attr_names)} #I go through the key names (minus 'id') and return an array containing their values for the recieving instance #basically like getting an array of getter methods for that instance end
insert()
click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 199 def insert sql = <<-SQL INSERT INTO #{self.class.table_name} (#{self.class.attributes_names_insert_sql}) VALUES (#{self.class.question_marks_insert_sql}) SQL #using splat operator to signify that there may be more than one argument in terms of attr_readers DB[:conn].execute(sql, *attribute_values_for_sql_check) #after inserting the card to the database, I want to get the primary key that is auto assigned to it #from sql and set it to the instance method 'id' of this very instance variable. self.id = DB[:conn].execute("SELECT last_insert_rowid() FROM #{self.class.table_name}")[0][0] #returns first array with the first value of the array (i.e. index 0) end
persisted?()
click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 184 def persisted? #the '!!' double bang converts object into a truthy value statement !!self.id end
save()
click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 172 def save #if the card has already been saved, then call update method persisted? ? update : insert #if not call insert method instead end
update()
click to toggle source
# File lib/mtg_card_finder/concerns/persistable.rb, line 189 def update #updates by the unique identifier of 'id' sql = <<-SQL UPDATE #{self.class.table_name} SET #{self.class.sql_columns_to_update} WHERE id=(?) SQL #using splat operator to signify that there may be more than one argument in terms of attr_readers DB[:conn].execute(sql, *attribute_values_for_sql_check, self.id) end