lib/active_triples/rdf_source.rb in active-triples-1.1.1 vs lib/active_triples/rdf_source.rb in active-triples-1.2.0

- old
+ new

@@ -90,15 +90,13 @@ delegate :query, :each, :load!, :count, :has_statement?, to: :graph ## # @!method to_base # @return (see RDF::Term#to_base) - # @!method term? - # @return (see RDF::Term#term?) # @!method escape # @return (see RDF::Term#escape) - delegate :to_base, :term?, :escape, to: :to_term + delegate :to_base, :escape, to: :to_term ## # Initialize an instance of this resource class. Defaults to a # blank node subject. In addition to RDF::Graph parameters, you # can pass in a URI and/or a parent to build a resource from a @@ -108,11 +106,11 @@ # new(nil, parent) # # @see RDF::Graph # @todo move this logic out to a Builder? def initialize(*args, &block) - @observers = Set.new + @observers = Set.new resource_uri = args.shift unless args.first.is_a?(Hash) @rdf_subject = get_uri(resource_uri) if resource_uri if args.first.is_a?(Hash) || args.empty? @@ -120,11 +118,16 @@ else set_persistence_strategy(ParentStrategy) persistence_strategy.parent = args.shift end - persistence_strategy.graph = RDF::Graph.new(*args, &block) + graph_params = if args.empty? || args.first.nil? + {} + else + args.shift + end + persistence_strategy.graph = RDF::Graph.new(**graph_params, &block) reload # Append type to graph if necessary. Array.wrap(self.class.type).each do |type| get_values(:type) << type unless get_values(:type).include?(type) @@ -396,13 +399,13 @@ # @yield gives self to block if this is a node, or an error is raised during # load # @yieldparam [ActiveTriples::RDFSource] resource self # # @return [ActiveTriples::RDFSource] self - def fetch(*args, &_block) + def fetch(**args, &_block) begin - load(rdf_subject, *args) + load(rdf_subject, **args) rescue => e if block_given? yield(self) else raise "#{self} is a blank node; " \ @@ -573,10 +576,30 @@ # @return [Boolean] def new_record? !persisted? end + ## + # @overload term? + # Returns `false` indicating this is not an RDF::Statemenet. + # @see RDF::Value#statement? + # @return [Boolean] + # @overload term?(value) + # Returns `true` if `self` contains the given RDF subject term. + # + # @param [RDF::Resource] value + # @return [Boolean] + # + # See RDF::Enumerable#term? + def term?(*args) + case args.length + when 0 then to_term.term? + when 1 then args.first && graph.term?(args.first) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end + end + def mark_for_destruction @marked_for_destruction = true end def marked_for_destruction? @@ -639,18 +662,18 @@ # # @param [RDF::Term] old_subject # @param [RDF::Term] new_subject # @return [void] def rewrite_statement_uris(old_subject, new_subject) - graph.query(subject: old_subject).each do |st| + graph.query([old_subject, nil, nil]).each do |st| graph.delete(st) st.subject = new_subject st.object = new_subject if st.object == old_subject graph.insert(st) end - graph.query(object: old_subject).each do |st| + graph.query([nil, nil, old_subject]).each do |st| graph.delete(st) st.object = new_subject graph.insert(st) end