active_fedora-crosswalks 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Y2Q0OGFjNmNkNzU3NTJjODQ2NTc2ODljZmZjYWZlZTM1ZmQ4Mzk3Mw==
4
+ MTZjYjFiNmFjMjlhNTQxMWE0YmQ2ZWVmY2EzY2Y3NTRkYjY1MTNlOQ==
5
5
  data.tar.gz: !binary |-
6
- NTEwMGFkMjExNzgyMDU2MWIxZWMwMzJhOWNlMjhkNWE3MjI2MjBmYw==
6
+ YTg3MzIzMzcyN2I4OTIwY2E1NWMzZDAyMjAxZTQ3MWI1OWY4Yjc5MQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZjU3OWZmOWE4OGYwNjc0ODM5ZTQ3NjQ1ZDVmZmY5NDk1YjdkY2ZhMWJhOWFm
10
- ZTE3ZTVjYzk5MzU5Nzg0NjY0YjQ2ZjhlMjlkNjAzNDVjYTRiNmQ2MDk2MWY3
11
- NDVhMTNjOGNhNmFkMmUzYzVmMDk2MWM0YzU0MjYyOWI1ZTA4NzM=
9
+ NDMxMDhkZDI4NWRkNDIzMDg2NWE1MTc3NTViNzM0OGVhZTkwY2ViYjY2YzY5
10
+ ZDE4NWNjM2VhM2I5OTYzMzAxZGZkNGYwMGQxYjU5OGIyYzkyM2RjNDI5ZmNl
11
+ ZDkxN2Y0MWVlODdhNDNjNTNmMTk1YTQyNmRiNzJkODIzZTJmOTY=
12
12
  data.tar.gz: !binary |-
13
- MTcxZTkxODRhMzkzMzk5ODA5MDVhNGEwMjNhMTM5YmQ2ZDE4ZmJiN2YyOTIz
14
- MWQ5YTBiZjk3ZTVlZDdiMzRhNGY0YTM2MGQ3YmFmNjNhMTEwN2U4YTUwYWVl
15
- NjViYzA0YTA4Y2Q2NGU0YjYxZGMxMTVjOTAyYTVhYWY5N2NhYjk=
13
+ MzNiOGQ1Y2JiMzgwMWZiMmIyMTIwYzk3OGU3Y2E2NGEwZWFhYjc3OWQ5NjQ1
14
+ MGQyMzk4MzFkNWRjZmE1MjljNzRhMTc5YzZjNGYwM2UzODlmYjJkZGE3MzFm
15
+ NzkwNDhjNjE4OTRmNDkwNmNmN2RhODAxOGZlNGFkODQxZDE5YTE=
@@ -6,9 +6,11 @@ module ActiveFedora
6
6
  def self.new(datastream, field)
7
7
  if self.to_s.include?("GenericAccessor")
8
8
  if datastream.kind_of?(ActiveFedora::RelsExtDatastream)
9
+ puts "Returning a rels accessor"
9
10
  return RelsExtAccessor.new(datastream, field)
10
11
  end
11
12
  if datastream.kind_of?(ActiveFedora::OmDatastream)
13
+ puts "Returning an OM Accessor"
12
14
  return OmAccessor.new(datastream, field)
13
15
  end
14
16
  end
@@ -27,10 +29,25 @@ module ActiveFedora
27
29
  def get_value
28
30
  FieldProxy.new(Array.wrap(datastream.send(field.to_s)), self, field)
29
31
  end
32
+ def original_get_value
33
+ if datastream.respond_to?("unstubbed_#{self.field}")
34
+ FieldProxy.new(Array.wrap(datastream.send("unstubbed_#{self.field}")), self, field)
35
+ else
36
+ get_value
37
+ end
38
+ end
30
39
  def set_value(*args)
31
40
  value = args.last
32
41
  datastream.send("#{field.to_s}=",value)
33
42
  end
43
+ def original_set_value(*args)
44
+ value = args.last
45
+ if datastream.respond_to?("unstubbed_#{self.field}=")
46
+ datastream.send("unstubbed_#{self.field}=",value)
47
+ else
48
+ set_value(value)
49
+ end
50
+ end
34
51
  end
35
52
  end
36
53
  end
@@ -15,6 +15,8 @@ module ActiveFedora
15
15
  value = Array.wrap(args.last)
16
16
  datastream.send(:update_indexed_attributes, {field => value})
17
17
  end
18
+ alias_method :original_get_value, :get_value
19
+ alias_method :original_set_value, :set_value
18
20
  end
19
21
  end
20
22
  end
@@ -18,6 +18,8 @@ module ActiveFedora
18
18
  datastream.model.add_relationship(field.to_sym, v)
19
19
  end
20
20
  end
21
+ alias_method :original_get_value, :get_value
22
+ alias_method :original_set_value, :set_value
21
23
  end
22
24
  end
23
25
  end
@@ -4,8 +4,13 @@ module ActiveFedora
4
4
  include ActiveModel::Validations
5
5
  validates :parent, :field, :to, :presence => true
6
6
  validate :parent_has_datastream
