Class: XeroMin::Erb

Inherits:
Object
  • Object
show all
Defined in:
lib/xero-min/erb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_dir = nil) ⇒ Erb

Returns a new instance of Erb.



11
12
13
14
# File 'lib/xero-min/erb.rb', line 11

def initialize(template_dir=nil)
  self.template_dir = template_dir || File.expand_path('../templates', __FILE__)
  self.post_processing_proc = lambda {|xml| xml.gsub(%r((\s*)<), '<')}
end

Instance Attribute Details

#post_processing_procObject

Public : post processing for raw erb default compacts xml, removing n and spaces



9
10
11
# File 'lib/xero-min/erb.rb', line 9

def post_processing_proc
  @post_processing_proc
end

#template_dirObject

Public : dir where templates are looked for



6
7
8
# File 'lib/xero-min/erb.rb', line 6

def template_dir
  @template_dir
end

Instance Method Details

#infered_template(sym) ⇒ Object



24
25
26
# File 'lib/xero-min/erb.rb', line 24

def infered_template(sym)
  "#{sym}.xml.erb"
end

#render(locals = {}) ⇒ Object

Public : renders a single entity with an infered template eg : render(contact: ‘me’) will render ##template_dir/contact.xml.erb with ‘me’ as lvar



18
19
20
21
22
# File 'lib/xero-min/erb.rb', line 18

def render(locals={})
  erb = ERB.new(read_template(infered_template(locals.keys.first)))
  inject_locals(locals)
  post_processing_proc.call(self.instance_eval {erb.result binding})
end