anvl 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/anvl.gemspec +1 -1
  3. data/lib/anvl.rb +27 -15
  4. data/test/test_anvl.rb +40 -7
  5. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{anvl}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
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"]
@@ -9,7 +9,7 @@ module ANVL
9
9
  attr_reader :entries
10
10
 
11
11
  def initialize obj = nil
12
- @entries = {}
12
+ @entries = Hash.new { |h,k| h[k] = [] }
13
13
 
14
14
  case obj
15
15
  when Hash
@@ -30,19 +30,14 @@ module ANVL
30
30
 
31
31
  end if obj
32
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
39
- end
33
+ add_entries_methods
40
34
 
41
- cleanup_entries
35
+ gc!
42
36
  end
43
37
 
44
38
  def to_s
45
- @entries.reject { |key, value| value.nil? }.map do |key, value|
39
+ gc!
40
+ @entries.map do |key, value|
46
41
  if value.is_a? Array
47
42
  value.map do |v|
48
43
  "#{key}: #{v}"
@@ -53,18 +48,23 @@ module ANVL
53
48
  end.join "\n"
54
49
  end
55
50
 
51
+ def to_h
52
+ gc!
53
+ @entries
54
+ end
55
+
56
56
  def [] key
57
- @entries[key] ||= []
58
57
  return @entries[key]
59
58
  end
60
59
 
61
60
  def []= key, value
61
+ value = [value] unless value.is_a? Array
62
62
  @entries[key] = value
63
63
  end
64
64
 
65
65
  def push hash
66
66
  hash.each do |key, value|
67
- @entries[key] ||= []
67
+ @entries[key] = [@entries[key]] unless @entries[key].is_a? Array
68
68
  if value.is_a? Array
69
69
  value.each do |v|
70
70
  @entries[key] << v
@@ -73,6 +73,7 @@ module ANVL
73
73
  @entries[key] << value
74
74
  end
75
75
  end
76
+ gc!
76
77
  @entries
77
78
  end
78
79
  alias_method :'<<', :push
@@ -85,13 +86,24 @@ module ANVL
85
86
 
86
87
  def parse_entry str, line=0
87
88
  key, value = str.split ":", 2
88
- @entries[key.to_sym] ||= []
89
89
  @entries[key.to_sym] << value.strip
90
90
  end
91
91
 
92
- def cleanup_entries
92
+ def add_entries_methods
93
+ @entries.public_methods(false).reject { |x| self.respond_to? x }.each do |meth|
94
+ (class << self; self; end).class_eval do
95
+ define_method meth do |*args|
96
+ @entries.send meth, *args
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def gc!
103
+ @entries.delete_if { |key, value| value.nil? or (value.is_a? Array and value.empty?) }
104
+
93
105
  @entries.each do |key, value|
94
- @entries[key] = value.first if value.length == 1
106
+ @entries[key] = value.first if value.is_a? Array and value.length == 1
95
107
  end
96
108
  end
97
109
  end
@@ -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.entries.length, 0)
6
+ assert_equal(0, h.entries.length)
7
7
  end
8
8
 
9
9
  def test_parse_comment
10
10
  h = ANVL.parse '#'
11
- assert_equal(h.entries.length, 0)
11
+ assert_equal(0, h.entries.length)
12
12
  end
13
13
 
14
14
  def test_first_draft
@@ -18,15 +18,48 @@ who: Gilbert, W.S. | Sullivan, Arthur
18
18
  what: The Yeomen of
19
19
  the Guard
20
20
  when/created: 1888'
21
- assert_equal(h[:entry], "")
22
- assert_equal(h[:who], 'Gilbert, W.S. | Sullivan, Arthur')
23
- assert_equal(h[:what], "The Yeomen of the Guard")
24
- assert_equal(h[:"when/created"], "1888")
21
+ assert_equal("", h[:entry])
22
+ assert_equal('Gilbert, W.S. | Sullivan, Arthur', h[:who])
23
+ assert_equal("The Yeomen of the Guard", h[:what])
24
+ assert_equal("1888", h[:"when/created"])
25
+ end
26
+
27
+ def test_multiple_values
28
+ h = ANVL::Document.parse 'entry:
29
+ a: 1
30
+ a: 2'
31
+ assert_equal({:a => ["1","2"], :entry => ""}, h.to_h)
32
+ end
33
+
34
+ def test_key_access
35
+ h = ANVL::Document.new
36
+ assert_equal([], h[:a])
37
+ h[:a] = 'a'
38
+ assert_equal({:a => 'a' }, h.to_h)
39
+ h[:a] = ['a', 'b']
40
+ assert_equal({:a => ['a', 'b'] }, h.to_h)
41
+ h[:a] << 'c'
42
+ assert_equal({:a => ['a', 'b', 'c'] }, h.to_h)
43
+ assert_equal(['a', 'b', 'c'], h[:a])
44
+
45
+ h[:b]
46
+ assert_equal({:a => ['a', 'b', 'c'] }, h.to_h)
47
+
48
+ h << { :a => 'd' }
49
+ assert_equal({:a => ['a', 'b', 'c', 'd'] }, h.to_h)
50
+
51
+ h << { :c => 1 }
52
+ assert_equal(1, h[:c])
53
+
54
+ h << { :c => 2 }
55
+ assert_equal([1, 2], h[:c])
56
+
57
+ assert_equal("a: a\na: b\na: c\na: d\nc: 1\nc: 2", h.to_s)
25
58
  end
26
59
 
27
60
  def test_fmt_empty
28
61
  str = ANVL.to_anvl({})
29
- assert_equal(str, '')
62
+ assert_equal('', str)
30
63
  end
31
64
 
32
65
  def test_fmt_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: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Beer