Module: Persistable::ClassMethods
- Included in:
- ModernFall, ModernRise, StandardFall, StandardRise
- Defined in:
- lib/mtg_card_finder/concerns/persistable.rb
Instance Method Summary collapse
- #attributes_names_insert_sql ⇒ Object
- #buy_link(id) ⇒ Object
-
#create(attributes_hash) ⇒ Object
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.
- #create_sql ⇒ Object
- #create_table ⇒ Object
- #find(id) ⇒ Object
- #make_csv_file ⇒ Object
- #question_marks_insert_sql ⇒ Object
-
#reify_from_row(row) ⇒ Object
opposite of abstraction is reification i.e.
- #remove_table ⇒ Object
- #sql_columns_to_update ⇒ Object
- #table_empty? ⇒ Boolean
- #table_name ⇒ Object
- #table_rows ⇒ Object
Instance Method Details
#attributes_names_insert_sql ⇒ Object
150 151 152 153 |
# 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 |
#buy_link(id) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 90 def buy_link(id) name = self.find(id) begin #collect the instant's values as a string word = name.card + " " + name.sets rescue #instead of getting an undefined method error in .card & .sets for nil:NilClass #just re-run method until user sets it to a true value Parser.purchase else #replace whitespace chars word.gsub!(/\s+/m, '%20') #create url for purchasing the chosen id card buy = "http://www.ebay.com/sch/?_nkw=#{word}&_sacat=0".fg COLORS[3] puts "" puts "Please highlight, right click and copy the #{"url".fg COLORS[3]} below and paste it to your preferred browser:" puts "--------------------------------------------------------------------------------------------" puts "" puts buy puts "" end end |
#create(attributes_hash) ⇒ Object
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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
144 145 146 147 148 |
# 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 ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 28 def create_table sql = " CREATE TABLE IF NOT EXISTS \#{self.table_name} ( \#{self.create_sql} )\n SQL\n\n DB[:conn].execute(sql)\nend\n" |
#find(id) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 113 def find(id) sql = " SELECT * FROM \#{self.table_name} WHERE id=(?)\n SQL\n\n row = DB[:conn].execute(sql, id)\n #if a row is actually returned i.e. the id actually exists\n if row.first\n self.reify_from_row(row.first)\n #using .first array method to return only the first nested array\n #that is taken from self.reify_from_row(row) which is the resulting id of the query\n else\n puts \"This card does not exist\"\n end\nend\n" |
#make_csv_file ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 ⇒ Object
155 156 157 158 159 160 |
# 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) ⇒ Object
opposite of abstraction is reification i.e. I’m getting the raw data of these variables
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# 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 ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 36 def remove_table sql = " DROP TABLE IF EXISTS \#{self.table_name}\n SQL\n\n DB[:conn].execute(sql)\nend\n" |
#sql_columns_to_update ⇒ Object
162 163 164 165 166 167 |
# 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? ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 44 def table_empty? sql = " SELECT * FROM \#{self.table_name}\n SQL\n\n table = DB[:conn].execute(sql)\n check = table.empty?\n if check == true\n true\n end\nend\n" |
#table_name ⇒ Object
6 7 8 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 6 def table_name "#{self.to_s.downcase}s" end |
#table_rows ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/mtg_card_finder/concerns/persistable.rb', line 56 def table_rows sql = " SELECT COUNT(*) FROM \#{self.table_name}\n SQL\n\n table = DB[:conn].execute(sql)\n rows = table.flatten.join.to_i\n rows\nend\n" |