Sha256: ecd3c69ef9edeb1ff53c121b4d7f8077f2d9fb7a731f5ffe6bec7f0f9e08faa5

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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

      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

3 entries across 3 versions & 1 rubygems

Version Path
tori-0.6.2 lib/tori/backend/chain.rb
tori-0.6.1 lib/tori/backend/chain.rb
tori-0.6.0 lib/tori/backend/chain.rb