ltsv 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -1
- data/lib/ltsv.rb +8 -4
- data/spec/ltsv_spec.rb +14 -8
- metadata +2 -2
data/README.md
CHANGED
@@ -67,7 +67,23 @@ Dumped objects should respond to :to_hash.
|
|
67
67
|
|
68
68
|
* Contributors
|
69
69
|
* Naoto SHINGAKI <https://github.com/naoto/>
|
70
|
-
*
|
70
|
+
* Chezou <https://github.com/chezou>
|
71
|
+
* Masato Ikeda <https://github.com/a2ikm>
|
72
|
+
|
73
|
+
### History
|
74
|
+
|
75
|
+
* 2013/02/12 0.1.0
|
76
|
+
Thanks to Masato Ikeda.
|
77
|
+
parse(String) method now accepts multi-line string and returns an Array of Hash. for single line String, use the new parse_line method.
|
78
|
+
* 2013/02/11 0.0.3
|
79
|
+
Thanks to Chezou.
|
80
|
+
* Added the specs for load() method.
|
81
|
+
* Fixed the bug when handling empty keys or values.
|
82
|
+
* 2013/02/08 0.0.2
|
83
|
+
Bug Fix for parse_io() internal method, which affects the behaviour when parse() method receives an IO instance for the first argument.
|
84
|
+
Thanks to Naoto Shingaki.
|
85
|
+
* 2013/02/07 0.0.1
|
86
|
+
First Release.
|
71
87
|
|
72
88
|
## Contributing
|
73
89
|
|
data/lib/ltsv.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
#
|
5
5
|
#
|
6
6
|
module LTSV
|
7
|
-
VERSION = "0.0
|
7
|
+
VERSION = "0.1.0"
|
8
8
|
|
9
9
|
# Parsing given stream or string.
|
10
10
|
# If you specified a stream as the first argument,
|
@@ -86,14 +86,18 @@ module LTSV
|
|
86
86
|
private
|
87
87
|
|
88
88
|
def parse_io(io, options)#:nodoc:
|
89
|
-
io.map{|l|
|
89
|
+
io.map{|l|parse_line l.chomp, options}
|
90
90
|
end
|
91
91
|
|
92
92
|
def parse_string(string, options)#:nodoc:
|
93
|
+
string.chomp.split($/).map{|l|parse_line l, options}
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse_line(line, options)#:nodoc:
|
93
97
|
symbolize_keys = options.delete(:symbolize_keys)
|
94
98
|
symbolize_keys = true if symbolize_keys.nil?
|
95
99
|
|
96
|
-
|
100
|
+
line.split("\t").inject({}) do |h, i|
|
97
101
|
(key, value) = i.split(':', 2)
|
98
102
|
next unless key
|
99
103
|
key = key.to_sym if symbolize_keys
|
@@ -137,7 +141,7 @@ module LTSV
|
|
137
141
|
value
|
138
142
|
end
|
139
143
|
|
140
|
-
module_function :load, :parse, :dump, :parse_io, :parse_string, :unescape!, :escape
|
144
|
+
module_function :load, :parse, :dump, :parse_io, :parse_string, :parse_line, :unescape!, :escape
|
141
145
|
|
142
146
|
class <<self
|
143
147
|
private :parse_io, :parse_string, :unescape!, :escape
|
data/spec/ltsv_spec.rb
CHANGED
@@ -4,35 +4,41 @@ describe LTSV do
|
|
4
4
|
|
5
5
|
describe :parse do
|
6
6
|
context 'String argument' do
|
7
|
-
it 'can parse labeled tab separated values into hash' do
|
7
|
+
it 'can parse labeled tab separated values into a hash in an array' do
|
8
8
|
line = "label1:value1\tlabel2:value2"
|
9
|
-
LTSV.parse(line).should == {:label1 => 'value1', :label2 => 'value2'}
|
9
|
+
LTSV.parse(line).should == [{:label1 => 'value1', :label2 => 'value2'}]
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'can parse multiline labeled tab separated values into hashes in an array' do
|
13
|
+
line = "label1:value1\tlabel2:value2\nlabel1:value3\tlabel2:value4"
|
14
|
+
LTSV.parse(line).should ==
|
15
|
+
[{:label1 => 'value1', :label2 => 'value2'}, {:label1 => 'value3', :label2 => 'value4'}]
|
10
16
|
end
|
11
17
|
|
12
18
|
it 'can parse the value that contains escape sequences' do
|
13
19
|
|
14
20
|
LTSV.parse("label1:value1\tlabel2:value\\nvalue").should ==
|
15
|
-
{:label1 => 'value1', :label2 => "value\nvalue"}
|
21
|
+
[{:label1 => 'value1', :label2 => "value\nvalue"}]
|
16
22
|
|
17
23
|
LTSV.parse("label1:value1\tlabel2:value\\rvalue").should ==
|
18
|
-
{:label1 => 'value1', :label2 => "value\rvalue"}
|
24
|
+
[{:label1 => 'value1', :label2 => "value\rvalue"}]
|
19
25
|
|
20
26
|
LTSV.parse("label1:value1\tlabel2:value\\tvalue").should ==
|
21
|
-
{:label1 => 'value1', :label2 => "value\tvalue"}
|
27
|
+
[{:label1 => 'value1', :label2 => "value\tvalue"}]
|
22
28
|
|
23
29
|
LTSV.parse("label1:value1\tlabel2:value\\\\value").should ==
|
24
|
-
{:label1 => 'value1', :label2 => "value\\value"}
|
30
|
+
[{:label1 => 'value1', :label2 => "value\\value"}]
|
25
31
|
end
|
26
32
|
|
27
33
|
it 'parses the value as-is when the backslash with a following ordinal character' do
|
28
34
|
|
29
35
|
LTSV.parse("label1:value1\tlabel2:value\\avalue").should ==
|
30
|
-
{:label1 => 'value1', :label2 => "value\\avalue"}
|
36
|
+
[{:label1 => 'value1', :label2 => "value\\avalue"}]
|
31
37
|
end
|
32
38
|
|
33
39
|
it 'parses the empty value field as nil' do
|
34
40
|
LTSV.parse("label1:\tlabel2:value2").should ==
|
35
|
-
{:label1 => nil, :label2 => 'value2'}
|
41
|
+
[{:label1 => nil, :label2 => 'value2'}]
|
36
42
|
end
|
37
43
|
end
|
38
44
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ltsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|