Created
May 20, 2019 12:09
-
-
Save vestige/08ac834e63cbe00d69ab73cbbcdf8162 to your computer and use it in GitHub Desktop.
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 Bicycle | |
attr_reader :size, :parts | |
def initialize(args={}) | |
@size = args[:size] | |
@parts = args[:parts] | |
end | |
def spares | |
parts.spares | |
end | |
end | |
require 'forwardable' | |
class Parts | |
extend Forwardable | |
def_delegators :@parts, :size, :each | |
include Enumerable | |
def initialize(parts) | |
@parts = parts | |
end | |
def spares | |
select{|part| part.needs_spare} | |
end | |
end | |
require 'ostruct' | |
module PartsFactory | |
def self.build(config, parts_class = Parts) | |
parts_class.new( | |
config.collect {|part_config| | |
create_part(part_config) | |
} | |
) | |
end | |
def self.create_part(part_config) | |
OpenStruct.new( | |
name: part_config[0], | |
description: part_config[1], | |
needs_spare: part_config.fetch(2, true) | |
) | |
end | |
end | |
road_config = [ | |
['chain', '10-speed'], | |
['tire_size', '23'], | |
['tape_color', 'red'] | |
] | |
mountain_config = [ | |
['chain', '10-speed'], | |
['tire_size', '2.1'], | |
['front_shock', 'Manitou', false], | |
['rear_shock', 'Fox'] | |
] | |
road_bike = Bicycle.new( | |
size: 'L', | |
parts: PartsFactory.build(road_config) | |
) | |
pp road_bike.spares | |
mountain_bike = Bicycle.new( | |
size: 'L', | |
parts: PartsFactory.build(mountain_config) | |
) | |
p '----------' | |
pp mountain_bike.spares | |
recumbent_config = [ | |
['chain', '9-speed'], | |
['tire_size', '28'], | |
['flag', 'tall and orange'] | |
] | |
recumbent_bike = Bicycle.new( | |
size: 'L', | |
parts: PartsFactory.build(recumbent_config) | |
) | |
p '----------' | |
pp recumbent_bike.spares |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment