-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.rb
43 lines (38 loc) · 1.01 KB
/
strategy.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
# Strategy pattern is used for implementing each version of algorythm as a separate object
require 'active_support/inflector'
class CV
attr_accessor :subject, :skills, :generator
def initialize(subject, options)
options[:skills] ||= []
options[:generator] ||= :pdf
@subject = subject
@skills = options[:skills]
@generator = "#{options[:generator]}_generator".to_s.classify.constantize.new
end
def generate
@generator.generate(self)
end
end
class PdfGenerator
def generate(context)
puts '--------- PDF ----------'
puts context.subject
context.skills.each_with_index do |skill, i|
puts "Skill ##{i}: #{skill}"
end
nil
end
end
class DocGenerator
def generate(context)
puts '--------- DOC ----------'
puts context.subject
context.skills.each_with_index do |skill, i|
puts "* Skill ##{i}: #{skill}"
end
nil
end
end
cv = CV.new('Jon Snow', skills: ['javascript', 'archery', 'swords'])
cv.generator = DocGenerator.new
puts cv.generate