module FedoraLens::Core

Public Class Methods

new(subject_or_data = {}, data = nil) click to toggle source
# File lib/fedora_lens/core.rb, line 56
def initialize(subject_or_data = {}, data = nil)
  init_core(subject_or_data, data)
end

Public Instance Methods

delete() click to toggle source
# File lib/fedora_lens/core.rb, line 78
def delete
  @orm.resource.delete
end
errors() click to toggle source
# File lib/fedora_lens/core.rb, line 62
def errors
  obj = Object.new
  def obj.[](key)         [] end
  def obj.full_messages() [] end
  obj
end
id() click to toggle source
# File lib/fedora_lens/core.rb, line 98
def id
  self.class.uri_to_id(URI.parse(uri)) if uri.present?
end
new_record?() click to toggle source
# File lib/fedora_lens/core.rb, line 90
def new_record?
  @orm.resource.new?
end
persisted?() click to toggle source
# File lib/fedora_lens/core.rb, line 60
def persisted?()      false end
read_attribute_for_validation(key) click to toggle source
# File lib/fedora_lens/core.rb, line 69
def read_attribute_for_validation(key)
  @attributes[key]
end
reload() click to toggle source
# File lib/fedora_lens/core.rb, line 73
def reload
  @orm = @orm.reload
  @attributes = get_attributes_from_orm(@orm)
end
save() click to toggle source
# File lib/fedora_lens/core.rb, line 82
def save
  new_record? ? create_record : update_record
end
save!() click to toggle source
# File lib/fedora_lens/core.rb, line 86
def save!
  save || raise(RecordNotSaved)
end
uri() click to toggle source
# File lib/fedora_lens/core.rb, line 94
def uri
  @orm.try(:resource).try(:subject_uri).try(:to_s)
end

Protected Instance Methods

create_record() click to toggle source
# File lib/fedora_lens/core.rb, line 122
def create_record
  push_attributes_to_orm
  create_and_fetch_attributes
  true
end
init_core(subject_or_data = {}, data = nil) click to toggle source

This allows you to overide the initializer, but still use this behavior

# File lib/fedora_lens/core.rb, line 104
def init_core(subject_or_data = {}, data = nil)
  case subject_or_data
    when Ldp::Resource::RdfSource
      @orm = Ldp::Orm.new(subject_or_data)
      @attributes = get_attributes_from_orm(@orm)
    when NilClass, Hash
      data = subject_or_data || {}
      @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, nil, RDF::Graph.new, FedoraLens.host + FedoraLens.base_path))
      @attributes = data.with_indifferent_access
    when String
      data ||= {}
      @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, subject_or_data, RDF::Graph.new))
      @attributes = data.with_indifferent_access
    else
      raise ArgumentError, "#{subject_or_data.class} is not acceptable"
    end
end
update_record() click to toggle source
# File lib/fedora_lens/core.rb, line 128
def update_record
  push_attributes_to_orm
  update_and_fetch_attributes
end

Private Instance Methods

clear_cached_response() click to toggle source

TODO this causes slowness because we're losing the cached GET response. However it prevents ETag exceptions caused when a subnode is added before an update.

# File lib/fedora_lens/core.rb, line 156
def clear_cached_response
  # This strips the current cached response (and ETag) from the current ORM to avoid ETag exceptions on the next update,
  # since the etag will change if a child node is added.
  @orm = Ldp::Orm.new @orm.resource.class.new @orm.resource.client, @orm.resource.subject
end
create_and_fetch_attributes() click to toggle source
# File lib/fedora_lens/core.rb, line 146
def create_and_fetch_attributes
    @orm = orm.create
    # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#created
    # TODO perhaps attributes could be lazily fetched
    @attributes = get_attributes_from_orm(@orm)
    clear_cached_response
end
get_attributes_from_orm(orm) click to toggle source
# File lib/fedora_lens/core.rb, line 167
def get_attributes_from_orm(orm)
  self.class.orm_to_hash.get(orm).with_indifferent_access
end
push_attributes_to_orm() click to toggle source
# File lib/fedora_lens/core.rb, line 163
def push_attributes_to_orm
  @orm = self.class.orm_to_hash.put(@orm, @attributes)
end
update_and_fetch_attributes() click to toggle source
# File lib/fedora_lens/core.rb, line 136
def update_and_fetch_attributes
  orm.save!.tap do
    clear_cached_response
    # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#lastModified
    # TODO perhaps attributes could be lazily fetched
    @attributes = get_attributes_from_orm(@orm)
    clear_cached_response
  end
end