7
- attr_accessor :parent, :datastream, :target_datastream, :field, :to
7
+ attr_accessor :parent, :datastream, :target_datastream, :field, :to, :transform, :reverse_transform
8
8
  def initialize(args)
9
+ @transform = args.delete(:transform)
10
+ @reverse_transform = args.delete(:reverse_transform)
11
+ unless transform && reverse_transform || (!transform && !reverse_transform)
12
+ raise "If a transform is provided, then a reverse transform must be as well."
13
+ end
9
14
  @datastream = args.delete(:datastream)
10
15
  @parent = @datastream.digital_object if @datastream
11
16
  @field = args.delete(:field)
@@ -26,6 +31,7 @@ module ActiveFedora
26
31
  datastream.crosswalk_fields << field
27
32
  create_reader
28
33
  create_writer
34
+ sync_values
29
35
  end
30
36
 
31
37
  def source_accessor
@@ -36,28 +42,49 @@ module ActiveFedora
36
42
  @target_accessor ||= Accessors::GenericAccessor.new(target_datastream, to)
37
43
  end
38
44
 
45
+ def sync_values(opts={})
46
+ current_source_values = Array.wrap(source_accessor.original_get_value)
47
+ current_target_values = perform_transform(Array.wrap(target_accessor.original_get_value))
48
+ combined = current_source_values | current_target_values
49
+ combined = current_source_values if opts[:force_target]
50
+ source_accessor.original_set_value(combined)
51
+ target_accessor.original_set_value(perform_reverse_transform(combined))
52
+ end
53
+
39
54
  protected
40
55
 
56
+ def perform_transform(values)
57
+ values.map!{|x| transform.call(x)} if transform
58
+ return values
59
+ end
60
+
61
+ def perform_reverse_transform(values)
62
+ values.map!{|x| reverse_transform.call(x)} if reverse_transform
63
+ return values
64
+ end
65
+
41
66
  def create_reader
42
67
  object, method, expected_args = source_accessor.get_reader
43
68
  crosswalker = self
69
+ object.singleton_class.class_eval do
70
+ alias_method "unstubbed_#{method}".to_sym, method.to_sym
71
+ end
44
72
  object.define_singleton_method(method) do |*args|
45
- if !expected_args || args[0..-2] == expected_args || (args.last.kind_of?(Hash) && args.last.has_key?(expected_args))
46
- crosswalker.source_accessor.set_value(crosswalker.target_accessor.get_value)
47
- end
48
- crosswalker.target_accessor.get_value
73
+ crosswalker.sync_values
74
+ FieldProxy.new(crosswalker.source_accessor.original_get_value, crosswalker.source_accessor, crosswalker.source_accessor.field)
49
75
  end
50
76
  end
51
77
 
52
78
  def create_writer
53
79
  object, method, expected_args = source_accessor.get_writer
54
80
  crosswalker = self
81
+ object.singleton_class.class_eval do
82
+ alias_method "unstubbed_#{method}".to_sym, method.to_sym
83
+ end
55
84
  object.define_singleton_method(method) do |*args|
56
- if !expected_args || args[0..-2] == expected_args || (args.last.kind_of?(Hash) && args.last.has_key?(expected_args))
57
- result = super(args.last)
58
- crosswalker.target_accessor.set_value(result)
59
- return result
60
- end
85
+ super(*args)
86
+ crosswalker.sync_values(:force_target => true)
87
+ FieldProxy.new(crosswalker.source_accessor.get_value, crosswalker.source_accessor, crosswalker.source_accessor.field)
61
88
  end
62
89
  end
63
90
 
@@ -1,5 +1,5 @@
1
1
  module ActiveFedora
2
2
  module Crosswalks
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -9,7 +9,7 @@ describe ActiveFedora::Crosswalks::Crosswalkable do
9
9
  after(:each) do
10
10
  Object.send(:remove_const, :CrosswalkAsset)
11
11
  end
12
- context "crosswalking RDF to RDF" do
12
+ describe "crosswalking RDF to RDF" do
13
13
  before(:each) do
14
14
  CrosswalkAsset.has_metadata :name => 'descMetadata', :type => ExampleRdfDatastream
15
15
  CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
@@ -71,7 +71,99 @@ describe ActiveFedora::Crosswalks::Crosswalkable do
71
71
  end
72
72
  end
73
73
  end
