1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[local] rename files to storage for consistency

This commit is contained in:
geemus 2010-09-08 12:50:38 -07:00
parent a637504d18
commit 24897e9c1e
11 changed files with 294 additions and 266 deletions

View file

@ -1,57 +1,17 @@
module Fog
class Local < Fog::Service
module Local
requires :local_root
extend Fog::Provider
model_path 'fog/local/models'
collection :directories
model :directory
model :file
collection :files
service_path 'fog/local'
service 'storage'
class Mock
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
@data = self.class.data[@local_root]
end
def local_root
@local_root
end
def path(partial)
partial
end
end
class Real
include Collections
def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
end
def local_root
@local_root
end
def path_to(partial)
::File.join(@local_root, partial)
end
def self.new(attributes = {})
location = caller.first
warning = "[yellow][WARN] Fog::Local#new is deprecated, use Fog::Local::Storage#new instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
Fog::Local::Storage.new(attributes)
end
end

View file

@ -9,21 +9,21 @@ module Local
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :files
Fog::Local.new
when :storage
Fog::Local::Storage.new
end
end
@@connections[service]
end
def services
[:files]
[:storage]
end
for collection in Fog::Local.collections
for collection in Fog::Local::Storage.collections
module_eval <<-EOS, __FILE__, __LINE__
def #{collection}
self[:files].#{collection}
self[:storage].#{collection}
end
EOS
end

View file

@ -1,31 +0,0 @@
require 'fog/collection'
require 'fog/local/models/directory'
module Fog
class Local
class Directories < Fog::Collection
model Fog::Local::Directory
def all
data = Dir.entries(connection.local_root).select do |entry|
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry))
end.map do |entry|
{:key => entry}
end
load(data)
end
def get(key)
if ::File.directory?(connection.path_to(key))
new(:key => key)
else
nil
end
end
end
end
end

View file

@ -1,50 +0,0 @@
require 'fog/model'
# require 'fog/local/models/files'
module Fog
class Local
class Directory < Fog::Model
extend Fog::Deprecation
deprecate(:name, :key)
deprecate(:name=, :key=)
identity :key
def destroy
requires :key
if ::File.directory?(path)
Dir.rmdir(path)
true
else
false
end
end
def files
@files ||= begin
Fog::Local::Files.new(
:directory => self,
:connection => connection
)
end
end
def save
requires :key
Dir.mkdir(path)
true
end
private
def path
connection.path_to(key)
end
end
end
end

View file

@ -1,58 +0,0 @@
require 'fog/model'
module Fog
class Local
class File < Fog::Model
identity :key, 'Key'
attr_accessor :body
attribute :content_length, :aliases => 'Content-Length'
# attribute :content_type, :aliases => 'Content-Type'
attribute :last_modified, :aliases => 'Last-Modified'
def body
@body ||= if last_modified
collection.get(identity).body
else
''
end
end
def directory
@directory
end
def destroy
requires :directory, :key
::File.delete(path)
true
end
def save(options = {})
requires :body, :directory, :key
file = ::File.new(path, 'w')
file.write(body)
file.close
merge_attributes(
:content_length => ::File.size(path),
:last_modified => ::File.mtime(path)
)
true
end
private
def directory=(new_directory)
@directory = new_directory
end
def path
connection.path_to(::File.join(directory.key, key))
end
end
end
end

View file

@ -1,71 +0,0 @@
require 'fog/collection'
require 'fog/local/models/file'
module Fog
class Local
class Files < Fog::Collection
attribute :directory
model Fog::Local::File
def all
requires :directory
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 get(key, &block)
requires :directory
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 = {})
requires :directory
super({ :directory => directory }.merge!(attributes))
end
private
def file_path(key)
connection.path_to(::File.join(directory.key, key))
end
end
end
end

View file

@ -0,0 +1,33 @@
require 'fog/collection'
require 'fog/local/models/directory'
module Fog
module Local
class Storage
class Directories < Fog::Collection
model Fog::Local::Directory
def all
data = Dir.entries(connection.local_root).select do |entry|
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry))
end.map do |entry|
{:key => entry}
end
load(data)
end
def get(key)
if ::File.directory?(connection.path_to(key))
new(:key => key)
else
nil
end
end
end
end
end
end

View file

@ -0,0 +1,52 @@
require 'fog/model'
# require 'fog/local/models/files'
module Fog
module Local
class Storage
class Directory < Fog::Model
extend Fog::Deprecation
deprecate(:name, :key)
deprecate(:name=, :key=)
identity :key
def destroy
requires :key
if ::File.directory?(path)
Dir.rmdir(path)
true
else
false
end
end
def files
@files ||= begin
Fog::Local::Files.new(
:directory => self,
:connection => connection
)
end
end
def save
requires :key
Dir.mkdir(path)
true
end
private
def path
connection.path_to(key)
end
end
end
end
end

View file

@ -0,0 +1,60 @@
require 'fog/model'
module Fog
module Local
class Storage
class File < Fog::Model
identity :key, 'Key'
attr_accessor :body
attribute :content_length, :aliases => 'Content-Length'
# attribute :content_type, :aliases => 'Content-Type'
attribute :last_modified, :aliases => 'Last-Modified'
def body
@body ||= if last_modified
collection.get(identity).body
else
''
end
end
def directory
@directory
end
def destroy
requires :directory, :key
::File.delete(path)
true
end
def save(options = {})
requires :body, :directory, :key
file = ::File.new(path, 'w')
file.write(body)
file.close
merge_attributes(
:content_length => ::File.size(path),
:last_modified => ::File.mtime(path)
)
true
end
private
def directory=(new_directory)
@directory = new_directory
end
def path
connection.path_to(::File.join(directory.key, key))
end
end
end
end
end

View file

@ -0,0 +1,73 @@
require 'fog/collection'
require 'fog/local/models/file'
module Fog
module Local
class Storage
class Files < Fog::Collection
attribute :directory
model Fog::Local::File
def all
requires :directory
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 get(key, &block)
requires :directory
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 = {})
requires :directory
super({ :directory => directory }.merge!(attributes))
end
private
def file_path(key)
connection.path_to(::File.join(directory.key, key))
end
end
end
end
end

60
lib/fog/local/storage.rb Normal file
View file

@ -0,0 +1,60 @@
module Fog
module Local
class Storage < Fog::Service
requires :local_root
model_path 'fog/local/models/storage'
collection :directories
model :directory
model :file
collection :files
class Mock
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
@data = self.class.data[@local_root]
end
def local_root
@local_root
end
def path(partial)
partial
end
end
class Real
include Collections
def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
end
def local_root
@local_root
end
def path_to(partial)
::File.join(@local_root, partial)
end
end
end
end
end