Last active
January 3, 2016 22:09
-
-
Save jwo/8526339 to your computer and use it in GitHub Desktop.
RubyOffRails - January 2014 Office Hours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Automobile | |
@@autos = [] | |
def self.count | |
@@autos.count | |
end | |
def self.autos_for_model(model_name) | |
@@autos.select{|auto| auto.model == model_name} | |
end | |
def self.search(args) | |
# model: honda | |
# color: red | |
results = args.map do |key, value| | |
@@autoss.select!{|auto| auto.send(key) == value.to_sym} | |
end | |
## We have an array of arrays, which will keep a red toyota since red matches | |
## only keep autos if they are in every result | |
results.flatten.uniq.select do |auto| | |
results.all?{|result| result.include? auto} | |
end | |
end | |
def self.register(automobile) | |
@@autos << automobile | |
end | |
attr_accessor :model, :color | |
def initialize(args) | |
@model = args.fetch(:model) | |
@color = args.fetch(:color) | |
Automobile.register(self) | |
end | |
def self.search(args) | |
Automobile.search(args) | |
end | |
end | |
Automobile.new model: :mazda, color: :red | |
Automobile.new model: :toyota, color: :red | |
Automobile.new model: :honda, color: :red | |
Automobile.new model: :mazda, color: :blue | |
puts Automobile.search("color"=> "red").inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### We ended up with a global singleton object, AutomobileRecords | |
### I like to think of this as a database, really. | |
require 'singleton' | |
class AutomobileRecords | |
include Singleton | |
attr_reader :autos | |
def initialize | |
@autos = [] | |
end | |
def count | |
autos.count | |
end | |
def autos_for_model(model_name) | |
autos.select{|auto| auto.model == model_name} | |
end | |
def search(args) | |
results = autos | |
args.each do |key, value| | |
results.select!{|auto| auto.send(key) == value.to_sym} | |
end | |
results | |
end | |
def register(automobile) | |
autos << automobile | |
end | |
end | |
class Automobile | |
attr_accessor :model, :color | |
def initialize(args) | |
@model = args.fetch(:model) | |
@color = args.fetch(:color) | |
AutomobileRecords.instance.register(self) | |
end | |
def self.search(args) | |
AutomobileRecords.instance.search(args) | |
end | |
end | |
Automobile.new model: :mazda, color: :red | |
Automobile.new model: :toyota, color: :red | |
Automobile.new model: :honda, color: :red | |
Automobile.new model: :mazda, color: :blue | |
puts Automobile.search("color"=> "red").inspect | |
puts AutomobileRecords.instance == AutomobileRecords.instance | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Automobile | |
@@autos = [] | |
def self.count | |
@@autos.count | |
end | |
def self.autos_for_model(model_name) | |
@@autos.select{|auto| auto.model == model_name} | |
end | |
def self.search(args) | |
# model: honda | |
# color: red | |
results = @@autos | |
args.each do |key, value| | |
results.select!{|auto| auto.send(key) == value.to_sym} | |
end | |
results | |
end | |
def self.register(automobile) | |
@@autos << automobile | |
end | |
attr_accessor :model, :color | |
def initialize(args) | |
@model = args.fetch(:model) | |
@color = args.fetch(:color) | |
Automobile.register(self) | |
end | |
def self.search(args) | |
Automobile.search(args) | |
end | |
end | |
Automobile.new model: :mazda, color: :red | |
Automobile.new model: :toyota, color: :red | |
Automobile.new model: :honda, color: :red | |
Automobile.new model: :mazda, color: :blue | |
puts Automobile.search("color"=> "red", model: :toyota).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment