toml-rb 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGE0NGNmZmVlODdlMTk3YTY3ZmJlZGIxM2JmNTM4MmYxZjU5ODcyYQ==
4
+ ODJhZWQzOTUxMzkwMDc3ODIxYzM0Zjg4ZTMwODU3YjY2NmM2Y2E4Mg==
5
5
  data.tar.gz: !binary |-
6
- MjhlMzA4YTJlZTlkOTFiMzI5Zjg3MzMyZGEzZTA1NDE3ODY3YmY3OQ==
6
+ NGQ4MmM2NDE2MmY3NGNhODM2ZDBiYmE5MmQ4YTk0MzZlYTU4ZjY3Zg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZWQxMTMwN2E2NjVlMzU0MGYwOTAwZjA1NTc0MzFiMjJkNjhjNDU3NzZlNGY5
10
- MzkyMDk4OWQ4NGNlYWJjNWVlYTI0ODNmNmNiMDM2NWIzY2E2YmI5NmFlNTkx
11
- NjVmZTYzMTFmNDRmZWVhM2JlMjUwZjgxZWE1NWFmMjczZTdkODA=
9
+ MWNjN2JhYTBhMDQ0ZDgzMDU1ZjVkMWQzY2Y2MDE0MmRjNmZiOGUwZjFkNzA2
10
+ M2RhMTBhMjgyMjQwMTE5ZmJlMzZjYmI1MzRjZTNiYWU3MzE4NjY4YmQzOWVh
11
+ ZDkzZTYxNjhhY2EwNzQ5YzQ2NzdmYjYzYTMwYmU0Y2E0OTVjZTg=
12
12
  data.tar.gz: !binary |-
13
- NmUzNTAwOWE5NmRkNjk2M2Y1NTgxYzBjOTYxOGVkZGMxY2VlNDA2Njc2Zjdm
14
- YTM3MTI5ZDcwMTMyNjNkOGZjMzc4ZWI4NmM2MjZkNDc5NDBiNDljNzI1MmZk
15
- NWQxYTQ4Mzc2NDBhYTA2N2U1YjdmOWY1YjFjYzJmN2IxYmVmZGU=
13
+ ZjA1MWEwMWY2YzA5OTNkYWE2MjFjNDNiNWY2ZmYyZGI5ZWM1Y2FhOTA1MGI2
14
+ M2I5ZjkxZmI1MTRjMzkxOGEyYmEyYzc4MGM0OTI1N2MyMjM2ZjJlYTU4MGQ4
15
+ OWQxYWJlZDhkNTVmYzBkNWE0OTFjMTAzMjFmYThiNGVhZjY2NzY=
data/init.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'citrus'
2
+
3
+ ROOT = File.dirname(File.expand_path(__FILE__))
4
+
5
+ require "#{ROOT}/lib/toml/string"
6
+ require "#{ROOT}/lib/toml/keyvalue"
7
+ require "#{ROOT}/lib/toml/keygroup"
8
+ require "#{ROOT}/lib/toml/parser"
9
+ require "#{ROOT}/lib/toml/dumper"
10
+
11
+ Citrus.load "#{ROOT}/lib/toml/grammars/helper.citrus"
12
+ Citrus.load "#{ROOT}/lib/toml/grammars/primitive.citrus"
13
+ Citrus.load "#{ROOT}/lib/toml/grammars/array.citrus"
14
+ Citrus.load "#{ROOT}/lib/toml/grammars/document.citrus"
15
+
data/lib/toml.rb CHANGED
@@ -53,4 +53,24 @@ module TOML
53
53
  def self.load_file(path, options = {})
54
54
  TOML.parse(File.read(path), options)
55
55
  end
56
+
57
+
58
+ # Public: Returns a *TOML* string from a Ruby Hash.
59
+ #
60
+ # hash - Ruby Hash to be dumped into *TOML*
61
+ #
62
+ #
63
+ # Examples
64
+ #
65
+ # TOML.dump({title: "TOML dump"})
66
+ # # => TODO:
67
+ #
68
+ # TOML.parse('title = "TOML parser"')
69
+ # # =>
70
+ #
71
+ #
72
+ # Returns a TOML string representing the hash.
73
+ def self.dump(hash)
74
+ Dumper.new(hash)
75
+ end
56
76
  end
@@ -0,0 +1,43 @@
1
+ module TOML
2
+ class Dumper
3
+ attr_reader :toml_str
4
+
5
+ def initialize(hash)
6
+ @toml_str = ""
7
+
8
+ visit(hash, '')
9
+ end
10
+
11
+ private
12
+
13
+ def visit(hash, prefix)
14
+ nested_pairs = []
15
+ simple_pairs = []
16
+
17
+ hash.keys.sort.each do |key|
18
+ val = hash[key]
19
+ (val.is_a?(Hash) ? nested_pairs : simple_pairs) << [key, val]
20
+ end
21
+
22
+ @toml_str += "[#{prefix}]\n" unless prefix.empty? || simple_pairs.empty?
23
+
24
+ # First add simple pairs, under the prefix
25
+ simple_pairs.each do |pair|
26
+ @toml_str << "#{pair[0].to_s} = #{to_toml(pair[1])}\n"
27
+ end
28
+
29
+ nested_pairs.each do |pair|
30
+ visit(pair[1], prefix.empty? ? pair[0].to_s : [prefix, pair[0]].join('.'))
31
+ end
32
+ end
33
+
34
+ def to_toml(obj)
35
+ case
36
+ when obj.is_a?(Time)
37
+ obj.strftime('%Y-%m-%dT%H:%M:%SZ')
38
+ else
39
+ obj.inspect
40
+ end
41
+ end
42
+ end
43
+ end
@@ -3,7 +3,7 @@ grammar Document
3
3
  include TomlArray
4
4
 
5
5
  rule document
6
- (comment | keygroup | keyvalue | "\n")*
6
+ (comment | keygroup | keyvalue | line_break)*
7
7
  end
8
8
 
9
9
  rule keygroup
@@ -1,6 +1,6 @@
1
1
  grammar Helper
2
2
  rule comment
3
- (space? "#" (~"\n")* "\n"?) { nil }
3
+ (space? "#" (~line_break)* line_break?) { nil }
4
4
  end
5
5
 
6
6
  rule space
@@ -8,6 +8,10 @@ grammar Helper
8
8
  end
9
9
 
10
10
  rule indent
11
- [ \t\n]*
11
+ [ \t\r\n]*
12
+ end
13
+
14
+ rule line_break
15
+ "\n" | "\r\n"
12
16
  end
13
17
  end
@@ -0,0 +1,58 @@
1
+ require_relative 'helper'
2
+
3
+ class DumperTest < Test::Unit::TestCase
4
+
5
+ def test_dump_empty
6
+ dumped = TOML.dump({})
7
+ assert_equal('', dumped.toml_str)
8
+ end
9
+
10
+ def test_dump_types
11
+ dumped = TOML.dump(string: %q(TOML "dump"))
12
+ assert_equal("string = \"TOML \\\"dump\\\"\"\n", dumped.toml_str)
13
+
14
+ dumped = TOML.dump(float: -13.24)
15
+ assert_equal("float = -13.24\n", dumped.toml_str)
16
+
17
+ dumped = TOML.dump(int: 1234)
18
+ assert_equal("int = 1234\n", dumped.toml_str)
19
+
20
+ dumped = TOML.dump(true: true)
21
+ assert_equal("true = true\n", dumped.toml_str)
22
+
23
+ dumped = TOML.dump(false: false)
24
+ assert_equal("false = false\n", dumped.toml_str)
25
+
26
+ dumped = TOML.dump(array: [1,2,3])
27
+ assert_equal("array = [1, 2, 3]\n", dumped.toml_str)
28
+
29
+ dumped = TOML.dump(array: [[1,2], ["weird", "one"]])
30
+ assert_equal("array = [[1, 2], [\"weird\", \"one\"]]\n", dumped.toml_str)
31
+
32
+ dumped = TOML.dump(datetime: Time.utc(1986,8,28,15,15))
33
+ assert_equal("datetime = 1986-08-28T15:15:00Z\n", dumped.toml_str)
34
+ end
35
+
36
+ def test_dump_nested_attributes
37
+ hash = {nested: {hash: { deep: true}}}
38
+ dumped = TOML.dump(hash)
39
+ assert_equal("[nested.hash]\ndeep = true\n",
40
+ dumped.toml_str)
41
+
42
+ hash[:nested].merge!(other: 12)
43
+ dumped = TOML.dump(hash)
44
+ assert_equal("[nested]\nother = 12\n[nested.hash]\ndeep = true\n",
45
+ dumped.toml_str)
46
+
47
+ hash[:nested].merge!(nest: {again: 'it never ends'})
48
+ dumped = TOML.dump(hash)
49
+ toml = "[nested]\n" +
50
+ "other = 12\n" +
51
+ "[nested.hash]\n" +
52
+ "deep = true\n" +
53
+ "[nested.nest]\n" +
54
+ "again = \"it never ends\"\n"
55
+
56
+ assert_equal(toml, dumped.toml_str)
57
+ end
58
+ end
data/test/example.toml CHANGED
@@ -25,9 +25,6 @@ enabled = true
25
25
  ip = "10.0.0.2"
