Class: Persistable::MogileFSAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/persistable/mogile_fs_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MogileFSAdapter

Returns a new instance of MogileFSAdapter.



9
10
11
12
# File 'lib/persistable/mogile_fs_adapter.rb', line 9

def initialize(options = {})
  @options = options
  @mogile_fs_class = options[:class]
end

Instance Attribute Details

#mogile_fs_classObject (readonly)

Returns the value of attribute mogile_fs_class.



6
7
8
# File 'lib/persistable/mogile_fs_adapter.rb', line 6

def mogile_fs_class
  @mogile_fs_class
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/persistable/mogile_fs_adapter.rb', line 6

def options
  @options
end

Instance Method Details

#delete(persistable) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/persistable/mogile_fs_adapter.rb', line 38

def delete(persistable)
  hosts = options[:tracker].is_a?(String) ? [options[:tracker]] : options[:tracker]
  connection = MogileFS::MogileFS.new(:domain => options[:domain], :hosts => hosts, :timeout => options[:timeout] ? options[:timeout] : 10)
  begin
    status = connection.delete(persistable.persistence_key)  
  rescue MogileFS::Backend::UnknownKeyError => e
    status = false
  ensure
    connection.backend.shutdown
  end

  status
end

#read(persistable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/persistable/mogile_fs_adapter.rb', line 22

def read(persistable)
  hosts = options[:tracker].is_a?(String) ? [options[:tracker]] : options[:tracker]
  connection = MogileFS::MogileFS.new(:domain => options[:domain], :hosts => hosts, :timeout => options[:timeout] ? options[:timeout] : 10)
  begin
    if data = connection.get_file_data(persistable.persistence_key)
      persistable.persistence_data = StringIO.new(data)
    else
      persistable.persistence_data = nil
    end
  rescue MogileFS::Backend::UnknownKeyError, MogileFS::Backend::NoKeyError => e
    persistable.persistence_data = nil
  ensure
    connection.backend.shutdown
  end
end

#write(persistable) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/persistable/mogile_fs_adapter.rb', line 14

def write(persistable)
  hosts = options[:tracker].is_a?(String) ? [options[:tracker]] : options[:tracker]
  connection = MogileFS::MogileFS.new(:domain => options[:domain], :hosts => hosts, :timeout => options[:timeout] ? options[:timeout] : 10)
  status = connection.store_file(persistable.persistence_key, mogile_fs_class, persistable.persistence_data)
  connection.backend.shutdown
  status
end