Class: Dslh

Inherits:
Object
  • Object
show all
Defined in:
lib/dslh.rb,
lib/dslh/version.rb

Defined Under Namespace

Classes: Scope

Constant Summary collapse

VERSION =
'0.1.4'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dslh

Returns a new instance of Dslh.



26
27
28
# File 'lib/dslh.rb', line 26

def initialize(options = {})
  @options = options.dup
end

Class Method Details

.eval(expr_or_options = nil, options = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dslh.rb', line 4

def self.eval(expr_or_options = nil, options = nil, &block)
  if options and not options.kind_of?(Hash)
    raise TypeError, "wrong argument type #{options.class} (expected Hash)"
  end

  expr = nil
  options ||= {}

  if expr_or_options
    case expr_or_options
    when String
      expr = expr_or_options
    when Hash
      options.update(expr_or_options)
    else
      raise TypeError, "wrong argument type #{expr_or_options.class} (expected String or Hash)"
    end
  end

  self.new(options).eval(expr, &block)
end

Instance Method Details

#eval(expr = nil, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dslh.rb', line 30

def eval(expr = nil, &block)
  retval = {}
  scope = Scope.new
  scope.instance_variable_set(:@__options__, @options)
  scope.instance_variable_set(:@__hash__, retval)

  if expr
    eval_args = [expr]

    [:filename, :lineno].each do |k|
      eval_args << @options[k] if @options[k]
    end

    scope.instance_eval(*eval_args)
  else
    scope.instance_eval(&block)
  end

  return retval
end