Sha256: 76ce6f0c07ff897172f7584a262fbbf0768df812e7220caba4041ccda815d779

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require 'active_support'
require 'active_support/core_ext/module/delegation'

module ActiveTriples::Identifiable
  extend ActiveSupport::Concern

  delegate :rdf_subject, :type, to: :resource

  ##
  # @return [ActiveTriples::Resource] a resource that contains this object's 
  # graph.
  def resource
   @resource ||= resource_class.new(to_uri)
  end
  
  def parent
    @parent ||= resource.parent
  end

  def parent=(val)
    @parent = val
  end

  ## 
  # @return [String] a uri or slug
  def to_uri
    return id if respond_to? :id and !resource_class.base_uri.nil?
    raise NotImplementedError
  end

  private
    def resource_class
      self.class.resource_class
    end

    def update_resource(&block)
      resource_class.properties.each do |name, property|
        if block_given?
          yield name, property
        else
          resource.set_value(property.predicate, self.send(property.term))
        end
      end
    end

  public

    module ClassMethods

      delegate :configure, :property, :properties, to: :resource_class

      def resource_class
        @resource_class ||= Class.new(ActiveTriples::Resource)
      end

      def from_uri(uri, *args)
        raise NotImplementedError
      end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active-triples-0.4.1 lib/active_triples/identifiable.rb
active-triples-0.4.0 lib/active_triples/identifiable.rb
active-triples-0.3.1 lib/active_triples/identifiable.rb