Module: YourDSL

Defined in:
lib/yourdsl.rb

Defined Under Namespace

Classes: Expression, Output, Scope

Constant Summary collapse

VERSION =
'0.7.1'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yourdsl.rb', line 31

def method_missing(sym, *args, &block)
  caller[0] =~ (/(.*):(.*):in?/)
  file, lineno = $1, $2
  self.file = file

  if !@@only.empty? && !@@only.include?(sym)
    fail(NoMethodError, sym.to_s)
  end
  if !@@exclude.empty? && @@exclude.include?(sym)
    fail(NoMethodError, sym.to_s)
  end

  args = (args.length == 1 ? args.first : args)
  @current_scope ||= Scope.new([])
  @current_scope.expressions << Expression.new(sym, args, lineno)
  if block
    # there is some simpler recursive way of doing this, will fix it shortly
    if @@remember_blocks_starting_with.include? sym
      @current_scope.expressions.last.proc = block
    else
      nest(&block)
    end
  end
end

Instance Method Details

#file=(file) ⇒ Object



24
25
26
27
28
29
# File 'lib/yourdsl.rb', line 24

def file=(file)
  unless @@file
    @@file = file
    @@output.file = @@file
  end
end

#outputObject



17
18
19
20
21
22
# File 'lib/yourdsl.rb', line 17

def output
  if @current_scope
    @@output.expressions = @current_scope.expressions
  end
  @@output
end

#record_your_dsl(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/yourdsl.rb', line 8

def record_your_dsl(opts = {})
  @@remember_blocks_starting_with = Array(opts[:retain_blocks_for])
  @@only = Array(opts[:only])
  @@exclude = Array(opts[:except])
  @@output = Output.new
  @@file = nil
  @stack = []
end