Class: Cure::Dsl::DslHandler

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/cure/dsl/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_trace, #log_warn

Constructor Details

#initialize(proc) ⇒ DslHandler

Returns a new instance of DslHandler.



31
32
33
# File 'lib/cure/dsl/template.rb', line 31

def initialize(proc)
  @proc = proc
end

Class Method Details

.init(&block) ⇒ Object



17
18
19
# File 'lib/cure/dsl/template.rb', line 17

def self.init(&block)
  DslHandler.new(block)
end

.init_from_content(dsl_source, identifier, line_number = 1) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cure/dsl/template.rb', line 21

def self.init_from_content(dsl_source, identifier, line_number=1)
  proc = Binding.get.eval(<<-SOURCE, identifier, line_number)
    Proc.new do
      #{dsl_source}
    end
  SOURCE

  DslHandler.new(proc)
end

Instance Method Details

#generate(instance_variables = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cure/dsl/template.rb', line 35

def generate(instance_variables={})
  dsl = Template.new
  instance_variables.each do |name, value|
    dsl.instance_variable_set("@#{name}", value)
  end

  dsl.instance_eval(&@proc)
  dsl

rescue StandardError => e
  log_error "Error parsing DSL: #{e.message}"

  # Raise specific error in future.
  raise e
end