anvl 0.1.3 → 0.2.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/VERSION +1 -1
- data/anvl.gemspec +11 -4
- data/lib/anvl.rb +2 -111
- data/lib/anvl/document.rb +130 -0
- data/lib/anvl/element.rb +61 -0
- data/lib/anvl/erc.rb +109 -0
- data/test/test_anvl.rb +28 -3
- data/test/test_element.rb +23 -0
- data/test/test_erc.rb +134 -0
- metadata +21 -14
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
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.
|
8
|
+
s.version = "0.2.0"
|
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-12-
|
12
|
+
s.date = %q{2010-12-12}
|
13
13
|
s.email = %q{chris@cbeer.info}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -25,8 +25,13 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"anvl.gemspec",
|
27
27
|
"lib/anvl.rb",
|
28
|
+
"lib/anvl/document.rb",
|
29
|
+
"lib/anvl/element.rb",
|
30
|
+
"lib/anvl/erc.rb",
|
28
31
|
"test/helper.rb",
|
29
|
-
"test/test_anvl.rb"
|
32
|
+
"test/test_anvl.rb",
|
33
|
+
"test/test_element.rb",
|
34
|
+
"test/test_erc.rb"
|
30
35
|
]
|
31
36
|
s.homepage = %q{http://github.com/cbeer/anvl}
|
32
37
|
s.licenses = ["MIT"]
|
@@ -35,7 +40,9 @@ Gem::Specification.new do |s|
|
|
35
40
|
s.summary = %q{Ruby implementation of A Name-Value Language (ANVL)}
|
36
41
|
s.test_files = [
|
37
42
|
"test/helper.rb",
|
38
|
-
"test/test_anvl.rb"
|
43
|
+
"test/test_anvl.rb",
|
44
|
+
"test/test_element.rb",
|
45
|
+
"test/test_erc.rb"
|
39
46
|
]
|
40
47
|
|
41
48
|
if s.respond_to? :specification_version then
|
data/lib/anvl.rb
CHANGED
@@ -1,115 +1,6 @@
|
|
1
|
+
require 'anvl/element'
|
2
|
+
require 'anvl/document'
|
1
3
|
module ANVL
|
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 = Hash.new { |h,k| h[k] = [] }
|
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
|
-
when /:/
|
27
|
-
parse_entry str, i
|
28
|
-
else # something is wrong..
|
29
|
-
nil
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end if obj
|
34
|
-
|
35
|
-
add_entries_methods
|
36
|
-
|
37
|
-
gc!
|
38
|
-
end
|
39
|
-
|
40
|
-
def to_s
|
41
|
-
gc!
|
42
|
-
@entries.map do |key, value|
|
43
|
-
if value.is_a? Array
|
44
|
-
value.map do |v|
|
45
|
-
"#{key}: #{v.to_s.gsub(/\n/, "\n ")}"
|
46
|
-
end
|
47
|
-
else
|
48
|
-
"#{key}: #{value.to_s.gsub(/\n/, "\n ")}"
|
49
|
-
end
|
50
|
-
end.join "\n"
|
51
|
-
end
|
52
|
-
|
53
|
-
def to_h
|
54
|
-
gc!
|
55
|
-
@entries
|
56
|
-
end
|
57
|
-
|
58
|
-
def [] key
|
59
|
-
return @entries[key]
|
60
|
-
end
|
61
|
-
|
62
|
-
def []= key, value
|
63
|
-
value = [value] unless value.is_a? Array
|
64
|
-
@entries[key] = value
|
65
|
-
end
|
66
|
-
|
67
|
-
def push hash
|
68
|
-
hash.each do |key, value|
|
69
|
-
@entries[key] = [@entries[key]] unless @entries[key].is_a? Array
|
70
|
-
if value.is_a? Array
|
71
|
-
value.each do |v|
|
72
|
-
@entries[key] << v
|
73
|
-
end
|
74
|
-
else
|
75
|
-
@entries[key] << value
|
76
|
-
end
|
77
|
-
end
|
78
|
-
gc!
|
79
|
-
@entries
|
80
|
-
end
|
81
|
-
alias_method :'<<', :push
|
82
|
-
|
83
|
-
private
|
84
|
-
|
85
|
-
def parse_comment str, line=0
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
def parse_entry str, line=0
|
90
|
-
key, value = str.split ":", 2
|
91
|
-
@entries[key.to_sym] << value.strip
|
92
|
-
end
|
93
|
-
|
94
|
-
def add_entries_methods
|
95
|
-
@entries.public_methods(false).reject { |x| self.respond_to? x }.each do |meth|
|
96
|
-
(class << self; self; end).class_eval do
|
97
|
-
define_method meth do |*args|
|
98
|
-
@entries.send meth, *args
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def gc!
|
105
|
-
@entries.delete_if { |key, value| value.nil? or (value.is_a? Array and value.empty?) }
|
106
|
-
|
107
|
-
@entries.each do |key, value|
|
108
|
-
@entries[key] = value.first if value.is_a? Array and value.length == 1
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
4
|
def self.parse *args
|
114
5
|
Document.parse *args
|
115
6
|
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module ANVL
|
2
|
+
class Document # < Hash
|
3
|
+
def self.parse str
|
4
|
+
anvl = self.new str
|
5
|
+
anvl
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :entries
|
9
|
+
|
10
|
+
def initialize obj = nil
|
11
|
+
@entries = Array.new
|
12
|
+
|
13
|
+
case obj
|
14
|
+
when Hash
|
15
|
+
self.push obj
|
16
|
+
|
17
|
+
when String
|
18
|
+
lines = obj.gsub(/\s?\n\s+/, ' ').split("\n")
|
19
|
+
|
20
|
+
lines.each_with_index do |str, i|
|
21
|
+
case str
|
22
|
+
when /^#/
|
23
|
+
parse_comment str, i
|
24
|
+
when /:/
|
25
|
+
parse_entry str, i
|
26
|
+
else # something is wrong..
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end if obj
|
32
|
+
|
33
|
+
add_entries_methods
|
34
|
+
|
35
|
+
gc!
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
gc!
|
40
|
+
@entries.map do |obj|
|
41
|
+
obj.to_anvl
|
42
|
+
end.join "\n"
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_h
|
46
|
+
gc!
|
47
|
+
h = {}
|
48
|
+
|
49
|
+
@entries.map do |obj|
|
50
|
+
h[(obj.display_label || obj.label).to_sym] ||= []
|
51
|
+
h[(obj.display_label || obj.label).to_sym] << obj.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
h.each do |label, value|
|
55
|
+
h[label] = value.first if value.length == 1
|
56
|
+
end
|
57
|
+
|
58
|
+
h
|
59
|
+
end
|
60
|
+
|
61
|
+
def [] display_label, args = {}
|
62
|
+
v = @entries.select { |x| x =~ display_label }
|
63
|
+
v &&= v.map { |x| x.to_s } unless args[:raw]
|
64
|
+
v &&= v.first if v.length == 1
|
65
|
+
v
|
66
|
+
end
|
67
|
+
alias_method :fetch, :[]
|
68
|
+
|
69
|
+
def []= display_label, value, append = false
|
70
|
+
label = convert_label display_label
|
71
|
+
value = [value] unless value.is_a? Array
|
72
|
+
@entries.delete_if { |x| x =~ label } unless append
|
73
|
+
value.each do |v|
|
74
|
+
case v
|
75
|
+
when Hash
|
76
|
+
@entries << element_class.new({ :document => self, :label => label, :value => v }.merge(v))
|
77
|
+
else
|
78
|
+
@entries << element_class.new({ :document => self, :label => label, :value => v })
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
alias_method :store, :[]=
|
84
|
+
|
85
|
+
def push hash
|
86
|
+
hash.each do |label, value|
|
87
|
+
self.store label, value, true
|
88
|
+
end
|
89
|
+
gc!
|
90
|
+
@entries
|
91
|
+
end
|
92
|
+
alias_method :'<<', :push
|
93
|
+
|
94
|
+
protected
|
95
|
+
def element_class
|
96
|
+
ANVL::Element
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_comment str, line=0
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def parse_entry str, line=0
|
104
|
+
label, value = str.split ":", 2
|
105
|
+
self.store label, value, true
|
106
|
+
end
|
107
|
+
|
108
|
+
def add_entries_methods
|
109
|
+
@entries.reject { |x| self.respond_to? x[:label] }.each do |obj|
|
110
|
+
(class << self; self; end).class_eval do
|
111
|
+
define_method obj.label do |*args|
|
112
|
+
@entries[obj.label]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def gc!
|
119
|
+
end
|
120
|
+
|
121
|
+
def format_value str = ''
|
122
|
+
str = str.to_s
|
123
|
+
str &&= str.gsub(/\n/, "\n ")
|
124
|
+
end
|
125
|
+
|
126
|
+
def convert_label label
|
127
|
+
label.to_s
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/lib/anvl/element.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module ANVL
|
2
|
+
class Element
|
3
|
+
attr_reader :document, :label, :display_label, :value
|
4
|
+
def initialize args
|
5
|
+
@document = args[:document]
|
6
|
+
@display_label = args[:display_label] || convert_label(args[:label])
|
7
|
+
@label = convert_label(args[:label])
|
8
|
+
@value = args[:value].to_s.strip
|
9
|
+
end
|
10
|
+
|
11
|
+
def [] key
|
12
|
+
self.send key.to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
str = @value
|
17
|
+
str &&= str.gsub(/\s*\n\s+/, ' ')
|
18
|
+
|
19
|
+
str
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_anvl
|
23
|
+
"#{@display_label || @label}:#{format_value_for_anvl}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def <=> obj
|
27
|
+
@value <=> obj
|
28
|
+
end
|
29
|
+
|
30
|
+
def == obj
|
31
|
+
case obj
|
32
|
+
when String
|
33
|
+
return @value == obj if obj.is_a? String
|
34
|
+
when Element
|
35
|
+
@label == obj.label && @value = obj.display_label
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def =~ str
|
42
|
+
str = str.to_s
|
43
|
+
str == @label or str == @display_label or convert_label(str) == @label
|
44
|
+
end
|
45
|
+
|
46
|
+
def push value
|
47
|
+
@document.store @display_label, value, true
|
48
|
+
end
|
49
|
+
alias_method :'<<', :push
|
50
|
+
|
51
|
+
private
|
52
|
+
def format_value_for_anvl
|
53
|
+
" " + @value.gsub(/\n/, "\n ")
|
54
|
+
end
|
55
|
+
|
56
|
+
def convert_label label
|
57
|
+
label
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
data/lib/anvl/erc.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'anvl'
|
2
|
+
module ANVL
|
3
|
+
class Erc < Document
|
4
|
+
STORY_TO_OFFSET = { 'erc' => 0, 'about-erc' => 10, 'support-erc' => 20, 'meta-erc' => 30 }
|
5
|
+
|
6
|
+
LABEL_TO_SYN = {
|
7
|
+
'who' => 'h1', 'what' => 'h2', 'when' => 'h3', 'where' => 'h4', 'how' => 'h5',
|
8
|
+
'about-who' => 'h11', 'about-what' => 'h12', 'about-when' => 'h13', 'about-where' => 'h14', 'about-how' => 'h15',
|
9
|
+
'support-who' => 'h21', 'support-what' => 'h22', 'support-when' => 'h23', 'support-where' => 'h24',
|
10
|
+
'meta-who' => 'h31', 'meta-what' => 'h32', 'meta-when' => 'h33', 'meta-where' => 'h34'
|
11
|
+
}
|
12
|
+
|
13
|
+
SYN_TO_KERNEL = LABEL_TO_SYN.invert
|
14
|
+
|
15
|
+
|
16
|
+
class Element < ANVL::Element
|
17
|
+
def to_s
|
18
|
+
str = super
|
19
|
+
str &&= format_initial_comma_to_recover_word_order str
|
20
|
+
str &&= decode_element_value_encoding str
|
21
|
+
str
|
22
|
+
end
|
23
|
+
|
24
|
+
def =~ str
|
25
|
+
bool = super
|
26
|
+
|
27
|
+
label = convert_label str
|
28
|
+
bool ||= (label =~ Regexp.new("\\(#{Regexp.escape(@label)}\\)"))
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def format_value_for_anvl
|
33
|
+
super.sub(/^\s+([,;\|])/, '\1')
|
34
|
+
end
|
35
|
+
|
36
|
+
def format_initial_comma_to_recover_word_order str, sort_point = ','
|
37
|
+
return str unless str[0, 1] == sort_point
|
38
|
+
arr = str.split(sort_point)
|
39
|
+
arr.shift
|
40
|
+
|
41
|
+
insig = nil
|
42
|
+
if str[-1,1] == sort_point
|
43
|
+
insig = arr.pop.strip
|
44
|
+
end
|
45
|
+
|
46
|
+
word = arr.pop
|
47
|
+
arr = [arr.join ","]
|
48
|
+
arr.unshift word
|
49
|
+
arr.unshift insig
|
50
|
+
|
51
|
+
arr.compact.map(&:strip).reject { |x| x.empty?}.join " "
|
52
|
+
end
|
53
|
+
|
54
|
+
def decode_element_value_encoding str
|
55
|
+
h = { '%sp' => ' ', '%ex' => '!', '%dq' => '"', '%ns' => '#', '%do' => '$', '%pe' => '%', '%am' => '&', '%sq' => '\'', '%op' => '(', '%cp' => ')', '%as' => '*', '%pl' => '+', '%co' => ',', '%sl' => '/', '%cn' => ':', '%sc' => ';', '%lt' => '<', '%eq' => '=', '%gt' => '>', '%qu' => '?', '%at' => '@', '%ox' => '[', '%ls' => '\\', '%cx' => ']', '%vb' => '|', '%nu' => "\0", '%%' => '%' }
|
56
|
+
s = str.dup
|
57
|
+
h.each do |key, value|
|
58
|
+
s.gsub! key, value
|
59
|
+
end
|
60
|
+
s
|
61
|
+
end
|
62
|
+
|
63
|
+
def convert_label label
|
64
|
+
str = label.to_s.downcase.gsub(' ', '_')
|
65
|
+
|
66
|
+
str = ANVL::Erc::LABEL_TO_SYN[str] if ANVL::Erc::LABEL_TO_SYN[str]
|
67
|
+
|
68
|
+
str
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def []= display_label, value, append = false
|
74
|
+
if value[0,1] != '|' and STORY_TO_OFFSET[display_label] and !value.strip.empty?
|
75
|
+
process_abbr_form display_label, value
|
76
|
+
elsif value[0,1] != ';' and value =~ /;/
|
77
|
+
value.split(';').each do |v|
|
78
|
+
self.store display_label, v, true
|
79
|
+
end
|
80
|
+
else
|
81
|
+
super
|
82
|
+
end
|
83
|
+
end
|
84
|
+
alias_method :store, :[]=
|
85
|
+
|
86
|
+
def complete?
|
87
|
+
!(self.fetch(:h1).empty? or self.fetch(:h2).empty? or self.fetch(:h3).empty? or self.fetch(:h4).empty?) ||
|
88
|
+
!(self.fetch(:h11).empty? or self.fetch(:h12).empty? or self.fetch(:h13).empty? or self.fetch(:h14).empty?) ||
|
89
|
+
!(self.fetch(:h21).empty? or self.fetch(:h22).empty? or self.fetch(:h23).empty? or self.fetch(:h24).empty?) ||
|
90
|
+
!(self.fetch(:h31).empty? or self.fetch(:h32).empty? or self.fetch(:h33).empty? or self.fetch(:h34).empty?)
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
def element_class
|
96
|
+
ANVL::Erc::Element
|
97
|
+
end
|
98
|
+
|
99
|
+
def process_abbr_form display_label, value
|
100
|
+
offset = STORY_TO_OFFSET[display_label]
|
101
|
+
elements = value.split('|')
|
102
|
+
elements.each_with_index do |e, i|
|
103
|
+
next if e.strip.empty?
|
104
|
+
self.store "h#{offset + i + 1}", e.strip
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
data/test/test_anvl.rb
CHANGED
@@ -38,7 +38,8 @@ a: 2'
|
|
38
38
|
assert_equal({:a => 'a' }, h.to_h)
|
39
39
|
h[:a] = ['a', 'b']
|
40
40
|
assert_equal({:a => ['a', 'b'] }, h.to_h)
|
41
|
-
h[:a]
|
41
|
+
h[:a].inspect
|
42
|
+
h.store :a, 'c', true
|
42
43
|
assert_equal({:a => ['a', 'b', 'c'] }, h.to_h)
|
43
44
|
assert_equal(['a', 'b', 'c'], h[:a])
|
44
45
|
|
@@ -49,10 +50,10 @@ a: 2'
|
|
49
50
|
assert_equal({:a => ['a', 'b', 'c', 'd'] }, h.to_h)
|
50
51
|
|
51
52
|
h << { :c => 1 }
|
52
|
-
assert_equal(1, h[:c])
|
53
|
+
assert_equal("1", h[:c])
|
53
54
|
|
54
55
|
h << { :c => 2 }
|
55
|
-
assert_equal([1, 2], h[:c])
|
56
|
+
assert_equal(["1", "2"], h[:c])
|
56
57
|
|
57
58
|
str = h.to_s
|
58
59
|
assert_match(/^a: a$/, str)
|
@@ -76,9 +77,33 @@ a: 2'
|
|
76
77
|
|
77
78
|
def test_fmt_first_draft
|
78
79
|
str = ANVL.to_anvl({:entry => [""], :who => ['Gilbert, W.S. | Sullivan, Arthur'], :what => ["The Yeomen of the Guard"], :"when/created" => [1888]})
|
80
|
+
h = ANVL.parse({:entry => [""], :who => ['Gilbert, W.S. | Sullivan, Arthur'], :what => ["The Yeomen of the Guard"], :"when/created" => [1888]})
|
79
81
|
assert_match(/entry:/, str)
|
80
82
|
assert_match(/who: Gilbert, W.S. | Sullivan, Arthur/, str)
|
81
83
|
assert_match(/what: The Yeomen of the Guard/, str)
|
82
84
|
assert_match(/when\/created: 1888/, str)
|
83
85
|
end
|
86
|
+
|
87
|
+
def test_display_label
|
88
|
+
h = ANVL::Document.new
|
89
|
+
h['a'] = {:display_label => 'A', :value => '123' }
|
90
|
+
assert_equal("123", h['a'])
|
91
|
+
|
92
|
+
assert_match(/A:/, h.to_s)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_str_vs_sym
|
96
|
+
str = 'erc:
|
97
|
+
who: Lederberg, Joshua
|
98
|
+
what: Studies of Human Families for Genetic Linkage
|
99
|
+
when: 1974
|
100
|
+
where: http://profiles.nlm.nih.gov/BB/AA/TT/tt.pdf
|
101
|
+
note: This is an arbitrary note inside a
|
102
|
+
small descriptive record.'
|
103
|
+
h = ANVL::Document.parse str
|
104
|
+
|
105
|
+
assert_equal("Lederberg, Joshua", h['who'])
|
106
|
+
assert_equal("Lederberg, Joshua", h[:who])
|
107
|
+
end
|
108
|
+
|
84
109
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestErcElement < Test::Unit::TestCase
|
4
|
+
def test_basic
|
5
|
+
h = ANVL::Erc::Element.new :label => 'abc', :value => '123'
|
6
|
+
assert_equal('123', h.to_s)
|
7
|
+
assert_equal('abc: 123', h.to_anvl)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_initial_comma_to_recover_natural_word_order
|
11
|
+
h = ANVL::Erc::Element.new :label => 'abc', :value => ', van Gogh, Vincent'
|
12
|
+
assert_equal('Vincent van Gogh', h.to_s)
|
13
|
+
assert_equal('abc:, van Gogh, Vincent', h.to_anvl)
|
14
|
+
|
15
|
+
h = ANVL::Erc::Element.new :label => 'abc', :value => ', Howell, III, PhD, 1922-1987, Thurston'
|
16
|
+
assert_equal('Thurston Howell, III, PhD, 1922-1987', h.to_s)
|
17
|
+
assert_equal('abc:, Howell, III, PhD, 1922-1987, Thurston', h.to_anvl)
|
18
|
+
|
19
|
+
h = ANVL::Erc::Element.new :label => 'abc', :value => ', McCartney, Paul, Sir,'
|
20
|
+
assert_equal('Sir Paul McCartney', h.to_s)
|
21
|
+
assert_equal('abc:, McCartney, Paul, Sir,', h.to_anvl)
|
22
|
+
end
|
23
|
+
end
|
data/test/test_erc.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'anvl/erc'
|
3
|
+
|
4
|
+
class TestErc < Test::Unit::TestCase
|
5
|
+
def test_parse_empty
|
6
|
+
h = ANVL::Erc.parse ''
|
7
|
+
assert_equal(0, h.entries.length)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_basic
|
11
|
+
str = 'erc:
|
12
|
+
who: Gibbon, Edward
|
13
|
+
what: The Decline and Fall of the Roman Empire
|
14
|
+
when: 1781
|
15
|
+
where: http://www.ccel.org/g/gibbon/decline/'
|
16
|
+
|
17
|
+
h = ANVL::Erc.parse str
|
18
|
+
assert_equal("", h[:erc])
|
19
|
+
assert_equal("Gibbon, Edward", h[:who])
|
20
|
+
assert_equal("The Decline and Fall of the Roman Empire", h[:what])
|
21
|
+
assert_equal("1781", h[:when])
|
22
|
+
assert_equal("http://www.ccel.org/g/gibbon/decline/", h[:where])
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_complete
|
26
|
+
str = 'erc:
|
27
|
+
what: The Digital Dilemma
|
28
|
+
where: http://books.nap.edu/html/digital%5Fdilemma
|
29
|
+
'
|
30
|
+
|
31
|
+
h = ANVL::Erc.parse str
|
32
|
+
assert_equal(false, h.complete?)
|
33
|
+
|
34
|
+
h[:who] = '---'
|
35
|
+
h[:when] = '---'
|
36
|
+
assert(h.complete?)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_complete_meta
|
40
|
+
str = 'meta-erc: NLM | pm9546494 | 19980418
|
41
|
+
| http://ark.nlm.nih.gov/12025/pm9546494??'
|
42
|
+
h = ANVL::Erc.parse str
|
43
|
+
assert(h.complete?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_missing_about
|
47
|
+
str = 'about-erc: | Bispectrum ; Nonlinearity ; Epilepsy
|
48
|
+
; Cooperativity ; Subdural ; Hippocampus'
|
49
|
+
h = ANVL::Erc.parse str
|
50
|
+
assert_contains(h['about-what'], 'Bispectrum')
|
51
|
+
assert_contains(h['about-what'], 'Nonlinearity')
|
52
|
+
assert_contains(h['about-what'], 'Epilepsy')
|
53
|
+
assert_contains(h['about-what'], 'Cooperativity')
|
54
|
+
assert_contains(h['about-what'], 'Subdural')
|
55
|
+
assert_contains(h['about-what'], 'Hippocampus')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_abbr
|
59
|
+
str = 'erc: Gibbon, Edward | The Decline and Fall of the Roman Empire
|
60
|
+
| 1781 | http://www.ccel.org/g/gibbon/decline/'
|
61
|
+
|
62
|
+
h = ANVL::Erc.parse str
|
63
|
+
assert_equal("Gibbon, Edward", h[:who])
|
64
|
+
assert_equal("The Decline and Fall of the Roman Empire", h[:what])
|
65
|
+
assert_equal("1781", h[:when])
|
66
|
+
assert_equal("http://www.ccel.org/g/gibbon/decline/", h[:where])
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_multiple_values
|
70
|
+
str = 'erc:
|
71
|
+
who: Smith, J; Wong, D; Khan, H'
|
72
|
+
h = ANVL::Erc.parse str
|
73
|
+
|
74
|
+
assert_contains(h[:who], 'Smith, J')
|
75
|
+
assert_contains(h[:who], 'Wong, D')
|
76
|
+
assert_contains(h[:who], 'Khan, H')
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_intl
|
80
|
+
str = 'erc:
|
81
|
+
who: Lederberg, Joshua
|
82
|
+
h2: Studies of Human Families for Genetic Linkage'
|
83
|
+
h = ANVL::Erc.parse str
|
84
|
+
assert_equal("Lederberg, Joshua", h[:who])
|
85
|
+
assert_equal("Lederberg, Joshua", h['who'])
|
86
|
+
assert_equal("Lederberg, Joshua", h[:h1])
|
87
|
+
assert_equal("Lederberg, Joshua", h['wer(h1)'])
|
88
|
+
assert_equal("Studies of Human Families for Genetic Linkage", h[:h2])
|
89
|
+
assert_equal("Studies of Human Families for Genetic Linkage", h[:what])
|
90
|
+
assert_equal("Studies of Human Families for Genetic Linkage", h['was(h2)'])
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_label_structure
|
94
|
+
str = 'erc:'
|
95
|
+
h = ANVL::Erc.parse str
|
96
|
+
|
97
|
+
h['marc_856'] = 'abc'
|
98
|
+
|
99
|
+
assert_equal("abc", h['marc_856'])
|
100
|
+
h['MARC 856'] = '123'
|
101
|
+
|
102
|
+
assert_equal("123", h['marc_856'])
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_initial_comma
|
106
|
+
str = 'erc:
|
107
|
+
who:, van Gogh, Vincent
|
108
|
+
who:, Howell, III, PhD, 1922-1987, Thurston
|
109
|
+
who:, Acme Rocket Factory, Inc., The
|
110
|
+
who:, Mao Tse Tung
|
111
|
+
who:, McCartney, Pat, Ms,
|
112
|
+
who:, McCartney, Paul, Sir,
|
113
|
+
who:, McCartney, Petra, Dr,
|
114
|
+
what:, Health and Human Services, United States Government
|
115
|
+
Department of, The,'
|
116
|
+
|
117
|
+
h = ANVL::Erc.parse str
|
118
|
+
assert_contains(h[:who], "Vincent van Gogh")
|
119
|
+
assert_contains(h[:who], "Thurston Howell, III, PhD, 1922-1987")
|
120
|
+
assert_contains(h[:who], "The Acme Rocket Factory, Inc.")
|
121
|
+
assert_contains(h[:who], "Mao Tse Tung")
|
122
|
+
assert_contains(h[:who], "Ms Pat McCartney")
|
123
|
+
assert_contains(h[:who], "Sir Paul McCartney")
|
124
|
+
assert_contains(h[:who], "Dr Petra McCartney")
|
125
|
+
assert_equal(h[:what], "The United States Government Department of Health and Human Services")
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_value_encoding
|
129
|
+
str = 'erc:
|
130
|
+
who: %sp%ex%dq'
|
131
|
+
h = ANVL::Erc.parse str
|
132
|
+
assert_contains(" !\"", h[:who])
|
133
|
+
end
|
134
|
+
end
|
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Beer
|
@@ -15,10 +15,13 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-12 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
name: shoulda
|
22
25
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
26
|
none: false
|
24
27
|
requirements:
|
@@ -28,11 +31,11 @@ dependencies:
|
|
28
31
|
segments:
|
29
32
|
- 0
|
30
33
|
version: "0"
|
31
|
-
name: shoulda
|
32
34
|
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
33
36
|
type: :development
|
34
37
|
prerelease: false
|
35
|
-
|
38
|
+
name: bundler
|
36
39
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
40
|
none: false
|
38
41
|
requirements:
|
@@ -44,11 +47,11 @@ dependencies:
|
|
44
47
|
- 0
|
45
48
|
- 0
|
46
49
|
version: 1.0.0
|
47
|
-
name: bundler
|
48
50
|
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
49
52
|
type: :development
|
50
53
|
prerelease: false
|
51
|
-
|
54
|
+
name: jeweler
|
52
55
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
53
56
|
none: false
|
54
57
|
requirements:
|
@@ -60,11 +63,11 @@ dependencies:
|
|
60
63
|
- 5
|
61
64
|
- 1
|
62
65
|
version: 1.5.1
|
63
|
-
name: jeweler
|
64
66
|
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
65
68
|
type: :development
|
66
69
|
prerelease: false
|
67
|
-
|
70
|
+
name: rcov
|
68
71
|
version_requirements: &id004 !ruby/object:Gem::Requirement
|
69
72
|
none: false
|
70
73
|
requirements:
|
@@ -74,10 +77,7 @@ dependencies:
|
|
74
77
|
segments:
|
75
78
|
- 0
|
76
79
|
version: "0"
|
77
|
-
name: rcov
|
78
80
|
requirement: *id004
|
79
|
-
type: :development
|
80
|
-
prerelease: false
|
81
81
|
description:
|
82
82
|
email: chris@cbeer.info
|
83
83
|
executables: []
|
@@ -97,8 +97,13 @@ files:
|
|
97
97
|
- VERSION
|
98
98
|
- anvl.gemspec
|
99
99
|
- lib/anvl.rb
|
100
|
+
- lib/anvl/document.rb
|
101
|
+
- lib/anvl/element.rb
|
102
|
+
- lib/anvl/erc.rb
|
100
103
|
- test/helper.rb
|
101
104
|
- test/test_anvl.rb
|
105
|
+
- test/test_element.rb
|
106
|
+
- test/test_erc.rb
|
102
107
|
has_rdoc: true
|
103
108
|
homepage: http://github.com/cbeer/anvl
|
104
109
|
licenses:
|
@@ -136,3 +141,5 @@ summary: Ruby implementation of A Name-Value Language (ANVL)
|
|
136
141
|
test_files:
|
137
142
|
- test/helper.rb
|
138
143
|
- test/test_anvl.rb
|
144
|
+
- test/test_element.rb
|
145
|
+
- test/test_erc.rb
|