class SocialSnippet::StorageBackend::MongoidStorage

Attributes

model[R]
paths[R]
workdir[R]

Public Class Methods

activate!() click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 113
def self.activate!
  ::SocialSnippet.class_eval do
    remove_const :Storage if defined?(::SocialSnippet::Storage)
    const_set :Storage, ::SocialSnippet::StorageBackend::MongoidStorage
  end
end
new() click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 12
def initialize
  @workdir = "/"
  @model = Model.find_or_create_by(:id => "social_snippet_storage")
  @paths = ::SortedSet.new model.paths
end

Public Instance Methods

cd(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 18
def cd(path)
  raise ::Errno::ENOENT unless exists?(path)
  if absolute?(path)
    @workdir = normalize(path)
  else
    @workdir = resolve(path)
  end
end
directory?(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 88
def directory?(path)
  realpath = resolve(path)
  paths.include? dirpath(realpath)
end
exists?(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 68
def exists?(path)
  realpath = resolve(path)
  paths.include?(realpath) ||
    paths.include?(dirpath realpath)
end
file?(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 93
def file?(path)
  return false if directory?(path)
  realpath = resolve(path)
  paths.include? realpath
end
glob(pattern) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 99
def glob(pattern)
  real_pattern = resolve(pattern)
  pattern_dir = dirpath(::File.dirname real_pattern)
  paths.select do |path|
    next if directory?(path) && path.end_with?("/")
    next if pattern_dir === path
    ::File.fnmatch real_pattern, path, ::File::FNM_PATHNAME
  end
end
mkdir(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 56
def mkdir(path)
  realpath = resolve(path)
  raise ::Errno::EEXIST if exists?(realpath)
  mkdir_p path
end
mkdir_p(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 62
def mkdir_p(path)
  realpath = resolve(path)
  raise ::Errno::EEXIST if file?(realpath)
  add_dir realpath
end
pwd() click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 109
def pwd
  workdir
end
read(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 43
def read(path)
  realpath = resolve(path)
  raise ::Errno::EISDIR if directory?(path)
  begin
    file = File.find_by(
      :path => realpath,
    )
  rescue ::Mongoid::Errors::DocumentNotFound
    raise ::Errno::ENOENT
  end
  file.content
end
rm(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 74
def rm(path)
  raise ::Errno::ENOENT unless exists?(path)
  realpath = resolve(path)
  delete_path realpath
end
rm_r(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 80
def rm_r(path)
  raise ::Errno::ENOENT unless exists?(path)
  realpath = resolve(path)
  paths.each do |tmp_path|
    delete_path tmp_path if tmp_path.start_with?(realpath)
  end
end
touch(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 27
def touch(path)
  raise ::Errno::ENOENT unless exists?(::File.dirname path)
  realpath = resolve(path)
  add_path realpath
end
write(path, content) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 33
def write(path, content)
  realpath = resolve(path)
  raise ::Errno::EISDIR if directory?(path)
  add_path realpath
  file = File.find_or_create_by(:path => realpath)
  file.update_attributes(
    :content => content,
  )
end

Private Instance Methods

absolute?(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 152
def absolute?(path)
  ::Pathname.new(path).absolute?
end
add_dir(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 122
def add_dir(path)
  return if exists?(path)
  add_path path
  add_path dirpath(path)
end
add_parent_dir(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 128
def add_parent_dir(path)
  return if exists?(path)
  items = path.split(::File::SEPARATOR)
  items.pop
  items.inject(::Array.new) do |tmp, item|
    tmp.push item
    add_dir ::File.join(*items)
    tmp
  end
end
add_path(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 139
def add_path(path)
  add_parent_dir path
  unless paths.include?(path)
    paths.add path
    model.push :paths => path
  end
end
delete_path(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 147
def delete_path(path)
  paths.delete path
  model.pull :paths => path
end
dirpath(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 164
def dirpath(path)
  path + "/"
end
normalize(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 168
def normalize(path)
  ::Pathname.new(path).cleanpath.to_s
end
resolve(path) click to toggle source
# File lib/social_snippet/storage_backend/mongoid_storage.rb, line 156
def resolve(path)
  if absolute?(path)
    normalize(path)
  else
    normalize(::File.join workdir, path)
  end
end