Sha256: d91ba810669ba2e5bf8ad5ec2aba251db9eb11ee442eace0c5da92bee3212c9b

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

module Ldp
  class Orm

    attr_reader :resource
    attr_reader :last_response

    def initialize resource
      @resource = resource
    end

    def subject_uri
      resource.subject_uri
    end

    def new?
      resource.new?
    end

    def persisted?
      !new?
    end

    def graph
      Ldp.instrument 'graph.orm.ldp', subject: subject_uri do
        resource.graph
      end
    end

    def value predicate
      graph.query(:subject => subject_uri, :predicate => predicate).map do |stmt|
        stmt.object
      end
    end

    def query *args, &block
      Ldp.instrument 'query.orm.ldp', subject: subject_uri do
        graph.query *args, &block
      end
    end

    def reload
      Ldp.instrument 'reload.orm.ldp', subject: subject_uri do
        Ldp::Orm.new resource.reload
      end
    end

    def create
      Ldp.instrument 'create.orm.ldp', subject: subject_uri do
        # resource.create returns a reloaded resource which causes any default URIs (e.g. "<>")
        # in the graph to be transformed to routable URIs
        Ldp::Orm.new resource.create
      end
    end

    def save
      Ldp.instrument 'save.orm.ldp', subject: subject_uri do
        @last_response = resource.save
        @last_response.success?
      end
    rescue Ldp::HttpError => e
      @last_response = e
      logger.debug e
      false
    end

    def save!
      result = save

      if result.is_a? RDF::Graph
        raise GraphDifferenceException.new "", result
      elsif !result
        raise @last_response
      end

      result
    end

    def delete
      Ldp.instrument 'delete.orm.ldp', subject: subject_uri do
        resource.delete
      end
    end

    def method_missing meth, *args, &block
      super
    end

    def respond_to?(meth)
      super
    end

    private

    def logger
      Ldp.logger
    end
  end

  class GraphDifferenceException < Exception
    attr_reader :diff
    def initialize message, diff
      super(message)
      @diff = diff
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ldp-0.3.0 lib/ldp/orm.rb
ldp-0.2.3 lib/ldp/orm.rb
ldp-0.2.2 lib/ldp/orm.rb
ldp-0.2.1 lib/ldp/orm.rb
ldp-0.2.0 lib/ldp/orm.rb
ldp-0.1.0 lib/ldp/orm.rb
ldp-0.0.10 lib/ldp/orm.rb
ldp-0.0.9 lib/ldp/orm.rb
ldp-0.0.8 lib/ldp/orm.rb