Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am alexgriff on github.
  • I am alexgriff (https://keybase.io/alexgriff) on keybase.
  • I have a public key ASA-QiayPEVGEAvYns_HyF6m9UNcJbP4gYNxm_26EFr6RQo

To claim this, I am signing this object:

# write a method 'get_names' that receives an array of instructors:
instructors = [{name: 'terrance', hometown: 'nj', mood: 'excited'}, {name: 'daniel', hometown: 'philadelphia', mood: 'refreshed'}, {name: 'alex', hometown: 'upstate ny', mood: 'good' }]
def get_names(instructors)
end
# the method should return an array of just the instructors names
@alexgriff
alexgriff / google_books_adapter.rb
Created August 22, 2017 16:29
Adapter Pattern
# How to think about the Single Responsibility Principle in your application...
# What part of your app has the responsibility of making the request to the API
# and turning the response into Ruby Objects & records in your database...?
# Not really the Models...
# ...And you may not want all the code that does this just intertwined within the logic of your app
# ... so where to put this code?
# That's what the Adapter Pattern is for.
# Add your code here
def count_letters(sentence)
end
# count_letters("hello hi")
#=> {'h'=> 2, "e"=> 1, "l"=> 2, "o" => 1, " "=> 1, "i"=> 1}
def has_2?(collection, element)
array = [[1],[2],[3]]
# 1 will it error? / name that error!
# 1a.
array.first + 1
# 1b.
array.first.first + 2
# 1c.
# - create a candidate
# --------------------
obama = Candidate.create(first_name: "Barack", last_name: "Obama", birthdate:"08-04-1961",
position_running_for: "President", national_candidate: true)
# (0.1ms) begin transaction
# SQL (0.3ms) INSERT INTO "candidates" ("first_name", "last_name", "birthdate", "position_running_for", "national_candidate") VALUES (?, ?, ?, ?, ?) [["first_name", "Barack"], ["last_name", "Obama"], ["birthdate", "1961-04-08"], ["position_running_for", "President"], ["national_candidate", "t"]]
# (1.4ms) commit transaction
# => #<Candidate id: 1544, position_running_for: "President", first_name: "Barack", last_name: "Obama",
# birthdate: "1961-04-08", national_candidate: true >
# Voter, the polymorphic model
class Voter < ActiveRecord::Base
belongs_to :voterable, polymorphic: true
has_many :votes
has_many :candidates, through: :votes
end
# Citizen, has one voter as voterable
# this is a new model where many of the attributes
class MakeVoterPolymorphic < ActiveRecord::Migration
def change
add_column :voters, :voterable_id, :integer
add_column :voters, :voterable_type, :string
end
end
# you could also use a shortened syntax that would do the same thing
# and add these two columns to the table
candidate = FactoryGirl.create :candidate, :with_many_voters
# => <Candidate id: 1534, position_running_for: "President", first_name: "Rudolph",
# last_name: "Carter", personality_type: "Brave, Hasty",
# bio: "Ut molestiae maiores non corporis. Voluptatem eum ...", political_views: nil, national_candidate: true>
candidate.votes
# => #<ActiveRecord::Associations::CollectionProxy
# [#<Vote id: 1544, voter_id: 556, candidate_id: 1534>,
# #<Vote id: 1545, voter_id: 557, candidate_id: 1534>, ...
# #<Vote id: 1553, voter_id: 565, candidate_id: 1534>]>