Module: Adapter::File

Defined in:
lib/adapter/file.rb,
lib/adapter/file/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#base_directory(root = './') ⇒ Object

private



89
90
91
# File 'lib/adapter/file.rb', line 89

def base_directory(root='./')
  @base_directory ||= ::File.expand_path(client)
end

#clearObject



69
70
71
72
73
74
75
76
77
# File 'lib/adapter/file.rb', line 69

def clear
   delete('', {:recursive=>true}) 
  # if ::File.directory?(base_directory)
  #   delete('', {:recursive=>true}) 
  # else
  #   p [:client, client]
  #   p [:base_dir, base_directory,  ::File.directory?(base_directory)]
  # end
end

#decode(value) ⇒ Object



83
84
85
# File 'lib/adapter/file.rb', line 83

def decode(value)
  value
end

#delete(key, options = {:recursive=>false}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/adapter/file.rb', line 49

def delete(key, options={:recursive=>false})
  if key?(key)
    value=read(key)
    if ::File.file?(full_key(key))
      ::File.delete(full_key(key))
    elsif ::File.directory?(full_key(key))
      if options[:recursive]
      keys.each{|f| ::FileUtils.rm_r(full_key(f)) }
      else
        ::FileUtils.rm(full_key(key)) unless options[:recursive]
      end
    elsif !::File.directory?(full_key(key))
      true
    else
      raise 'unknown error'
    end
    value
  end
end

#encode(value) ⇒ Object



79
80
81
# File 'lib/adapter/file.rb', line 79

def encode(value)
  value
end

#full_key(key) ⇒ Object



93
94
95
96
# File 'lib/adapter/file.rb', line 93

def full_key(key)
  raise "you are not allowed to use double dot '..' notation in paths" if key.match(/\.\.\//)
  ::File.expand_path(::File.join(base_directory,key_for(key)))
end

#initialize(client = './files', options = {}) ⇒ Object



10
11
12
13
# File 'lib/adapter/file.rb', line 10

def initialize(client='./files', options={})
  FileUtils.mkdir_p(client)# unless ::File.directory?(client)
  base_directory client  # this will be the base directory
end

#key?(key) ⇒ Boolean

does the file already exist?

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/adapter/file.rb', line 16

def key?(key)
  raise "you are not allowed to use double dot '..' notation in paths" if key.match(/\.\.\//)
  ::File.exists?(full_key(key))
end

#keysObject



21
22
23
24
25
26
27
# File 'lib/adapter/file.rb', line 21

def keys
  files=[]
  ::Find.find(base_directory) do |item|
    files << item[(base_directory.length)..-1] if ::File.file?(item)
  end
  files
end

#read(key) ⇒ Object

return the file contents, or the directory listing if a directory



30
31
32
33
34
35
36
# File 'lib/adapter/file.rb', line 30

def read(key)
  if ::File.file?(full_key(key))
    ::File.read(full_key(key))
  elsif  ::File.directory?(full_key(key))
    Dir.entries('.')[2..-1]  #don't return the . and .. directories
  end
end

#write(key, value) ⇒ Object

TODO implement fetch def fetch(key)

raise "not implemented yet.  will return value or passed default"

end



43
44
45
46
47
# File 'lib/adapter/file.rb', line 43

def write(key, value)
  paths = ::File.split(full_key(key))
  FileUtils.mkdir_p(paths.first) unless ::File.directory?(paths.first)
  f = ::File.open(full_key(key), 'w') {|f| f.write(value) }
end