-
-
Save tra38/20bea744a037c6619f60 to your computer and use it in GitHub Desktop.
This is a class.
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 DataTemplate | |
attr_reader :data, :narrative | |
def initialize(data_hash) | |
#code written with help from jon2992 (IRC name: aegis3121) | |
data_hash.each do |key, value| | |
self.instance_variable_set("@#{key}", value) | |
self.class.send :attr_reader, key | |
end | |
@narrative = [] | |
write_narrative | |
end | |
def write_narrative | |
#user writes in what should happened next | |
raise "There is no 'write_narrative' method present in your class." | |
end | |
def write(klass) | |
@narrative << klass.new.generate | |
end | |
def conditional_write(condition, klass_a, klass_b = nil) | |
if condition | |
@narrative << klass_a.new.generate | |
elsif klass_b | |
@narrative << klass_b.new.generate | |
else | |
end | |
end | |
def publish | |
outline = @narrative.join(" ") | |
insert_data(outline) | |
end | |
private | |
def insert_data(outline) | |
outline = ERB.new(outline) | |
outline.result(binding) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment