Class: Rezept::DSLContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rezept/dsl_context.rb

Instance Method Summary collapse

Constructor Details

#initializeDSLContext

Returns a new instance of DSLContext.



5
6
7
8
9
# File 'lib/rezept/dsl_context.rb', line 5

def initialize
  @docs = []
  @templates = {}
  @context = Hashie::Mash.new()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/rezept/dsl_context.rb', line 17

def method_missing(method_name, *args, &block)
  if [:Automation, :Command, :Policy].include?(method_name)
    hash = dslh_eval(block)
    hash['name'] = args[0]
    hash['document_type'] = method_name.to_s
    @docs << hash
  else
    super
  end
end

Instance Method Details

#contextObject



32
33
34
# File 'lib/rezept/dsl_context.rb', line 32

def context
  @context
end

#dslh_eval(block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rezept/dsl_context.rb', line 48

def dslh_eval(block)
  scope_hook = proc do |scope|
    scope.instance_eval("      def include_template(template_name, context = {})\n        tmplt = @templates[template_name.to_s]\n\n        unless tmplt\n          raise \"Template '\#{template_name}' is not defined\"\n        end\n\n        context_orig = @context\n        @context = @context.merge(context)\n        instance_eval(&tmplt)\n        @context = context_orig\n      end\n\n      def context\n        @context\n      end\n\n      def __dsl(&block)\n        @__hash__ = JSON.generate(Dslh::ScopeBlock.nest(binding, 'block'))\n      end\n\n      def __script(str)\n        str.split(/\\R/)\n      end\n\n      def __script_file(file)\n        File.read(file).split(/\\R/)\n      end\n    EOS\n  end\n\n  scope_vars = {templates: @templates, context: @context}\n\n  Dslh.eval(allow_empty_args: true, scope_hook: scope_hook, scope_vars: scope_vars, &block)\nend\n")

#eval_dsl(dsl_file) ⇒ Object



11
12
13
14
15
# File 'lib/rezept/dsl_context.rb', line 11

def eval_dsl(dsl_file)
  @_dsl_file = dsl_file
  instance_eval(File.read(dsl_file), dsl_file)
  @docs
end

#require(file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rezept/dsl_context.rb', line 36

def require(file)
  docfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@_dsl_file), file))

  if File.exist?(docfile)
    instance_eval(File.read(docfile), docfile)
  elsif File.exist?(docfile + '.rb')
    instance_eval(File.read(docfile + '.rb'), docfile + '.rb')
  else
    Kernel.require(file)
  end
end

#template(name, &block) ⇒ Object



28
29
30
# File 'lib/rezept/dsl_context.rb', line 28

def template(name, &block)
  @templates[name.to_s] = block
end