Class: Docs::AbstractStore

Inherits:
Object
  • Object
show all
Includes:
Instrumentable
Defined in:
lib/docs/storage/abstract_store.rb

Direct Known Subclasses

FileStore, NullStore

Defined Under Namespace

Classes: InvalidPathError, LockError

Instance Method Summary collapse

Methods included from Instrumentable

extended, included

Constructor Details

#initialize(path) ⇒ AbstractStore

Returns a new instance of AbstractStore

Raises:

  • (ArgumentError)

10
11
12
13
14
# File 'lib/docs/storage/abstract_store.rb', line 10

def initialize(path)
  path = Pathname.new(path).cleanpath
  raise ArgumentError if path.relative?
  @root_path = @working_path = path.freeze
end

Instance Method Details

#closeObject


36
37
38
# File 'lib/docs/storage/abstract_store.rb', line 36

def close
  set_working_path @root_path
end

#delete(path) ⇒ Object


56
57
58
59
60
61
62
63
# File 'lib/docs/storage/abstract_store.rb', line 56

def delete(path)
  path = expand_path(path)

  if file_exist?(path)
    destroy(path)
    true
  end
end

#each(&block) ⇒ Object


79
80
81
# File 'lib/docs/storage/abstract_store.rb', line 79

def each(&block)
  list_files(working_path, &block)
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/docs/storage/abstract_store.rb', line 65

def exist?(path)
  file_exist? expand_path(path)
end

#expand_path(path) ⇒ Object


24
25
26
# File 'lib/docs/storage/abstract_store.rb', line 24

def expand_path(path)
  join_paths @working_path, path
end

#mtime(path) ⇒ Object


69
70
71
72
# File 'lib/docs/storage/abstract_store.rb', line 69

def mtime(path)
  path = expand_path(path)
  file_mtime(path) if file_exist?(path)
end

#open(path, &block) ⇒ Object


28
29
30
31
32
33
34
# File 'lib/docs/storage/abstract_store.rb', line 28

def open(path, &block)
  if block_given?
    open_yield_close(path, &block)
  else
    set_working_path join_paths(@root_path, path)
  end
end

#read(path) ⇒ Object


40
41
42
43
# File 'lib/docs/storage/abstract_store.rb', line 40

def read(path)
  path = expand_path(path)
  read_file(path) if file_exist?(path)
end

#replace(path = nil, &block) ⇒ Object


83
84
85
86
87
88
89
# File 'lib/docs/storage/abstract_store.rb', line 83

def replace(path = nil, &block)
  if path
    return open(path) { replace(&block) }
  else
    lock { track_touched { yield.tap { delete_untouched } } }
  end
end

#root_pathObject


16
17
18
# File 'lib/docs/storage/abstract_store.rb', line 16

def root_path
  @root_path.to_s
end

#size(path) ⇒ Object


74
75
76
77
# File 'lib/docs/storage/abstract_store.rb', line 74

def size(path)
  path = expand_path(path)
  file_size(path) if file_exist?(path)
end

#working_pathObject


20
21
22
# File 'lib/docs/storage/abstract_store.rb', line 20

def working_path
  @working_path.to_s
end

#write(path, value) ⇒ Object


45
46
47
48
49
50
51
52
53
54
# File 'lib/docs/storage/abstract_store.rb', line 45

def write(path, value)
  path = expand_path(path)
  touch(path)

  if file_exist?(path)
    update(path, value)
  else
    create(path, value)
  end
end