Class: ZanTools::Generatable::Configuration

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

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



13
14
15
# File 'lib/zan_tools/generatable.rb', line 13

def initialize
  @config = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zan_tools/generatable.rb', line 21

def method_missing(name, *args)
  matches = name.match(/([^=]+)(=)?/)
  # 判断是否为赋值或取值
  if !matches.nil? && !matches[2].nil? && args.size == 1
    raise "preserved name `#{matches[1]}` is invalid" if respond_to?(matches[1])
    @config[matches[1]] = args[0]
  elsif !matches.nil? && matches[2].nil? && @config.key?(matches[1])
    @config[matches[1]]
  else
    super
  end
end

Instance Method Details

#compile(tmpl) ⇒ Object



34
35
36
37
38
39
# File 'lib/zan_tools/generatable.rb', line 34

def compile(tmpl)
  template = ERB.new(tmpl, nil, '-')
  template.result(binding)
rescue StandardError => e
  raise TemplateError.new("#{e.class} - #{e.message}")
end

#each(&block) ⇒ Object



17
18
19
# File 'lib/zan_tools/generatable.rb', line 17

def each(&block)
  @config.each(&block)
end