forked from ualbertalib/sfx2sirsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_holdings.rb
86 lines (71 loc) · 2.61 KB
/
summary_holdings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module SummaryHoldings
def overlap?(a, b)
a.include?(b.begin) || b.include?(a.begin) || a.end==b.begin || a.end+1==b.begin #this last to cover the fact that years have thickness
end
def merge_ranges(a, b) # where ranges overlap
[a.begin, b.begin].min...[a.end, b.end].max
end
def combine_ranges(a, b) # for disjoint ranges
[a, b]
end
def clean(summaryHoldings)
summaryHoldings.each{ |range|
range.gsub(/\(/,"",).gsub(/\)/,"").strip if range.include?("(")
}
end
def pp(range) # pretty-print an individual range
(range.end == Time.now.year) ? endr = " " : endr = range.end.to_s
range.begin.to_s+"-"+endr
end
def pretty_print(holdings) # pretty-print the whole summary_holdings statement
range, combined, message = holdings
statement = ""
if combined.empty?
statement = pp(range)
else # combined not empty
pretty = ""
combined.each { |a|
pretty += ", #{pp(a)}"
}
statement = pretty_print([range, [], nil])+pretty
end
statement = range.first.to_s if (range.size == 0 and range != (0...0))
statement = "Available" if (range.size == 0 and range == (0...0))
(message.nil? or message.empty?) ? statement : statement+", #{message}"
end
def compile(summaryHoldings)
thresholds = []
clean(summaryHoldings).each{ |range|
embargo = false
range_scan = range.scan(/\d{4}/)
startr = range_scan.first.to_i
message = range.split("-").last if (range_scan.count==1 and range.split("-").count == 2)
(range_scan.count == 2 or range.scan(/-/).empty?) ? endr = range_scan.last.to_i : endr = Time.now.year
embargo = true if message
thresholds << {:range =>(startr...endr), :message=>message, :embargo=>embargo}
}
thresholds
end
def merge(summaryHoldings)
merged = summaryHoldings.first[:range]
message = ""
combined = []
override_embargo = false
# merge overlapping ranges
summaryHoldings.each { |holdings|
merged = merge_ranges(merged, holdings[:range]) if overlap?(merged, holdings[:range])
message = holdings[:message] if holdings[:message] # this only needs to be done once
override_embargo = true if (holdings[:range].end == Time.now.year and holdings[:embargo] == false)
}
# combine disjoint ranges
summaryHoldings.each{ |holdings|
combined << combine_ranges(merged, holdings[:range]) unless overlap?(merged, holdings[:range])
}
combined = combined.flatten.uniq.reject{|a| overlap?(merged, a)}
if override_embargo
return merged, combined, nil
else
return merged, combined, message
end
end
end