Skip to content

Instantly share code, notes, and snippets.

@rjswenson
Last active January 2, 2016 06:09
Show Gist options
  • Save rjswenson/8261809 to your computer and use it in GitHub Desktop.
Save rjswenson/8261809 to your computer and use it in GitHub Desktop.
My responses to Elastic Suite questions.
# Q1: Given this scenario, how would you go about returning the answer of life anytime the method answer_to_life is called?
# https://gist.github.com/7e676b9a9e6c9d47c319
# The cleanest way to have all classes you mentioned (which of course are objects) possess the same method is to find a common
# superclass they share, from which they inherit. Since we are dealing with a miriad of Classes (NilClass, String, FixNum) they
# all share the superclass of 'Class' so I modified it to contain the method.
class Object
def answer_to_life
42
end
end
p 1234234.answer_to_life
# => 42
p 12.45.answer_to_life
# => 42
p "hello".answer_to_life
# => 42
p NilClass.answer_to_life
# => 42
# Q2: Given the User.include? method, list 2 ways you have to override this method to include your own implementation of it?
# First: Write a new file with the class User with the method, this will override any previous method of the same name.
class User
def self.include?
p "YES, I totally include!"
end
end
$: User.include?
=> "Yes, I totally include!"
# Second: Write a module that modifies the behavior and include it within the User class.
# The module include (extended into User for a CLASS method) will overwrite it's own method of the same name
class User
def self.include?
p "YES, I totally include!"
end
end
module UsefulMethods
def self.included(base)
base.instance_eval do
def self.include?
p "I am so including it!"
end
end
end
end
$: User.send(:include, UsefulMethods) # Send allows for calling of private methods.
$: User.include?
=> "I am so including it!"
# Q3: If you were to implement a news feed (ex: Facebook, Twitter, etc) How would you go about implementing/architecting one
# (you can choose the features you want to implement: following, users, etc; let me know what you implement in your answer)?
# Models: NewsFeed, Users, Posts, Relationships(followed, followers)
# Features: users, posting, following/followed, timestamps
class User
has_many :posts, dependent: :destroy
has_one :newsfeed, dependent: :destroy
has_many :followed_users, through: :relationships, source: "followed_id"
has_many :relationships, foreign_key: "follower_id"
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
end
class NewsFeed
belongs_to :user, dependent: :destroy
end
class Relationship
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end
class Post
belongs_to :user
end
"TABLES:
Users
name
email
password
timestamps
Posts
user_id
content
timestamps
Relationships
follower_id
followed_id
timestamps
NewsFeeds
user_id
To populate our Newsfeed, we take our user, find his followers, and then their posts to display.
We will need quite a few indexes between all the foreign keys for this to increase database speed."
# Q4: Make this test pass. Attach your solution to your reply.
# https://gist.github.com/a4df63b0cf89ce945ca4
class Author
attr_accessor :records
def initialize(records)
@records = records
end
# get all titles of all books for all authors
def titles
all_titles = Array.new
# This method traverses through the array/hash/array/hash setup for records and pulls only titles.
# Its only problem is nested statements, making it O(n^2) worst case access.
records.each do |chunk|
chunk[:books].each do |hash|
all_titles << hash[:title]
end
end
all_titles
end
end
describe Author do
describe '#title' do
it "should get all the titles for all the authors" do
authors = [
{
:name => "Dave Thomas",
:books => [
{
:title => "Programming Ruby 1.9",
:isbn => "978-1-93435-608-1"
},
{
:title => "Agile Web Development with Rails",
:isbn => "978-0-9776-1663-3"
}
],
},
{
:name => "Mike Clark",
:books => [
{
:title => "Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps",
:isbn => "978-0-9787-3922-5"
}
]
}
]
all_titles = [
"Programming Ruby 1.9",
"Agile Web Development with Rails",
"Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps"
]
author_model = Author.new(authors)
author_model.titles.should == all_titles
end
end
end
# Q5: Given this (models.rb shows the models you have, along with their relations, data.csv shows the input data,
# along with a header row), write a script that will take the data.csv file and create the proper instances of models.
# For example, when your script is done running, there should be two Products; the product with product_number 7012765
# should have two Variations (with variation_codes of 790 and 670), and each of those variations should have six StockItems
# (with stock_item_names 1, 2, 3, 4, 5, and 6).
# https://gist.github.com/timting/5c85ff3655ccec7e94fb
# I'm using this as a rake command
require 'csv'
namespace :q5_csv_import do
task :migrate_data => :environment do
csv = CSV.read(file, col_sep: ",", headers: true )
csv.each do |row|
product = Product.find(product_number: row[0]) || Product.new(product_number: row[0]).save
variation = Variation.find(variation_code: row[1]) || Variation.new(product_id: product.id,
variation_code: row[1]).save
stock_item = StockItem.find(stock_item_name: row[2]) || StockItem.new(variation_id: variation.id,
stock_item_name: row[2]).save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment