rvs 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.
- data/MIT-LICENSE +20 -0
- data/lib/rvs/parse.rb +102 -0
- data/lib/rvs/to_rvs.rb +76 -0
- data/lib/rvs.rb +2 -0
- data/test/rvs.dt.rb +70 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2012 Makani Mason, Kem Mason
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/rvs/parse.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'strscan'
|
|
2
|
+
require 'bigdecimal'
|
|
3
|
+
|
|
4
|
+
module RVS
|
|
5
|
+
class Parser
|
|
6
|
+
def initialize(str)
|
|
7
|
+
@scan = StringScanner.new(str)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def run
|
|
11
|
+
parse_item
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def parse_item
|
|
15
|
+
next_char = @scan.peek(1)
|
|
16
|
+
if next_char == '['
|
|
17
|
+
parse_array
|
|
18
|
+
elsif next_char == '{'
|
|
19
|
+
parse_hash
|
|
20
|
+
else
|
|
21
|
+
parse_value
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def parse_array
|
|
26
|
+
# throw away the opening [
|
|
27
|
+
@scan.getch
|
|
28
|
+
|
|
29
|
+
retval = []
|
|
30
|
+
while !@scan.eos?
|
|
31
|
+
next_char = @scan.peek(1)
|
|
32
|
+
if next_char == ']'
|
|
33
|
+
# throw away the closing ]
|
|
34
|
+
@scan.getch
|
|
35
|
+
break
|
|
36
|
+
end
|
|
37
|
+
# throw away the ,
|
|
38
|
+
@scan.getch if next_char == ','
|
|
39
|
+
retval.push(parse_item)
|
|
40
|
+
end
|
|
41
|
+
retval
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def parse_hash
|
|
45
|
+
# throw away the opening {
|
|
46
|
+
@scan.getch
|
|
47
|
+
|
|
48
|
+
retval = {}
|
|
49
|
+
curr_key = nil
|
|
50
|
+
while !@scan.eos?
|
|
51
|
+
next_char = @scan.peek(1)
|
|
52
|
+
if next_char == '}'
|
|
53
|
+
# throw away the closing }
|
|
54
|
+
@scan.getch
|
|
55
|
+
break
|
|
56
|
+
end
|
|
57
|
+
# throw away the comma , or >
|
|
58
|
+
@scan.getch if [',','>'].include?(next_char)
|
|
59
|
+
|
|
60
|
+
next_value = parse_item
|
|
61
|
+
if curr_key
|
|
62
|
+
retval[curr_key] = next_value
|
|
63
|
+
curr_key = nil
|
|
64
|
+
else
|
|
65
|
+
curr_key = next_value
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
retval
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def parse_value
|
|
72
|
+
type_identifier = @scan.getch
|
|
73
|
+
if type_identifier == 'b'
|
|
74
|
+
@scan.getch == '1'
|
|
75
|
+
elsif type_identifier == 'd'
|
|
76
|
+
Date.strptime(get_chars(8), '%Y%m%d')
|
|
77
|
+
elsif type_identifier == 'e'
|
|
78
|
+
DateTime.strptime(get_chars(14), '%Y%m%d%H%M%S')
|
|
79
|
+
elsif type_identifier == 't'
|
|
80
|
+
Time.strptime(get_chars(14), '%Y%m%d%H%M%S')
|
|
81
|
+
elsif type_identifier == 's'
|
|
82
|
+
get_chars(@scan.scan_until(/:/).chop.to_i)
|
|
83
|
+
elsif type_identifier == 'w'
|
|
84
|
+
@scan.scan(/\d+/).to_i
|
|
85
|
+
elsif type_identifier == 'c'
|
|
86
|
+
BigDecimal(@scan.scan(/[0-9\.]+/))
|
|
87
|
+
else
|
|
88
|
+
raise "unexpected type identifier"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def get_chars(count)
|
|
93
|
+
retval = @scan.peek(count)
|
|
94
|
+
@scan.pos = @scan.pos + count
|
|
95
|
+
retval
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def self.parse(str)
|
|
100
|
+
Parser.new(str).run
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/rvs/to_rvs.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require 'bigdecimal'
|
|
2
|
+
require 'date'
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
class String
|
|
6
|
+
def to_rvs
|
|
7
|
+
"s#{size}:#{self}"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module WholeNumberToRvs
|
|
12
|
+
def to_rvs
|
|
13
|
+
"w#{self}"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Fixnum
|
|
18
|
+
include WholeNumberToRvs
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Bignum
|
|
22
|
+
include WholeNumberToRvs
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class FalseClass
|
|
26
|
+
def to_rvs
|
|
27
|
+
'b0'
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class TrueClass
|
|
32
|
+
def to_rvs
|
|
33
|
+
'b1'
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class BigDecimal
|
|
38
|
+
def to_rvs
|
|
39
|
+
"c#{to_s('F')}"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class Date
|
|
44
|
+
def to_rvs
|
|
45
|
+
"d#{strftime('%Y%m%d')}"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class DateTime
|
|
50
|
+
def to_rvs
|
|
51
|
+
"e#{strftime('%Y%m%d%H%M%S')}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class Time
|
|
56
|
+
def to_rvs
|
|
57
|
+
"t#{strftime('%Y%m%d%H%M%S')}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
class Array
|
|
62
|
+
def to_rvs
|
|
63
|
+
inner = collect {|elem| elem.to_rvs }.join(',')
|
|
64
|
+
"[#{inner}]"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class Hash
|
|
69
|
+
def to_rvs
|
|
70
|
+
ary = []
|
|
71
|
+
each_pair do |key, value|
|
|
72
|
+
ary.push("#{key.to_rvs}>#{value.to_rvs}")
|
|
73
|
+
end
|
|
74
|
+
"{#{ary.join(',')}}"
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/rvs.rb
ADDED
data/test/rvs.dt.rb
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require_relative '../lib/rvs/parse'
|
|
2
|
+
require_relative '../lib/rvs/to_rvs'
|
|
3
|
+
|
|
4
|
+
class Test_rvs < DohTest::TestGroup
|
|
5
|
+
def verify(original_obj, expected_str)
|
|
6
|
+
actual_str = original_obj.to_rvs
|
|
7
|
+
assert_equal(expected_str, actual_str)
|
|
8
|
+
recreated_obj = RVS::parse(actual_str)
|
|
9
|
+
assert_equal(original_obj, recreated_obj)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_whole_numbers
|
|
13
|
+
fixnum = 1000
|
|
14
|
+
assert(fixnum.is_a?(Fixnum))
|
|
15
|
+
verify(fixnum, 'w1000')
|
|
16
|
+
|
|
17
|
+
bignum = 9999999999999999999999999999999
|
|
18
|
+
assert(bignum.is_a?(Bignum))
|
|
19
|
+
verify(bignum, 'w9999999999999999999999999999999')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_boolean
|
|
23
|
+
verify(true, 'b1')
|
|
24
|
+
verify(false, 'b0')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_bigdecimal
|
|
28
|
+
verify(BigDecimal('100'), 'c100.0')
|
|
29
|
+
verify(BigDecimal('100.14'), 'c100.14')
|
|
30
|
+
verify(BigDecimal('100.143912981212381923'), 'c100.143912981212381923')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_date
|
|
34
|
+
verify(Date.new(2012,2,9), 'd20120209')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_datetime
|
|
38
|
+
verify(DateTime.new(2012,2,9,1,5,7), 'e20120209010507')
|
|
39
|
+
verify(DateTime.new(2012,2,9,17,39,45), 'e20120209173945')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_time
|
|
43
|
+
verify(Time.new(2012,2,9,1,5,7), 't20120209010507')
|
|
44
|
+
verify(Time.new(2012,2,9,17,39,45), 't20120209173945')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_array
|
|
48
|
+
verify([1, 2], '[w1,w2]')
|
|
49
|
+
verify([1, [2,3]], '[w1,[w2,w3]]')
|
|
50
|
+
verify([1, [2,[3,4]]], '[w1,[w2,[w3,w4]]]')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_hash
|
|
54
|
+
verify({1 => 2}, '{w1>w2}')
|
|
55
|
+
verify({1 => 2, 3 => 4}, '{w1>w2,w3>w4}')
|
|
56
|
+
verify({1 => 2, 3 => {4 => 5}}, '{w1>w2,w3>{w4>w5}}')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_mixed
|
|
60
|
+
verify({1 => ['blah'], ['blee'] => {4 => 5}}, '{w1>[s4:blah],[s4:blee]>{w4>w5}}')
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_string
|
|
64
|
+
verify('', 's0:')
|
|
65
|
+
verify('blah', 's4:blah')
|
|
66
|
+
verify('blahblee', 's8:blahblee')
|
|
67
|
+
verify('blahb\'lee', "s9:blahb'lee")
|
|
68
|
+
verify('blahb\'le"e', %q{s10:blahb'le"e})
|
|
69
|
+
end
|
|
70
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rvs
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Makani Mason
|
|
9
|
+
- Kem Mason
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2012-02-23 00:00:00.000000000Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: dohutil
|
|
17
|
+
requirement: &70365557731000 !ruby/object:Gem::Requirement
|
|
18
|
+
none: false
|
|
19
|
+
requirements:
|
|
20
|
+
- - ! '>='
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 0.1.4
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: *70365557731000
|
|
26
|
+
description: serialization of common ruby value classes to ascii text
|
|
27
|
+
email:
|
|
28
|
+
- devinfo@atpsoft.com
|
|
29
|
+
executables: []
|
|
30
|
+
extensions: []
|
|
31
|
+
extra_rdoc_files:
|
|
32
|
+
- MIT-LICENSE
|
|
33
|
+
files:
|
|
34
|
+
- lib/rvs/parse.rb
|
|
35
|
+
- lib/rvs/to_rvs.rb
|
|
36
|
+
- lib/rvs.rb
|
|
37
|
+
- test/rvs.dt.rb
|
|
38
|
+
- MIT-LICENSE
|
|
39
|
+
homepage: https://github.com/atpsoft/rvs
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
none: false
|
|
48
|
+
requirements:
|
|
49
|
+
- - ! '>='
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: 1.9.2
|
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
|
+
none: false
|
|
54
|
+
requirements:
|
|
55
|
+
- - ! '>='
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 1.8.15
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 3
|
|
63
|
+
summary: Ruby Values Serialization to ascii text
|
|
64
|
+
test_files:
|
|
65
|
+
- test/rvs.dt.rb
|
|
66
|
+
has_rdoc:
|