Skip to content

Commit

Permalink
Warn when the same predicate is used in more than one property in the…
Browse files Browse the repository at this point in the history
… same class
  • Loading branch information
hectorcorrea committed Jan 9, 2015
1 parent bee58b8 commit 80d34ac
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/active_fedora/attributes.rb
Expand Up @@ -171,16 +171,23 @@ def multiple?(field)
end

def property name, properties={}, &block
warn_duplicate_predicates name, properties
properties = { multiple: true }.merge(properties)
find_or_create_defined_attribute(name, nil, properties)
raise ArgumentError, "#{name} is a keyword and not an acceptable property name." if protected_property_name? name
reflection = ActiveFedora::Attributes::PropertyBuilder.build(self, name, properties, &block)
# reflection = ActiveTriple::PropertyBuilder.build(self, name, properties, &block)
ActiveTriples::Reflection.add_reflection self, name, reflection
end

private

def warn_duplicate_predicates new_name, new_properties
new_predicate = new_properties[:predicate]
self.properties.select{|k, existing| existing.predicate == new_predicate}.each do |key, value|
ActiveFedora::Base.logger.warn "Same predicate (#{new_predicate}) used for properties #{key} and #{new_name}"
end
end

def find_or_create_defined_attribute(field, dsid, args)
delegated_attributes[field] ||= DelegatedAttribute.new(field, dsid, datastream_class_for_name(dsid), args)
end
Expand Down
40 changes: 40 additions & 0 deletions spec/unit/property_predicate_spec.rb
@@ -0,0 +1,40 @@
require 'spec_helper'

describe 'Properties with the same predicate' do

let(:warningMsg) {"Same predicate (http://purl.org/dc/terms/title) used for properties title1 and title2"}

it "should warn" do

# Note that the expect test must be before the class is parsed.
expect(ActiveFedora::Base.logger).to receive(:warn).with(warningMsg)

module TestModel1
class Book < ActiveFedora::Base
property :title1, predicate: ::RDF::DC.title
property :title2, predicate: ::RDF::DC.title
end
end

end

end


describe 'Properties with different predicate' do

it "should not warn" do

# Note that the expect test must be before the class is parsed.
expect(ActiveFedora::Base.logger).to_not receive(:warn)

module TestModel2
class Book < ActiveFedora::Base
property :title1, predicate: ::RDF::DC.title
property :title2, predicate: ::RDF::DC.creator
end
end

end

end

0 comments on commit 80d34ac

Please sign in to comment.