Sha256: 0f281db954ba9e6b3102468f99665df4403c2b7457cce6aee15e8854c3618c25

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Xampl

  require "fileutils"
  require "xamplr/persisters/caching"

  class FilesystemPersister < AbstractCachingPersister

    def initialize(name=nil, format=nil, root=File.join(".", "repo"))
      super(root, name, format)
    end

    def FilesystemPersister.kind
      :filesystem
    end

    def kind
      FilesystemPersister.kind
    end

    def write(xampl)
      #raise XamplException.new(:no_index_so_no_persist) unless xampl.get_the_index or xampl.ignore_when_no_index
      raise XamplException.new("no_index_so_no_persist [#{xampl.class.name}]") unless xampl.get_the_index or xampl.ignore_when_no_index
      return unless xampl.get_the_index

      place = File.join(@root_dir, xampl.class.name.split("::"))

      FileUtils.mkdir_p(place) unless File.exist?(place)

      place = File.join(place, xampl.get_the_index)

      representation = represent(xampl)
      if representation then
        File.open(place, "w") do |out|
          out.puts representation
          out.fsync
          if $is_darwin then
            out.fcntl(51, 0) # Attempt an F_FULLFSYNC fcntl to commit data to disk
          end

        end
        @write_count = @write_count + 1
      end
      xampl.changes_accepted
      return true
    end

    def read_representation(klass, pid)
      place = File.join(@root_dir, klass.name.split("::"), pid)

      return nil unless File.exist?(place)
      representation = File.read(place)
      return representation
    end
  end

  Xampl.register_persister_kind(FilesystemPersister)
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
xamplr-1.9.13 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.12 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.11 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.10 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.9 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.8 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.7 lib/xamplr/persisters/filesystem.rb
xamplr-1.9.6 lib/xamplr/persisters/filesystem.rb