lib/spread2rdf/mapping/cell.rb in spread2rdf-0.1.2 vs lib/spread2rdf/mapping/cell.rb in spread2rdf-0.1.3

- old
+ new

@@ -33,19 +33,28 @@ value.blank? end def map_to_object(value) case schema.object_mapping_mode - when :to_string then value + when :to_string then map_to_literal(value) when :resource_ref then resolve_resource_ref when :new_resource then create_resource_object - when :custom then exec(&schema.cell_mapping) + when :custom then exec(&schema.cell_mapping) else raise 'internal error: unknown column mapping type' end end private :map_to_object + def map_to_literal(value) + if language = schema.try(:object).try(:fetch, :language, nil) + RDF::Literal.new(value, language: language.to_sym) + else + value + end + end + private :map_to_literal + def resolve_resource_ref source = schema.object[:from] if source[:worksheet] && result = resolve_resource_ref_from_worksheet(source[:worksheet]) return result elsif source[:data_source] && result = resolve_resource_ref_from_data_sources(source[:data_source]) @@ -54,18 +63,15 @@ raise "#{self}: couldn't find a resource for #{value} in any of the defined sources" end end private :resolve_resource_ref - def resolve_resource_ref_from_worksheet(worksheet) - worksheet = spreadsheet.worksheet(worksheet) - raise "#{self}: couldn't find source worksheet #{source[:worksheet]}" if worksheet.nil? + def resolve_resource_ref_from_worksheet(worksheet_name) + worksheet = spreadsheet.worksheet(worksheet_name) + raise "#{self}: couldn't find source worksheet #{worksheet_name}" if worksheet.nil? source_predicate = RDF::RDFS.label # TODO: make this configurable via a attribute in the schema definition - result = worksheet.graph.query([nil, source_predicate, value]) - return nil if result.empty? - raise "#{self}: found multiple resources for #{value} in #{worksheet}: #{result.map(&:subject)}" if result.count > 1 - result.first.subject + query_subject(worksheet.graph, source_predicate, value, worksheet) end private :resolve_resource_ref_from_worksheet def resolve_resource_ref_from_data_sources(data_sources) raise ArgumentError, "expecting an Array, but got #{data_sources}" unless data_sources.is_a? Array @@ -77,15 +83,20 @@ end private :resolve_resource_ref_from_data_sources def resolve_resource_ref_from_data_source(data_source) source_predicate = RDF::RDFS.label # TODO: make this configurable via a attribute in the schema definition - result = data_source.query([nil, source_predicate, value]) + query_subject(data_source, source_predicate, value) + end + private :resolve_resource_ref_from_data_source + + def query_subject(data_source, predicate, value, data_source_name = nil) + data_source_name ||= "data source #{data_source}" + result = data_source.query([nil, predicate, map_to_literal(value)]) return nil if result.empty? - raise "#{self}: found multiple resources for #{value} in data sources: #{result.map(&:subject)}" if result.count > 1 + raise "#{self}: found multiple resources for #{value} in #{data_source_name}: #{result.map(&:subject)}" if result.count > 1 result.first.subject end - private :resolve_resource_ref_from_data_source def create_resource_object case when (schema.object.try(:fetch, :uri, nil) || object) == :bnode RDF::Node.new