Skip to content

Commit

Permalink
rework rdf_resource and rdf_configurable to pass the tests we've adde…
Browse files Browse the repository at this point in the history
…d so far
  • Loading branch information
Tom Johnson committed Nov 26, 2013
1 parent 4fb74a7 commit 5115df3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
9 changes: 7 additions & 2 deletions lib/oregon_digital/rdf_configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ def rdf_label
RDF::RDFS.label
end

def type
nil
end

def configure(options = {})
singleton_class.class_eval do {
:base_uri => options[:base_uri],
:rdf_label => options[:rdf_label]
:rdf_label => options[:rdf_label],
:type => options[:type]
}.each do |name, value|
# redefine reader methods only when required,
# otherwise, use the ancestor methods
Expand All @@ -32,7 +37,7 @@ def property(name, opts={}, &block)
config = ActiveFedora::Rdf::NodeConfig.new(name, opts[:predicate], :class_name => opts[:class_name]).tap do |config|
config.with_index(&block) if block_given?
end
behaviors = config.behaviors.flatten unless config.behaviors.empty?
behaviors = config.behaviors.flatten if config.behaviors and not config.behaviors.empty?
self.properties[name] = {
:behaviors => behaviors,
:type => config.type,
Expand Down
32 changes: 22 additions & 10 deletions lib/oregon_digital/rdf_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,51 @@ def base_uri
self.class.base_uri
end

def type
@type ||= self.class.type
end

def type=(type)
raise "Type must be an RDF::URI" unless type.respond_to? :to_uri
@type = type.to_uri
self.set_value(RDF::RDFS.type, type)
end

def rdf_label
get_values(self.class.rdf_label)
end

def set_value(property, values)
values = [values] if values.kind_of? RDF::Graph
values = Array(values)
delete([subject, predicate_for_property(property), nil])
property_class = class_for_property(property)
property = predicate_for_property(property) unless property.kind_of? RDF::URI
delete([subject, property, nil])
values.each do |val|
val = RDF::Literal(val) if val.kind_of? String
# warn("Warning: #{val.to_s} is not of class #{class_for_property(property)}.") unless val.kind_of? class_for_property(property) or class_for_property(property) == nil
# warn("Warning: #{val.to_s} is not of class #{property_class}.") unless val.kind_of? property_class or property_class == nil
if val.kind_of? RdfResource
add_node(property, val)
next
end
val = val.to_uri if val.respond_to? :to_uri
raise 'value must be an RDF URI, Node, Literal, or a plain string' unless
val.kind_of? RDF::Resource or val.kind_of? RDF::Literal
insert [subject, predicate_for_property(property), val]
insert [subject, property, val]
end
end

def get_values(property)
values = []
# property = predicate_for_property(property) unless property.kind_of? RDF::URI
query(:subject => subject, :predicate => predicate_for_property(property)).each_statement do |statement|
property = predicate_for_property(property) unless property.kind_of? RDF::URI
query(:subject => subject, :predicate => property).each_statement do |statement|
value = statement.object
value = value.to_s if value.kind_of? RDF::Literal
values << value
end
values
end

def add_node(property, resource)
insert [subject, predicate_for_property(property), resource.subject]
end

def set_subject!(uri_or_str)
raise "Refusing update URI when one is already assigned!" unless subject.node?
statements = query(:subject => subject)
Expand Down Expand Up @@ -80,7 +88,11 @@ def predicate_for_property(property)
end

def class_for_property(property)
self.class.properties[property][:class_name]
self.class.properties[property][:class_name] if self.class.properties.include? property
end

def add_node(property, resource)
insert [subject, predicate_for_property(property), resource.subject]
end

end
Expand Down

0 comments on commit 5115df3

Please sign in to comment.