Sha256: 0e9fa63ced97b8653cb268c3c3cef75d7743c0625595324706d142ab10f785db

Contents?: true

Size: 1.69 KB

Versions: 54

Compression:

Stored size: 1.69 KB

Contents

require 'fog/collection'
require 'fog/local/models/file'

module Fog
  module Local

    class Files < Fog::Collection

      model Fog::Local::File

      def all
        if directory.collection.get(directory.key)
          data = Dir.entries(connection.path_to(directory.key)).select do |key|
            key[0...1] != '.' && !::File.directory?(connection.path_to(key))
          end.map do |key|
            path = file_path(key)
            {
              :content_length => ::File.size(path),
              :key            => key,
              :last_modified  => ::File.mtime(path)
            }
          end
          load(data)
        else
          nil
        end
      end

      def directory
        @directory
      end

      def get(key, &block)
        path = file_path(key)
        if ::File.exists?(path)
          data = {
            :content_length => ::File.size(path),
            :key            => key,
            :last_modified  => ::File.mtime(path)
          }
          if block_given?
            file = ::File.open(path)
            while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end
            file.close
            new(data)
          else
            body = nil
            ::File.open(path) do |file|
              body = file.read
            end
            new(data.merge!(:body => body))
          end
        else
          nil
        end
      end

      def new(attributes = {})
        super({ :directory => directory }.merge!(attributes))
      end

      private

      def directory=(new_directory)
        @directory = new_directory
      end

      def file_path(key)
        connection.path_to(::File.join(directory.key, key))
      end

    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
fog-0.2.30 lib/fog/local/models/files.rb
fog-0.2.28 lib/fog/local/models/files.rb
fog-0.2.27 lib/fog/local/models/files.rb
fog-0.2.26 lib/fog/local/models/files.rb
fog-0.2.25 lib/fog/local/models/files.rb
fog-0.2.24 lib/fog/local/models/files.rb
tecnh-fog-0.2.23.vpc lib/fog/local/models/files.rb
fog-0.2.23 lib/fog/local/models/files.rb
fog-0.2.22 lib/fog/local/models/files.rb
fog-0.2.21 lib/fog/local/models/files.rb
fog-0.2.20 lib/fog/local/models/files.rb
fog-0.2.19 lib/fog/local/models/files.rb
fog-0.2.18 lib/fog/local/models/files.rb
fog-0.2.17 lib/fog/local/models/files.rb
fog-0.2.16 lib/fog/local/models/files.rb
fog-0.2.15 lib/fog/local/models/files.rb
fog-0.2.14 lib/fog/local/models/files.rb
fog-0.2.13 lib/fog/local/models/files.rb
fog-0.2.12 lib/fog/local/models/files.rb
fog-0.2.11 lib/fog/local/models/files.rb