gedcom_ruby 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gedcom_ruby.rb +36 -22
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42866a57868acd57302baaa15c34da3ee628efae
|
4
|
+
data.tar.gz: 1d9ea35ed03626c8c246bf2e7af1e12638a90566
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29e7b96122f3964984543612057151c56d639895b440cab466cf4f0dde6e9861c25ac1b3ed6228008fbae652db9af5bfb080b1073bfb8a3a28e5ebabb566a3eb
|
7
|
+
data.tar.gz: 40fb3f8ac4fae584f9cc23c655c39bc24095a6c0578533db3d5f1b2dce516b7d005f031b10b69f48cff1e5ddb70fa2612e6798c532ec43005ca8213fdd5f61d9
|
data/lib/gedcom_ruby.rb
CHANGED
@@ -20,16 +20,19 @@
|
|
20
20
|
#require '_gedcom'
|
21
21
|
require 'gedcom_ruby/date'
|
22
22
|
require 'stringio'
|
23
|
+
require 'byebug'
|
23
24
|
|
24
25
|
module GEDCOM
|
25
|
-
attr_accessor :auto_concat
|
26
|
-
ANY = [:any]
|
27
26
|
|
28
27
|
class Parser
|
28
|
+
attr_accessor :auto_concat
|
29
|
+
attr_reader :callbacks
|
30
|
+
ANY = [:any]
|
31
|
+
|
29
32
|
def initialize(&block)
|
30
33
|
@callbacks = {
|
31
|
-
:before =>
|
32
|
-
:after =>
|
34
|
+
:before => {},
|
35
|
+
:after => {}
|
33
36
|
}
|
34
37
|
|
35
38
|
@context_stack = []
|
@@ -39,17 +42,27 @@ module GEDCOM
|
|
39
42
|
@auto_concat = true
|
40
43
|
|
41
44
|
instance_eval(&block) if block_given?
|
45
|
+
|
46
|
+
after_initialize
|
47
|
+
end
|
48
|
+
|
49
|
+
def after_initialize
|
50
|
+
# Template
|
42
51
|
end
|
43
52
|
|
44
53
|
def before(tags, callback=nil, &block)
|
45
54
|
tags = [tags].flatten
|
46
55
|
callback = check_proc_or_block(callback, &block)
|
56
|
+
|
57
|
+
@callbacks[:before][tags] = default_empty(@callbacks[:before][tags])
|
47
58
|
@callbacks[:before][tags].push(callback)
|
48
59
|
end
|
49
60
|
|
50
61
|
def after(tags, callback=nil, &block)
|
51
62
|
tags = [tags].flatten
|
52
63
|
callback = check_proc_or_block(callback, &block)
|
64
|
+
|
65
|
+
@callbacks[:after][tags] = default_empty(@callbacks[:after][tags])
|
53
66
|
@callbacks[:after][tags].push(callback)
|
54
67
|
end
|
55
68
|
|
@@ -75,6 +88,10 @@ module GEDCOM
|
|
75
88
|
|
76
89
|
protected
|
77
90
|
|
91
|
+
def default_empty(arr)
|
92
|
+
arr || []
|
93
|
+
end
|
94
|
+
|
78
95
|
def check_proc_or_block(proc, &block)
|
79
96
|
unless proc or block_given?
|
80
97
|
raise ArgumentError.new("proc or block required")
|
@@ -95,12 +112,13 @@ module GEDCOM
|
|
95
112
|
|
96
113
|
def parse_io(io)
|
97
114
|
io.each_line do |line|
|
98
|
-
|
99
|
-
next if
|
115
|
+
line = line.rstrip!
|
116
|
+
next if line.empty?
|
117
|
+
level, tag, rest = line.match(/^(\d) (\S+) ?(.*)$/).captures
|
100
118
|
level = level.to_i
|
101
119
|
|
102
120
|
if (tag == 'CONT' || tag == 'CONC') and @auto_concat
|
103
|
-
concat_data
|
121
|
+
concat_data(tag, rest)
|
104
122
|
next
|
105
123
|
end
|
106
124
|
|
@@ -114,7 +132,7 @@ module GEDCOM
|
|
114
132
|
|
115
133
|
do_callbacks(:before, @context_stack, rest)
|
116
134
|
end
|
117
|
-
unwind_to
|
135
|
+
unwind_to(-1)
|
118
136
|
end
|
119
137
|
|
120
138
|
def unwind_to(level)
|
@@ -127,24 +145,20 @@ module GEDCOM
|
|
127
145
|
end
|
128
146
|
|
129
147
|
def concat_data(tag, rest)
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
if tag == 'CONT'
|
137
|
-
@data_stack[-1] << "\n" + (rest || "")
|
138
|
-
elsif tag == 'CONC'
|
139
|
-
old = @data_stack[-1].chomp
|
140
|
-
@data_stack[-1] = old + (rest || "")
|
141
|
-
end
|
142
|
-
end
|
148
|
+
rest = rest || "" # Handle nil case
|
149
|
+
@data_stack[-1] = case
|
150
|
+
when @data_stack.last.empty? then rest
|
151
|
+
when @context_stack.last == 'BLOB' then "#{@data_stack.last}#{rest}"
|
152
|
+
when tag == 'CONT' then "#{@data_stack.last}\n#{rest}"
|
153
|
+
when tag == 'CONC' then "#{@data_stack.last}#{rest}"
|
143
154
|
end
|
144
155
|
end
|
145
156
|
|
146
157
|
def do_callbacks(context_sym, tags, data)
|
147
|
-
|
158
|
+
return if tags == []
|
159
|
+
tag_cbs = default_empty(@callbacks[context_sym][tags])
|
160
|
+
any_cbs = default_empty(@callbacks[context_sym][ANY])
|
161
|
+
relevant_callbacks = tag_cbs + any_cbs
|
148
162
|
relevant_callbacks.each do |callback|
|
149
163
|
callback.call(data)
|
150
164
|
end
|
metadata
CHANGED