-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_east_injection_chain_tale.rb
112 lines (91 loc) · 2.9 KB
/
06_east_injection_chain_tale.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'pry'
require './05_east_injection_tale'
# Sandi Metz - Somethin is Nothing
# https://www.youtube.com/watch?v=OMPfEXIlTVE
# https://www.sandimetz.com/blog/2014/9/9/shape-at-the-bottom-of-all-things
class VerseTextTemplate
def recite_tale(tale)
recite_verses(tale, tale.phrases.count)
self
end
def recite_verses(tale, number)
puts (1..number).map {|i| "VERSE: #{i}\n#{build_verse(tale, i)}"}.
join(tale.verses_joiner)
self
end
def recite_verse(tale, number)
puts "VERSE: #{number}\n#{build_verse(tale, number)}"
self
end
private
attr_reader :phrases, :phrase_joiner, :verses_joiner, :verse_intro, :verse_closing
def build_phrase(tale, number)
tale.phrases.last(number).join(tale.phrase_joiner)
end
def build_verse(tale, number)
"#{tale.verse_intro}#{build_phrase(tale, number)}#{tale.verse_closing}"
end
end
class EastTale
def initialize(**args)
@phrases = args[:phrases] || PHRASES
@phrase_joiner = args[:phrase_joiner] || PHRASE_JOINER
@verses_joiner = args[:verses_joiner] || VERSES_JOINER
@verse_intro = args[:verse_intro] || VERSE_INTRO
@verse_closing = args[:verse_closing] || VERSE_CLOSE
end
def recite_tale(template: VerseTextTemplate.new)
template.recite_tale(tail_values)
self
end
def recite_verses(number, template: VerseTextTemplate.new)
template.recite_verses(tail_values, number)
self
end
def recite_verse(number, template: VerseTextTemplate.new)
template.recite_verse(tail_values, number)
self
end
private
attr_reader :phrases, :phrase_joiner, :verses_joiner, :verse_intro, :verse_closing
# create a value object
require 'ostruct'
def tail_values
OpenStruct.new(phrases: phrases, phrase_joiner: phrase_joiner,
verses_joiner: verses_joiner,
verse_intro: verse_intro, verse_closing: verse_closing)
end
end
# open/closed using composition/injection
class InjectedEastTale < EastTale
def initialize(**args)
order = args.delete(:order) || Order::Default
effect = args.delete(:effect) || Effect::Default
phrases = args[:phrases] || PHRASES
recite_order = order.call(phrases)
args[:phrases] = effect.call(recite_order)
super(args)
end
end
InjectedEastTale.new.recite_tale
puts "*" * 10
InjectedEastTale.new.recite_tale
puts "#" * 30
InjectedEastTale.new(order: Order::Random).recite_tale
puts "#" * 30
InjectedEastTale.new(effect: Effect::Echo).recite_tale
puts "#" * 30
InjectedEastTale.new( order: Order::Random,
effect: Effect::Echo ).recite_tale
puts "#" * 30
InjectedEastTale.new( order: Order.new('random'),
effect: Effect.new('echo') ).recite_tale
# EasternTale.new.recite_verses(2)
#
# puts "*" * 10
# EasternTale.new.recite_verse(3)
#
# puts "*" * 10
# puts "*" * 10
# tale = EasternTale.new
# tale.recite_verses(4).recite_verse(5)