74
- context "crosswalking RDF to RELS-EXT" do
74
+ describe "transforms" do
75
+ context "when you pass a transform" do
76
+ context "but you don't pass a reverse transform" do
77
+ before(:each) do
78
+ CrosswalkAsset.has_metadata :name => 'descMetadata', :type => ExampleRdfDatastream
79
+ CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
80
+ ds.crosswalk :field => :title, :to => :other_title, :in => :descMetadata, :transform => Proc.new{|x| "bla_#{x}"}
81
+ end
82
+ end
83
+ it "should raise an error when you try to initialize the object" do
84
+ expect{asset}.to raise_error
85
+ end
86
+ end
87
+ context "as well as a reverse transform (for RDF)" do
88
+ before(:each) do
89
+ CrosswalkAsset.has_metadata :name => 'descMetadata', :type => ExampleRdfDatastream
90
+ CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
91
+ ds.crosswalk :field => :title, :to => :other_title, :in => :descMetadata, :transform => Proc.new{|x| "bla_#{x}"},
92
+ :reverse_transform => Proc.new{|x| x.split("bla_").last}
93
+ end
94
+ end
95
+ context "when a field is set" do
96
+ context "on the source datastream" do
97
+ before(:each) do
98
+ asset.descMetadata.other_title = "bla"
99
+ end
100
+ it "should transform the value" do
101
+ expect(asset.xwalkMetadata.title).to eq ["bla_bla"]
102
+ end
103
+ it "should leave the source datastream correct" do
104
+ expect(asset.descMetadata.other_title).to eq ["bla"]
105
+ end
106
+ end
107
+ context "on the crosswalked datastream" do
108
+ before(:each) do
109
+ asset.xwalkMetadata.title = "bla_bla"
110
+ end
111
+ it "should reverse transform the value for the source datastream" do
112
+ expect(asset.descMetadata.other_title).to eq ["bla"]
113
+ end
114
+ it "should leave the crosswalk datastream correct" do
115
+ expect(asset.xwalkMetadata.title).to eq ["bla_bla"]
116
+ end
117
+ end
118
+ end
119
+ end
120
+ context "as well as a reverse transform (for Rels)" do
121
+ before(:each) do
122
+ CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
123
+ ds.crosswalk :field => :title, :to => :is_member_of_collection, :in => "RELS-EXT", :transform => Proc.new{|x| x.split("info:fedora/").last},
124
+ :reverse_transform => Proc.new{|x| "info:fedora/#{x}" }
125
+ end
126
+ end
127
+ context "when a field exists prior to crosswalking (previous data in graph)" do
128
+ before(:each) do
129
+ asset.xwalkMetadata.set_value(asset.xwalkMetadata.rdf_subject, :title, "test:testing")
130
+ end
131
+ it "should maintain that value" do
132
+ expect(asset.xwalkMetadata.title).to eq ["test:testing"]
133
+ end
134
+ it "should perform the transform to rels" do
135
+ asset.xwalkMetadata.title # Have to call this to perform the crosswalk right now. Calls sync_values.
136
+ expect(asset.relationships(:is_member_of_collection)).to eq ["info:fedora/test:testing"]
137
+ end
138
+ end
139
+ context "when a field is set" do
140
+ context "on the source datastream" do
141
+ before(:each) do
142
+ asset.add_relationship(:is_member_of_collection, "info:fedora/test:test")
143
+ end
144
+ it "should transform the value" do
145
+ expect(asset.xwalkMetadata.title).to eq ["test:test"]
146
+ end
147
+ it "should leave the source datastream correct" do
148
+ expect(asset.relationships(:is_member_of_collection)).to eq ["info:fedora/test:test"]
149
+ end
150
+ end
151
+ context "on the crosswalked datastream" do
152
+ before(:each) do
153
+ asset.xwalkMetadata.title = "test:test"
154
+ end
155
+ it "should reverse transform the value for the source datastream" do
156
+ expect(asset.relationships(:is_member_of_collection)).to eq ["info:fedora/test:test"]
157
+ end
158
+ it "should leave the crosswalk datastream correct" do
159
+ expect(asset.xwalkMetadata.title).to eq ["test:test"]
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ describe "crosswalking RDF to RELS-EXT" do
75
167
  before(:each) do
76
168
  CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
77
169
  ds.crosswalk :field => :title, :to => :is_member_of_collection, :in => "RELS-EXT"
@@ -122,7 +214,7 @@ describe ActiveFedora::Crosswalks::Crosswalkable do
122
214
  end
123
215
  end
124
216
  end
125
- context "crosswalking RDF to OM" do
217
+ describe "crosswalking RDF to OM" do
126
218
  before(:each) do
127
219
  CrosswalkAsset.has_metadata :name => 'descMetadata', :type => DummyOmDatastream
128
220
  CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => ExampleRdfDatastream do |ds|
@@ -162,12 +254,12 @@ describe ActiveFedora::Crosswalks::Crosswalkable do
162
254
  asset.xwalkMetadata.title << "test"
163
255
  end
164
256
  it "should add the metadata to the source field" do
165
- expect(asset.descMetadata.name.family_name).to eq ["Horn", "test", "Caesar"]
257
+ expect(asset.descMetadata.name.family_name).to eq ["Horn", "Caesar", "test"]
166
258
  end
167
259
  end
168
260
  end
169
261
  end
170
- context "crosswalking OM to RDF" do
262
+ describe "crosswalking OM to RDF" do
171
263
  before(:each) do
172
264
  CrosswalkAsset.has_metadata :name => 'descMetadata', :type => ExampleRdfDatastream
173
265
  CrosswalkAsset.has_metadata :name => 'xwalkMetadata', :type => DummyOmDatastream do |ds|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fedora-crosswalks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trey Terrell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-02 00:00:00.000000000 Z
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler