to_lua 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 788d72b7334d20049dc49d469132b848617c9307
4
- data.tar.gz: f865e91cb845f1ca85fc0fab501f495aa1011977
3
+ metadata.gz: 243a62236ad1a235e6e17c87acdd108df8385842
4
+ data.tar.gz: 5ca7abf2a66c25faf24ac64858c99c4b379eb1d2
5
5
  SHA512:
6
- metadata.gz: 3b22748559a85c9f2e77a58cf041c68ae87da6efc7f5cdbd6a343daa5ada514f7b49763e668dfe2c79157505bcb43f10461883a30c4433caaa69c2f3276d0a3a
7
- data.tar.gz: 5baf0f0e092430b092dbdb27a076d2ddd121b85ff731972dda6f58f858713ceaa36044c90c86b67b83d6d50aa744c221e28c4fab89cd0c2f5105450e8444ea07
6
+ metadata.gz: 653c74c6e83183f99798ff68ab4204fcaeae59071b695b13642be537abc7037affc4c7244d49ad10d264382065c1ec7fc1ad08d36de0c1ec49406e828aa9d8ed
7
+ data.tar.gz: cc0540385ff5b6319db07509d28bb75712455341b7686a44ba5b39ffb0ad718ce6bb414ad1659c23b02063044f1be5f405a56f34b72d6f47683f56a80214b527
@@ -0,0 +1,146 @@
1
+ require_relative 'helpers'
2
+
3
+ module ToLua
4
+ module Generator
5
+ class State
6
+ attr_accessor :depth, :indent
7
+
8
+ def self.from_state(opts)
9
+ case
10
+ when self === opts
11
+ opts
12
+ when opts.respond_to?(:to_hash)
13
+ new(opts.to_hash)
14
+ when opts.respond_to?(:to_h)
15
+ new(opts.to_h)
16
+ else
17
+ new
18
+ end
19
+ end
20
+
21
+ def initialize(opts = {})
22
+ @pretty = false
23
+ @depth = 0
24
+ @indent = ' '
25
+ configure(opts)
26
+ end
27
+
28
+ def configure(opts)
29
+ @pretty = opts[:pretty] if opts.key?(:pretty)
30
+ @indent = opts[:indent] if opts.key?(:indent)
31
+ end
32
+
33
+ def pretty?
34
+ @pretty
35
+ end
36
+
37
+ def to_h
38
+ {
39
+ pretty: @pretty,
40
+ indent: @indent,
41
+ depth: @depth
42
+ }
43
+ end
44
+
45
+ def ==(other)
46
+ if other.is_a? State
47
+ self.to_h == other.to_h
48
+ else
49
+ false
50
+ end
51
+ end
52
+ end
53
+
54
+ module GeneratorMethods
55
+ module Hash
56
+ def to_lua(state = nil)
57
+ state = State.from_state(state)
58
+ depth = state.depth += 1
59
+
60
+ fields = []
61
+
62
+ self.each do |key, value|
63
+ field = ''
64
+ field << state.indent * state.depth if state.pretty?
65
+ field << '['
66
+ field << key.to_s.to_lua
67
+ field << ']'
68
+ field << ' ' if state.pretty?
69
+ field << '='
70
+ field << ' ' if state.pretty?
71
+ field << value.to_lua(state)
72
+ fields << field
73
+ end
74
+
75
+ depth = state.depth -= 1
76
+
77
+ join_str = state.pretty? ? ",\n" : ','
78
+
79
+ result = '{'
80
+ result << "\n" if state.pretty?
81
+ result << fields.join(join_str)
82
+ result << "\n" if state.pretty?
83
+ result << state.indent * depth if state.pretty?
84
+ result << '}'
85
+ result
86
+ end
87
+ end
88
+
89
+ module Array
90
+ def to_lua(state = nil)
91
+ state = State.from_state(state)
92
+ depth = state.depth += 1
93
+
94
+ fields = []
95
+
96
+ self.each do |value|
97
+ field = ''
98
+ field << state.indent * state.depth if state.pretty?
99
+ field << value.to_lua(state)
100
+ fields << field
101
+ end
102
+
103
+ depth = state.depth -= 1
104
+
105
+ join_str = state.pretty? ? ",\n" : ','
106
+
107
+ result = '{'
108
+ result << "\n" if state.pretty?
109
+ result << fields.join(join_str)
110
+ result << "\n" if state.pretty?
111
+ result << state.indent * depth if state.pretty?
112
+ result << '}'
113
+ result
114
+ end
115
+ end
116
+
117
+ module String
118
+ def to_lua(*)
119
+ '"' + ToLua::Helpers.encode_string(self) + '"'
120
+ end
121
+ end
122
+
123
+ module NilClass
124
+ def to_lua(*)
125
+ 'nil'
126
+ end
127
+ end
128
+
129
+ module ToString
130
+ def to_lua(*)
131
+ self.to_s
132
+ end
133
+ end
134
+
135
+ module Object
136
+ def to_lua(state = nil)
137
+ if self.respond_to? :as_lua
138
+ self.as_lua.to_lua(state)
139
+ else
140
+ self.to_s.to_lua(state)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -1,37 +1,37 @@
1
- module ToLua
2
- class Helpers
3
- LUA_C_STYLE_ESCAPES = {
4
- "\a" => "\\a",
5
- "\b" => "\\b",
6
- "\f" => "\\f",
7
- "\n" => "\\n",
8
- "\r" => "\\r",
9
- "\t" => "\\t",
10
- "\v" => "\\v",
11
- "\\" => "\\\\",
12
- "\"" => "\\\"",
13
- "'" => "\\'",
14
- "[" => "\\[",
15
- "]" => "\\]"
16
- }.freeze
17
-
18
- def self.encode_string(string)
19
- encoded = []
20
-
21
- string.each_char do |char|
22
- if LUA_C_STYLE_ESCAPES[char]
23
- encoded << LUA_C_STYLE_ESCAPES[char]
24
- elsif char =~ /^[^[:print:]]$/
25
- char.each_byte do |byte|
26
- encoded << '\\'
27
- encoded << byte.to_s
28
- end
29
- else
30
- encoded << char
31
- end
32
- end
33
-
34
- encoded.join
35
- end
36
- end
37
- end
1
+ module ToLua
2
+ class Helpers
3
+ LUA_C_STYLE_ESCAPES = {
4
+ "\a" => "\\a",
5
+ "\b" => "\\b",
6
+ "\f" => "\\f",
7
+ "\n" => "\\n",
8
+ "\r" => "\\r",
9
+ "\t" => "\\t",
10
+ "\v" => "\\v",
11
+ "\\" => "\\\\",
12
+ "\"" => "\\\"",
13
+ "'" => "\\'",
14
+ "[" => "\\[",
15
+ "]" => "\\]"
16
+ }.freeze
17
+
18
+ def self.encode_string(string)
19
+ encoded = []
20
+
21
+ string.each_char do |char|
22
+ if LUA_C_STYLE_ESCAPES[char]
23
+ encoded << LUA_C_STYLE_ESCAPES[char]
24
+ elsif char =~ /^[^[:print:]]$/
25
+ char.each_byte do |byte|
26
+ encoded << '\\'
27
+ encoded << byte.to_s
28
+ end
29
+ else
30
+ encoded << char
31
+ end
32
+ end
33
+
34
+ encoded.join
35
+ end
36
+ end
37
+ end
data/lib/to_lua.rb CHANGED
@@ -1,29 +1,33 @@
1
- require_relative 'to_lua/extensions'
2
-
3
- class Hash
4
- include ToLua::Extensions::Hash
5
- end
6
-
7
- class Array
8
- include ToLua::Extensions::Array
9
- end
10
-
11
- class String
12
- include ToLua::Extensions::String
13
- end
14
-
15
- class NilClass
16
- include ToLua::Extensions::NilClass
17
- end
18
-
19
- class Numeric
20
- include ToLua::Extensions::ToString
21
- end
22
-
23
- class TrueClass
24
- include ToLua::Extensions::ToString
25
- end
26
-
27
- class FalseClass
28
- include ToLua::Extensions::ToString
29
- end
1
+ require_relative 'to_lua/generator'
2
+
3
+ class Hash
4
+ include ToLua::Generator::GeneratorMethods::Hash
5
+ end
6
+
7
+ class Array
8
+ include ToLua::Generator::GeneratorMethods::Array
9
+ end
10
+
11
+ class String
12
+ include ToLua::Generator::GeneratorMethods::String
13
+ end
14
+
15
+ class NilClass
16
+ include ToLua::Generator::GeneratorMethods::NilClass
17
+ end
18
+
19
+ class Numeric
20
+ include ToLua::Generator::GeneratorMethods::ToString
21
+ end
22
+
23
+ class TrueClass
24
+ include ToLua::Generator::GeneratorMethods::ToString
25
+ end
26
+
27
+ class FalseClass
28
+ include ToLua::Generator::GeneratorMethods::ToString
29
+ end
30
+
31
+ class Object
32
+ include ToLua::Generator::GeneratorMethods::Object
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_lua
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mantas Norvaiša
@@ -17,7 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/to_lua.rb
20
- - lib/to_lua/extensions.rb
20
+ - lib/to_lua/generator.rb
21
21
  - lib/to_lua/helpers.rb
22
22
  homepage: https://github.com/mntnorv/to_lua
23
23
  licenses:
@@ -29,18 +29,19 @@ require_paths:
29
29
  - lib
30
30
  required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - '>='
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.4.1
42
+ rubygems_version: 2.4.5
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Serialize objects to lua tables
46
46
  test_files: []
47
+ has_rdoc:
@@ -1,51 +0,0 @@
1
- require_relative 'helpers'
2
-
3
- module ToLua
4
- module Extensions
5
- module Hash
6
- def to_lua
7
- fields = []
8
-
9
- self.each do |key, value|
10
- field = '['
11
- field << key.to_s.to_lua
12
- field << ']='
13
- field << value.to_lua
14
- fields << field
15
- end
16
-
17
- '{' + fields.join(',') + '}'
18
- end
19
- end
20
-
21
- module Array
22
- def to_lua
23
- fields = []
24
-
25
- self.each do |value|
26
- fields << value.to_lua
27
- end
28
-
29
- '{' + fields.join(',') + '}'
30
- end
31
- end
32
-
33
- module String
34
- def to_lua
35
- '"' + ToLua::Helpers.encode_string(self) + '"'
36
- end
37
- end
38
-
39
- module NilClass
40
- def to_lua
41
- 'nil'
42
- end
43
- end
44
-
45
- module ToString
46
- def to_lua
47
- self.to_s
48
- end
49
- end
50
- end
51
- end