Skip to content

Instantly share code, notes, and snippets.

@dsasse07
dsasse07 / literally_defined_methods.rb
Last active Dec 29, 2020
Example of literally defined aggregate methods for use in blog post.
View literally_defined_methods.rb
def self.count_vhs_by_rentals
Rental.all.each_with_object({}) do |rental, vhs_hash|
vhs_hash[rental.vhs].nil? ? vhs_hash[rental.vhs] = 1 : vhs_hash[rental.vhs] += 1
end
end
#=> {<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
def self.count_vhs_by_client
@dsasse07
dsasse07 / send_example.rb
Created Dec 28, 2020
Basic send method examples
View send_example.rb
“hello”.capitalize #=> “Hello”
“hello”.send(“capitalize”) #=> “Hello”
@dsasse07
dsasse07 / rental_aggregate_refactored.rb
Last active Sep 3, 2021
aggregate methods refactored using .send
View rental_aggregate_refactored.rb
def self.count_vhs_rentals_by(attribute)
Rental.all.each_with_object({}) do |rental, vhs_hash|
vhs_hash[rental.send(#{attribute}”)].nil? ? vhs_hash[rental.“#{attribute}”] = 1 : vhs_hash[rental.“#{attribute}”] += 1
end
end
Vhs.count_vhs_rentals_by(“vhs”) #=>
#{<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
@dsasse07
dsasse07 / abstraction_to_module.rb
Created Dec 28, 2020
Refactoring and abstracting a method to a module for use between multiple classes
View abstraction_to_module.rb
module GenericHelpers
def make_hash_by_attribute(array, attribute)
array.each_with_object({}) do |array_item, new_hash|
new_hash[array_item.send("#{attribute}")].nil? ? new_hash[array_item.send("#{attribute}")] = 1 : new_hash[array_item.send("#{attribute}")] += 1
end
end
end
@dsasse07
dsasse07 / module_inclusion.rb
Last active Dec 28, 2020
Calling refactored method from a module
View module_inclusion.rb
class Vhs
include GenericHelpers
extend GenericHelpers
def self.count_vhs_by_rentals
make_hash_by_attribute(Rental.all, "vhs")
end
def self.count_vhs_by_client
make_hash_by_attribute(Rental.all, "client")
View module_inclusion2.rb
class Client
include GenericHelpers
extend GenericHelpers
def self.count_clients_by_age
make_hash_by_attribute(Client.all, "age")
end
end
@dsasse07
dsasse07 / AR_group_and_count.rb
Created Dec 28, 2020
ActiveRecord group by count
View AR_group_and_count.rb
Rental.group(:vhs).count #=>
#{<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
@dsasse07
dsasse07 / twilio_sms_snippet.rb
Created Jan 10, 2021
Twilio Send SMS code snippet from Twilio CodeExchange
View twilio_sms_snippet.rb
require 'twilio-ruby'
account_sid = 'ACcd207c4be71151a3f50ef05c79e865d5'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyy'
client = Twilio::REST::Client.new(account_sid, auth_token)
from = '+15551234567' # Your Twilio number
to = '+15555555555' # The mobile number the text will be sent to
client.messages.create(
@dsasse07
dsasse07 / Twilio_module.rb
Created Jan 10, 2021
Incorporating Twilio send SMS code snippet into a module for inclusion in necessary classes.
View Twilio_module.rb
require 'dotenv/load' #This ruby gem will is what we will be using to secure our Twilio credentials
require 'twilio-ruby'
module TwilioControls
# To set up environmental variables, see http://twil.io/secure
@@account_sid = ENV['TWILIO_ACCOUNT_SID'] #This is used in conjuction with the 'dotenv/load' gem
@@auth_token = ENV['TWILIO_AUTH_TOKEN'] #This is used in conjuction with the 'dotenv/load' gem
@@client = Twilio::REST::Client.new(@@account_sid, @@auth_token)
@dsasse07
dsasse07 / .env
Created Jan 10, 2021
Setting up .env for Twilio integration
View .env
export TWILIO_ACCOUNT_SID='AC###################################'
export TWILIO_AUTH_TOKEN='################################'
#your account_sid and auth_token can be found on your Twilio console at twilio.com/console