Module: Templatar::ModelAdditions

Defined in:
lib/templatar/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#has_template(options = {}) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/templatar/model_additions.rb', line 4

def has_template(options = {})
  raise StandardError.new('Cannot add has_template to a non-AR model') unless self.respond_to?(:column_names)

  self.send(:define_method, :template?) { @templatar }

  custom_methods = options.fetch(:methods, [])

  metaclass = class << self; self; end
  metaclass.send(:define_method, :template) do
    @templatar_singleton ||= begin
      t = self.new
      t.instance_variable_set :@templatar, true
      t_metaclass = class << t; self; end
      (self.column_names + custom_methods).each do |getter|
        t_metaclass.send(:define_method, getter) { getter.to_sym == :id ? '__ID__' : "#{getter}__TEMPLATE__" }
      end
      t
    end
  end
end