Class: RDF::LDP::NonRDFSource::FileStorageAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/ldp/storage_adapters/file_storage_adapter.rb

Overview

TODO:

check thread saftey on write for base implementation

StorageAdapters bundle the logic for mapping a ‘NonRDFSource` to a specific IO stream. Implementations must conform to a minimal interface:

- `#initailize` must accept a `resource` parameter. The input should be
   a `NonRDFSource` (LDP-NR).
- `#io` must yield and return a IO object in binary mode that represents
  the current state of the LDP-NR.
  - If a block is passed to `#io`, the implementation MUST allow return a
    writable IO object and that anything written to the stream while
    yielding is synced with the source in a thread-safe manner.
  - Clients not passing a block to `#io` SHOULD call `#close` on the
    object after reading it.
  - If the `#io` object responds to `#to_path` it MUST give the location
    of a file whose contents are identical the IO object's. This supports
    Rack's response body interface.
- `#delete` remove the contents from the corresponding storage. This MAY
    be a no-op if is undesirable or impossible to delete the contents
    from the storage medium.

Beyond this interface, implementations are permitted to behave as desired. They may, for instance, reject undesirable content or alter the graph (or metagraph) of the resource. They should throw appropriate ‘RDF::LDP` errors when failing to allow the middleware to handle response codes and messages.

The base storage adapter class provides a simple File storage implementation.

Examples:

reading from a ‘StorageAdapter`

storage = StorageAdapter.new(an_nr_source)
storage.io.read # => [string contents of `an_nr_source`]

writing to a ‘StorageAdapter`

storage = StorageAdapter.new(an_nr_source)
storage.io { |io| io.write('moomin') }

See Also:

Constant Summary collapse

STORAGE_PATH =
'.storage'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ FileStorageAdapter

Initializes the storage adapter.

Parameters:



51
52
53
# File 'lib/rdf/ldp/storage_adapters/file_storage_adapter.rb', line 51

def initialize(resource)
  @resource = resource
end

Instance Method Details

#deleteBoolean

Returns 1 if the file has been deleted, otherwise false.

Returns:

  • (Boolean)

    1 if the file has been deleted, otherwise false



74
75
76
77
# File 'lib/rdf/ldp/storage_adapters/file_storage_adapter.rb', line 74

def delete
  return false unless File.exist?(path)
  File.delete(path)
end

#io {|IO| ... } ⇒ IO

Gives an IO object which represents the current state of @resource. Opens the file for read-write (mode: r+), if it already exists; otherwise, creates the file and opens it for read-write (mode: w+).

Yields:

  • (IO)

    yields a read-writable object conforming to the Ruby IO interface for storage. The IO object will be closed when the block ends.

Returns:

  • (IO)

    an object conforming to the Ruby IO interface



65
66
67
68
69
70
# File 'lib/rdf/ldp/storage_adapters/file_storage_adapter.rb', line 65

def io(&block)
  FileUtils.mkdir_p(path_dir) unless Dir.exist?(path_dir)
  FileUtils.touch(path) unless file_exists?

  File.open(path, 'r+b', &block)
end