Class: Cabinet::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/cabinet/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, auth = {}) ⇒ Instance

Returns a new instance of Instance.



5
6
7
8
# File 'lib/cabinet/instance.rb', line 5

def initialize(provider, auth={})
  fog_config      = auth.merge({:provider => fog_provider(provider)})
  self.connection = Fog::Storage.new(fog_config)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/cabinet/instance.rb', line 3

def connection
  @connection
end

#directoryObject

Returns the value of attribute directory.



3
4
5
# File 'lib/cabinet/instance.rb', line 3

def directory
  @directory
end

Instance Method Details

#append(name, new_content) ⇒ Object



40
41
42
43
# File 'lib/cabinet/instance.rb', line 40

def append(name, new_content)
  content = exists?(name) ? get(name) + new_content : new_content
  put(name, content) and !!reload(name)
end

#compress(name, content) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cabinet/instance.rb', line 73

def compress(name, content)
  name = name.gsub(/(.+?)(\.gz)?$/, '\1.gz')
  tmp  = "/tmp/cabinet-tmp-#{name}-#{Time.now}"

  File.open(tmp, 'w') do |f|
    gz = Zlib::GzipWriter.new(f)
    gz.write content
    gz.close
  end

  put(name, File.read(tmp)) and (File.unlink(tmp) == 1)
end

#copy_to(klass, name) ⇒ Object



61
62
63
# File 'lib/cabinet/instance.rb', line 61

def copy_to(klass, name)
  klass.put(name, get(name))
end

#decompress(name) ⇒ Object



86
87
88
# File 'lib/cabinet/instance.rb', line 86

def decompress(name)
  Zlib::GzipReader.new(StringIO.new(get(name))).read
end

#delete(name_or_regexp) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cabinet/instance.rb', line 45

def delete(name_or_regexp)
  @file = nil

  directory.files.reload

  if name_or_regexp.class == Regexp
    directory.files.select{|f| f.key.match(name_or_regexp)}.each do |file|
      file.destroy
    end
  else
    directory.files.get(name_or_regexp).destroy
  end

  true
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/cabinet/instance.rb', line 65

def exists?(name)
  !!file(name)
end

#get(name) ⇒ Object



17
18
19
# File 'lib/cabinet/instance.rb', line 17

def get(name)
  file(name).body
end

#list(regexp = /.*/) ⇒ Object



21
22
23
24
# File 'lib/cabinet/instance.rb', line 21

def list(regexp=/.*/)
  directory.files.reload
  directory.files.select{|f| f.key.match(regexp)}.to_a.map(&:key)
end

#modified(name) ⇒ Object



69
70
71
# File 'lib/cabinet/instance.rb', line 69

def modified(name)
  file(name).last_modified + 0 if exists?(name)
end

#put(name, content) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/cabinet/instance.rb', line 26

def put(name, content)
  content = content.to_s

  begin
    directory.files.create(:key => name, :body => content).content_length == content.length
  rescue
    false
  end
end

#touch(name) ⇒ Object



36
37
38
# File 'lib/cabinet/instance.rb', line 36

def touch(name)
  (exists?(name) ? put(name, get(name)) : put(name, "")) and !!reload(name)
end