Class: GemHadar::TemplateCompiler

Inherits:
Object
  • Object
show all
Includes:
Tins::BlockSelf, Tins::MethodMissingDelegator::DelegatorModule
Defined in:
lib/gem_hadar.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ TemplateCompiler

Returns a new instance of TemplateCompiler.



773
774
775
776
777
# File 'lib/gem_hadar.rb', line 773

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



788
789
790
791
792
793
794
795
796
# File 'lib/gem_hadar.rb', line 788

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



779
780
781
782
783
784
785
786
# File 'lib/gem_hadar.rb', line 779

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