Class: GemHadar::TemplateCompiler
- Inherits:
-
Object
- Object
- GemHadar::TemplateCompiler
- Includes:
- Tins::BlockSelf, Tins::MethodMissingDelegator::DelegatorModule
- Defined in:
- lib/gem_hadar/template_compiler.rb
Instance Method Summary collapse
- #compile(src, dst) ⇒ Object
-
#initialize(&block) ⇒ TemplateCompiler
constructor
A new instance of TemplateCompiler.
- #method_missing(id, *a, &b) ⇒ Object
Constructor Details
#initialize(&block) ⇒ TemplateCompiler
Returns a new instance of TemplateCompiler.
7 8 9 10 11 |
# File 'lib/gem_hadar/template_compiler.rb', line 7 def initialize(&block) super block_self(&block) @values = {} instance_eval(&block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *a, &b) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/gem_hadar/template_compiler.rb', line 22 def method_missing(id, *a, &b) if a.empty? && id && @values.key?(id) @values[id] elsif a.size == 1 @values[id] = a.first else super end end |
Instance Method Details
#compile(src, dst) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/gem_hadar/template_compiler.rb', line 13 def compile(src, dst) template = File.read(src) File.open(dst, 'w') do |output| erb = ERB.new(template, nil, '-') erb.filename = src.to_s output.write erb.result binding end end |