Module: ToLua::Generator::GeneratorMethods::Hash

Included in:
Hash
Defined in:
lib/to_lua/generator.rb

Instance Method Summary collapse

Instance Method Details

#to_lua(state = nil) ⇒ Object



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
# File 'lib/to_lua/generator.rb', line 56

def to_lua(state = nil)
  state = State.from_state(state)
  depth = state.depth += 1

  fields = []

  self.each do |key, value|
    field  =  ''
    field  << state.indent * state.depth if state.pretty?
    field  << '['
    field  << key.to_s.to_lua
    field  << ']'
    field  << ' ' if state.pretty?
    field  << '='
    field  << ' ' if state.pretty?
    field  << value.to_lua(state)
    fields << field
  end

  depth = state.depth -= 1

  join_str = state.pretty? ? ",\n" : ','

  result =  '{'
  result << "\n" if state.pretty?
  result << fields.join(join_str)
  result << "\n" if state.pretty?
  result << state.indent * depth if state.pretty?
  result << '}'
  result
end