marcspec 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
+ 1.1.0
2
+ * Added support for Proc objects as the value in a MVMap
1
3
  1.0.0
2
4
  * Added constant specs (:constantValue=>'val' or :constantValue=>['array', 'of', 'values'])
3
5
  * Arbitrarily decided this is version 1.0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/marcspec/map.rb CHANGED
@@ -60,7 +60,7 @@ module MARCSpec
60
60
 
61
61
  # Check for map equality
62
62
  def == other
63
- return ((other.mapname == self.mapname) and (other.map = self.map))
63
+ return ((other.mapname == @mapname) and (other.map == @map))
64
64
  end
65
65
 
66
66
  # Generic pretty_print; used mostly for translating from solrmarc
@@ -13,15 +13,27 @@ module MARCSpec
13
13
  #
14
14
  # Keys can be either strings or regular expressions (e.g., /^Bil/).
15
15
  #
16
+ # IF the key is a regexp, the value may then be a Proc object that takes a single argument: the
17
+ # MatchData object produced by calling key.match(passed_in_value)
18
+ #
16
19
  # Again, note that if several keys are === to the passed argument, all the values will be returned.
17
20
 
18
21
  class MultiValueMap < Map
19
22
 
20
23
  attr_accessor :mapname,:map
21
24
 
22
-
25
+ # Given a passed_in_key (and optional default) return the set of values that match, as described
26
+ # above.
23
27
  def [] key, default=nil
24
- rv = @map.map {|pv| pv[0] === key ? pv[1] : nil}
28
+ rv = []
29
+ @map.each do |pv|
30
+ if pv[1].is_a? Proc
31
+ match = pv[0].match key
32
+ rv << pv[1].call(match) if match
33
+ else
34
+ rv << pv[1] if pv[0] === key
35
+ end
36
+ end
25
37
  rv.flatten!
26
38
  rv.compact!
27
39
  rv.uniq!
@@ -32,6 +44,9 @@ module MARCSpec
32
44
  end
33
45
  end
34
46
 
47
+ # Try to produce a valid MVMap from a solrmarc file
48
+ # @param [String] filename The file to read
49
+ # @return [MARCSpec::MultiValueMap] A new mvmap
35
50
  def self.from_solrmarc_file filename
36
51
  mapname = File.basename(filename).sub(/\..+?$/, '')
37
52
  kvlist = []
@@ -50,12 +65,16 @@ module MARCSpec
50
65
  return self.new(mapname, kvlist)
51
66
  end
52
67
 
68
+ # Produce a string suitable for pretty-printing. Unfortunately, we need to just plain
69
+ # delete the procs before doing so
70
+
53
71
  def asPPString
72
+ map = @map.reject {|kv| kv[1].is_a? Proc}
54
73
  s = StringIO.new
55
74
  s.print "{\n :maptype=>:multi,\n :mapname=>"
56
75
  PP.singleline_pp(@mapname, s)
57
76
  s.print ",\n :map => "
58
- PP.pp(@map, s)
77
+ PP.pp(map, s)
59
78
  s.puts "\n}"
60
79
  return s.string
61
80
  end
data/spec/maps_spec.rb CHANGED
@@ -3,7 +3,14 @@ require 'spec_helper'
3
3
  describe "Maps" do
4
4
  before do
5
5
  @kvmap = MARCSpec::KVMap.new('kvmap', {'one' => '1', 'two' => ['2', 'zwei']})
6
- @mvmap = MARCSpec::MultiValueMap.new('mvmap', [[/bi/, 'Bill'], [/mo/i, 'Molly'], [/ll/, 'Bill'], [/lly/i, ['One', 'Two']]])
6
+ @mvmap = MARCSpec::MultiValueMap.new('mvmap', [
7
+ [/bi/, 'Bill'],
8
+ [/mo/i, 'Molly'],
9
+ [/ll/, 'Bill'],
10
+ [/lly/i, ['One', 'Two']],
11
+ [/^.*?\s+(.*)$/, Proc.new{|m| m[1]}]
12
+ ]
13
+ )
7
14
  end
8
15
 
9
16
  it "knows its name" do
@@ -44,12 +51,35 @@ describe "Maps" do
44
51
  newkvmap.should.equal @kvmap
45
52
  end
46
53
 
47
- it "should round trip a multivaluemap" do
54
+ it "should round trip a multivaluemap without a Proc" do
55
+ cleanmap = []
56
+ @mvmap.map.each do |kv|
57
+ cleanmap.push kv unless kv[1].is_a? Proc
58
+ end
59
+ @mvmap.map = cleanmap
48
60
  s = @mvmap.asPPString
49
61
  newmvmap = MARCSpec::MultiValueMap.fromPPString s
50
62
  newmvmap.should.equal @mvmap
51
63
  end
52
64
 
65
+ it "can't round-trip a multivaluemap with a Proc" do
66
+ s = @mvmap.asPPString
67
+ puts s
68
+ newmvmap = MARCSpec::MultiValueMap.fromPPString s
69
+ newmvmap.should.not.equal @mvmap
70
+ end
71
+
72
+
73
+ it "can use a proc in a multivaluemap" do
74
+ @mvmap['Molly Dueber'].sort.should.equal ['Molly', 'Bill', 'Dueber', 'One', 'Two'].sort
75
+ end
76
+
77
+ it "can use a simple passthrough in a multivaluemap" do
78
+ @mvmap = MARCSpec::MultiValueMap.new('mvmap', [[/.*/, Proc.new {|m| m[0]}]])
79
+ @mvmap['one'].should.equal ['one']
80
+ @mvmap['two'].should.equal ['two']
81
+ end
82
+
53
83
  it "should read a kv solrmarc file" do
54
84
  map = MARCSpec::KVMap.from_solrmarc_file "#{DIR}/data/umich/translation_maps/country_map.properties"
55
85
  map.mapname.should.equal 'country_map'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marcspec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - BillDueber
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-14 00:00:00 -04:00
18
+ date: 2010-09-15 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency