anvl 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/anvl.gemspec +2 -2
- data/lib/anvl.rb +94 -32
- data/test/test_anvl.rb +2 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/anvl.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{anvl}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chris Beer"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-28}
|
13
13
|
s.email = %q{chris@cbeer.info}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
data/lib/anvl.rb
CHANGED
@@ -1,45 +1,107 @@
|
|
1
1
|
module ANVL
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
2
|
+
class Document # < Hash
|
3
|
+
|
4
|
+
def self.parse str
|
5
|
+
anvl = ANVL::Document.new str
|
6
|
+
anvl
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :entries
|
10
|
+
|
11
|
+
def initialize obj = nil
|
12
|
+
@entries = {}
|
13
|
+
|
14
|
+
case obj
|
15
|
+
when Hash
|
16
|
+
obj.each do |key, value|
|
17
|
+
@entries[key.to_sym] = value
|
18
|
+
end
|
19
|
+
when String
|
20
|
+
lines = obj.gsub(/\s?\n\s+/, ' ').split("\n")
|
21
|
+
|
22
|
+
lines.each_with_index do |str, i|
|
23
|
+
case str
|
24
|
+
when /^#/
|
25
|
+
parse_comment str, i
|
26
|
+
else
|
27
|
+
parse_entry str, i
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end if obj
|
32
|
+
|
33
|
+
@entries.public_methods(false).each do |meth|
|
34
|
+
(class << self; self; end).class_eval do
|
35
|
+
define_method meth do |*args|
|
36
|
+
@entries.send meth, *args
|
37
|
+
end unless self.respond_to? meth
|
38
|
+
end
|
16
39
|
end
|
40
|
+
|
41
|
+
cleanup_entries
|
17
42
|
end
|
18
43
|
|
19
|
-
|
20
|
-
key, value
|
21
|
-
|
22
|
-
|
44
|
+
def to_s
|
45
|
+
@entries.reject { |key, value| value.nil? }.map do |key, value|
|
46
|
+
if value.is_a? Array
|
47
|
+
value.map do |v|
|
48
|
+
"#{key}: #{v}"
|
49
|
+
end
|
50
|
+
else
|
51
|
+
"#{key}: #{value}"
|
52
|
+
end
|
53
|
+
end.join "\n"
|
23
54
|
end
|
24
55
|
|
25
|
-
|
26
|
-
|
56
|
+
def [] key
|
57
|
+
@entries[key] ||= []
|
58
|
+
return @entries[key]
|
27
59
|
end
|
28
60
|
|
29
|
-
|
30
|
-
|
61
|
+
def []= key, value
|
62
|
+
@entries[key] = value
|
63
|
+
end
|
31
64
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
65
|
+
def push hash
|
66
|
+
hash.each do |key, value|
|
67
|
+
@entries[key] ||= []
|
68
|
+
if value.is_a? Array
|
69
|
+
value.each do |v|
|
70
|
+
@entries[key] << v
|
71
|
+
end
|
72
|
+
else
|
73
|
+
@entries[key] << value
|
74
|
+
end
|
41
75
|
end
|
76
|
+
@entries
|
42
77
|
end
|
43
|
-
|
78
|
+
alias_method :'<<', :push
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def parse_comment str, line=0
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
def parse_entry str, line=0
|
87
|
+
key, value = str.split ":", 2
|
88
|
+
@entries[key.to_sym] ||= []
|
89
|
+
@entries[key.to_sym] << value.strip
|
90
|
+
end
|
91
|
+
|
92
|
+
def cleanup_entries
|
93
|
+
@entries.each do |key, value|
|
94
|
+
@entries[key] = value.first if value.length == 1
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.parse *args
|
100
|
+
Document.parse *args
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.to_anvl *args
|
104
|
+
anvl = Document.parse *args
|
105
|
+
anvl.to_s
|
44
106
|
end
|
45
107
|
end
|
data/test/test_anvl.rb
CHANGED
@@ -3,12 +3,12 @@ require 'helper'
|
|
3
3
|
class TestAnvl < Test::Unit::TestCase
|
4
4
|
def test_parse_empty
|
5
5
|
h = ANVL.parse ''
|
6
|
-
assert_equal(h,
|
6
|
+
assert_equal(h.entries.length, 0)
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_parse_comment
|
10
10
|
h = ANVL.parse '#'
|
11
|
-
assert_equal(h,
|
11
|
+
assert_equal(h.entries.length, 0)
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_first_draft
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anvl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Beer
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-28 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|