Class: Calyx::DataTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx.rb

Overview

The DataTemplate class was written by Tariq Ali in 2016, and licensed under the MIT License.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash = {}) ⇒ DataTemplate

Returns a new instance of DataTemplate.



184
185
186
187
188
189
190
191
192
# File 'lib/calyx.rb', line 184

def initialize(data_hash = {})
  data_hash.each do |key, value|
    self.define_singleton_method(:"#{key}") do
      value
    end
  end
  @narrative = []
  write_narrative
end

Instance Attribute Details

#narrativeObject (readonly)

Returns the value of attribute narrative.



182
183
184
# File 'lib/calyx.rb', line 182

def narrative
  @narrative
end

Instance Method Details

#conditional_write(condition, klass_a, klass_b = nil) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/calyx.rb', line 203

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

#resultObject



212
213
214
# File 'lib/calyx.rb', line 212

def result
  ERB.new(@narrative.join(" ")).result(binding)
end

#write(klass) ⇒ Object



199
200
201
# File 'lib/calyx.rb', line 199

def write(klass)
  @narrative << klass.new.generate
end

#write_narrativeObject



194
195
196
197
# File 'lib/calyx.rb', line 194

def write_narrative
  #user writes in what should happened next
  raise "There is no 'write_narrative' method present in your class."
end