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.3.9'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dslh

of class methods



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dslh.rb', line 40

def initialize(options = {})
  @options = {
    :time_inspecter => method(:inspect_time),
    :dump_old_hash_array_format => false,
    :force_dump_braces => false,
    :use_braces_instead_of_do_end => false,
  }.merge(options)

  @options[:key_conv] ||= (@options[:conv] || proc {|i| i.to_s })
  @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



90
91
92
93
94
95
# File 'lib/dslh.rb', line 90

def deval(hash)
  buf = StringIO.new
  depth = @options[:initial_depth] || 0
  deval0(hash, depth, buf)
  buf.string
end

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



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
86
87
88
# File 'lib/dslh.rb', line 52

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]

  (@options[:scope_vars] || {}).each do |name, value|
    scope.instance_variable_set("@#{name}", value)
  end

  if @options[:ignore_methods]
    ignore_methods = Array(@options[:ignore_methods])

    ignore_methods.each do |method_name|
      scope.instance_eval(<<-EOS)
        def #{method_name}(*args, &block)
          method_missing(#{method_name.to_s.inspect}, *args, &block)
        end
      EOS
    end
  end

  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