26
26
  dc = "eqdc10"
27
27
 
28
- [servers]
29
- amount = 2
30
-
31
28
  [clients]
32
29
  data = [ ["gamma", "delta"], [1, 2] ]
33
30
 
data/test/toml_test.rb CHANGED
@@ -23,7 +23,6 @@ class TomlTest < Test::Unit::TestCase
23
23
  },
24
24
 
25
25
  "servers" => {
26
- "amount" => 2,
27
26
  "alpha" => {
28
27
  "ip" => "10.0.0.1",
29
28
  "dc" => "eqdc10"
@@ -88,7 +87,6 @@ class TomlTest < Test::Unit::TestCase
88
87
  },
89
88
 
90
89
  servers: {
91
- amount: 2,
92
90
  alpha: {
93
91
  ip: "10.0.0.1",
94
92
  dc: "eqdc10"
@@ -107,4 +105,9 @@ class TomlTest < Test::Unit::TestCase
107
105
 
108
106
  assert_equal(hash, parsed)
109
107
  end
108
+
109
+ def test_line_break
110
+ parsed = TOML.parse("hello = 'world'\r\nline_break = true")
111
+ assert_equal({'hello' => 'world', 'line_break' => true}, parsed)
112
+ end
110
113
  end
data/toml-rb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'toml-rb'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.2'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = "TOML parser in ruby, for ruby."
6
6
  s.description = "A TOML parser using Citrus parsing library. Formerly known as 'toml_parser-ruby'. "
@@ -15,7 +15,8 @@ Gem::Specification.new do |s|
15
15
  "lib/**/*.rb",
16
16
  "lib/**/*.citrus",
17
17
  "*.gemspec",
18
- "test/*.*"
18
+ "test/*.*",
19
+ "init.rb"
19
20
  ]
20
21
 
21
22
  s.add_dependency "citrus"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toml-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emiliano Mancuso
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: citrus
@@ -35,6 +35,7 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - README.md
37
37
  - Rakefile
38
+ - lib/toml/dumper.rb
38
39
  - lib/toml/keygroup.rb
39
40
  - lib/toml/keyvalue.rb
40
41
  - lib/toml/parser.rb
@@ -46,11 +47,13 @@ files:
46
47
  - lib/toml/grammars/primitive.citrus
47
48
  - toml-rb.gemspec
48
49
  - toml_parser-ruby.gemspec
50
+ - test/dumper_test.rb
49
51
  - test/example.toml
50
52
  - test/grammar_test.rb
51
53
  - test/hard_example.toml
52
54
  - test/helper.rb
53
55
  - test/toml_test.rb
56
+ - init.rb
54
57
  homepage: http://github.com/eMancu/toml-rb
55
58
  licenses:
56
59
  - MIT