Class: Angie::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/angie/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, &block) ⇒ DSL



5
6
7
8
9
10
# File 'lib/angie/dsl.rb', line 5

def initialize(parent = nil, &block)
  @config = []
  @parent = parent
  @indentation = parent ? parent.indentation + 1 : 0
  instance_exec(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
29
30
31
# File 'lib/angie/dsl.rb', line 25

def method_missing(name, *args, &block)
  if block_given?
    block(name.to_s, &block)
  else
    directive(name.to_s, *args)
  end
end

Instance Method Details

#block(name, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/angie/dsl.rb', line 16

def block(name, &block)
  @config << "#{indent}#{name} {"
  if block_given?
    child = Angie::DSL.new(self, &block)
    @config.concat(child.config)
  end
  @config << "#{indent}}"
end

#configObject



37
38
39
# File 'lib/angie/dsl.rb', line 37

def config
  @config
end

#directive(name, *args) ⇒ Object



12
13
14
# File 'lib/angie/dsl.rb', line 12

def directive(name, *args)
  @config << "#{indent}#{name} #{args.join(' ')};"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



33
34
35
# File 'lib/angie/dsl.rb', line 33

def respond_to_missing?(method_name, include_private = false)
  true
end

#to_sObject



41
42
43
# File 'lib/angie/dsl.rb', line 41

def to_s
  @config.join("\n") unless @parent
end