anvl 0.1.0 → 0.1.1

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 +2 -2
  3. data/lib/anvl.rb +94 -32
  4. data/test/test_anvl.rb +2 -2
  5. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{anvl}
8
- s.version = "0.1.0"
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-26}
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",
@@ -1,45 +1,107 @@
1
1
  module ANVL
2
- def self.parse str
3
- data = {}
4
- entries = []
5
- lines = str.split("\n")
6
-
7
- lines.each do |l|
8
- case l
9
- when /^\#/
10
- # comment
11
- next
12
- when /^\s/
13
- entries.last << l.gsub(/^\s+/, ' ')
14
- else
15
- entries << l
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
- entries.each do |e|
20
- key, value = e.split ":", 2
21
- data[key.to_sym] ||= []
22
- data[key.to_sym] << value.strip
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
- data.each do |key, value|
26
- data[key] = value.first if value.length == 1
56
+ def [] key
57
+ @entries[key] ||= []
58
+ return @entries[key]
27
59
  end
28
60
 
29
- data
30
- end
61
+ def []= key, value
62
+ @entries[key] = value
63
+ end
31
64
 
32
- def self.to_anvl h
33
- lines = []
34
- h.each do |key, value|
35
- if value.respond_to? :each
36
- value.each do |v|
37
- lines << "#{key}: #{v}"
38
- end
39
- else
40
- lines << "#{key}: #{value}"
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
- lines.join "\n"
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
@@ -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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
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-26 00:00:00 -05:00
18
+ date: 2010-11-28 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency