Class: Dslh

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

Defined Under Namespace

Classes: Scope, ScopeBlock

Constant Summary collapse

INDENT_SPACES =
'  '
VERSION =
'0.2.3'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dslh

of class methods



40
41
42
43
44
# File 'lib/dslh.rb', line 40

def initialize(options = {})
  @options = options.dup
  @options[:key_conv] ||= @options[:conv]
  @options[:value_conv] ||= @options[:conv]
end

Class Method Details

.deval(hash, options = {}, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/dslh.rb', line 31

def deval(hash, options = {}, &block)
  if [hash, options].all? {|i| not i.kind_of?(Hash) }
    raise TypeError, "wrong argument type #{options.class} (expected Hash)"
  end

  self.new(options).deval(hash, &block)
end

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dslh.rb', line 9

def 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

#deval(hash) ⇒ Object



68
69
70
71
72
# File 'lib/dslh.rb', line 68

def deval(hash)
  buf = StringIO.new
  deval0(hash, 0, buf)
  buf.string
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dslh.rb', line 46

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

  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