Sha256: 2ced67a45a3dad0416ee53085c94149a3a63c302aef0e4d940883a99d85c93e6

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

module Tori
  module Backend
    # Chain based on `exist?` method
    # @example
    #   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
    class Chain
      class ExistError < StandardError
      end

      attr_accessor :backends

      def initialize(*backends)
        @backends = backends
      end

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

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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tori-0.8.0 lib/tori/backend/chain.rb
tori-0.7.2 lib/tori/backend/chain.rb
tori-0.7.1 lib/tori/backend/chain.rb
tori-0.7.0 lib/tori/backend/chain.rb
tori-0.6.6 lib/tori/backend/chain.rb
tori-0.6.5 lib/tori/backend/chain.rb
tori-0.6.4 lib/tori/backend/chain.rb
tori-0.6.3 lib/tori/backend/chain.rb