Class: Fog::Local::Files

Inherits:
Collection show all
Defined in:
lib/fog/local/models/files.rb

Instance Attribute Summary

Attributes inherited from Collection

#connection

Instance Method Summary collapse

Methods inherited from Collection

#clear, #create, #initialize, #inspect, #load, model, #model, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #identity, #identity=, #merge_attributes, #new_record?, #requires

Constructor Details

This class inherits a constructor from Fog::Collection

Instance Method Details

#allObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/local/models/files.rb', line 11

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

#directoryObject



29
30
31
# File 'lib/fog/local/models/files.rb', line 29

def directory
  @directory
end

#get(key, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/local/models/files.rb', line 33

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

#new(attributes = {}) ⇒ Object



58
59
60
# File 'lib/fog/local/models/files.rb', line 58

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