Class: Tori::Backend::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/tori/backend/chain.rb

Overview

Chain based on ‘exist?` method

Examples:

class Book < ActiveRecord::Base
  include Tori::Backend # short cut

  # If exist "lib/pdf" load this,
  # But nothing, Load from S3 "book" bucket.
  chain_backend = Chain.new(
    FileSystem.new(Pathname("lib/pdf")),
    S3.new(bucket: "book"),
  )
  tori :pdf, chain_backend do |model|
    "book/#{__tori__}/#{model.id}"
  end
end

Defined Under Namespace

Classes: ExistError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*backends) ⇒ Chain

Returns a new instance of Chain.



24
25
26
# File 'lib/tori/backend/chain.rb', line 24

def initialize(*backends)
  @backends = backends
end

Instance Attribute Details

#backendsObject

Returns the value of attribute backends.



22
23
24
# File 'lib/tori/backend/chain.rb', line 22

def backends
  @backends
end

Instance Method Details

#backend(filename) ⇒ Object

Raises:



28
29
30
31
32
33
34
35
# File 'lib/tori/backend/chain.rb', line 28

def backend(filename)
  @backends.each do |b|
    if b.exist?(filename)
      return b
    end
  end
  raise ExistError, "exist(#{filename}) backend not found"
end

#exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/tori/backend/chain.rb', line 37

def exist?(filename)
  backend(filename)
rescue ExistError
  false
else
  true
end

#open(filename, &block) ⇒ Object



49
50
51
# File 'lib/tori/backend/chain.rb', line 49

def open(filename, &block)
  backend(filename).open(filename, &block)
end

#read(filename) ⇒ Object



45
46
47
# File 'lib/tori/backend/chain.rb', line 45

def read(filename)
  backend(filename).read(filename)
end