-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path8_25.rb
91 lines (75 loc) · 2.56 KB
/
8_25.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Bicycle
attr_reader :size, :parts
def initialize(size:, parts:)
@size = size
@parts = 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
class Part
attr_reader :name, :description, :needs_spare
def initialize(name:, description:, needs_spare: true)
@name = name
@description = description
@needs_spare = needs_spare
end
end
################
module PartsFactory
def self.build(config:,
part_class: Part,
parts_class: Parts)
parts_class.new(
config.collect {|part_config|
part_class.new(
name: part_config[0],
description: part_config[1],
needs_spare: part_config.fetch(2, true))})
end
end
################
road_config =
[['chain', '11-speed'],
['tire_size', '23'],
['tape_color', 'red']]
mountain_config =
[['chain', '11-speed'],
['tire_size', '2.1'],
['front_shock', 'Manitou'],
['rear_shock', 'Fox', false]]
puts PartsFactory.build(config: road_config).inspect
# => #<Parts:0x00007f83f0046bd8 @parts=[#<Part:0x00007f83f0046d40 @name="chain", @description="11-speed", @needs_spare=true>, #<Part:0x00007f83f0046cc8 @name="tire_size", @description="23", @needs_spare=true>, #<Part:0x00007f83f0046c28 @name="tape_color", @description="red", @needs_spare=true>]>
# to get it nicely formated, check if you regenerate
# => #<Parts:0x007fb903932718 @parts=[
# #<Part:0x007fb9039328a8
# @name="chain",
# @description="11-speed",
# @needs_spare=true>,
# #<Part:0x007fb903932808
# @name="tire_size",
# etc...
puts PartsFactory.build(config: mountain_config).inspect
# => #<Parts:0x00007f83f00464d0 @parts=[#<Part:0x00007f83f00466d8 @name="chain", @description="11-speed", @needs_spare=true>, #<Part:0x00007f83f0046638 @name="tire_size", @description="2.1", @needs_spare=true>, #<Part:0x00007f83f0046598 @name="front_shock", @description="Manitou", @needs_spare=true>, #<Part:0x00007f83f0046520 @name="rear_shock", @description="Fox", @needs_spare=false>]>
# to get it nicely formated, check if you regenerate
# => #<Parts:0x007fb903931f98 @parts=[
# #<Part:0x007fb9039321c8
# @name="chain",
# @description="11-speed",
# @needs_spare=true>,
# #<Part:0x007fb903932150
# @name="tire_size",
# etc...