Class: HP::Cloud::ResourceFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/resource_factory.rb

Constant Summary collapse

REMOTE_TYPES =
[:container,
:container_directory,
:object,
:object_store,
:shared_directory,
:shared_resource]
LOCAL_TYPES =
[:directory, :file]

Class Method Summary collapse

Class Method Details

.create(storage, fname) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hpcloud/resource_factory.rb', line 41

def self.create(storage, fname)
  unless (fname.start_with?('http://') || fname.start_with?('https://'))
    if fname.length > 0
      unless fname.start_with?(':')
        unless fname.start_with?('/')
          unless fname.start_with?('.')
            fname = ':' + fname
          end
        end
      end
    end
  end
  return ResourceFactory.create_any(storage, fname)
end

.create_any(storage, fname) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hpcloud/resource_factory.rb', line 56

def self.create_any(storage, fname)
  ftype = detect_type(fname)
  case ftype
  when :directory, :file
    return LocalResource.new(storage, fname)
  when :object_store
    return ObjectStore.new(storage, fname)
  when :shared_resource, :shared_directory
    return SharedResource.new(storage, fname)
  when :container
    return ContainerResource.new(storage, fname)
  end
  return RemoteResource.new(storage, fname)
end

.detect_type(resource) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hpcloud/resource_factory.rb', line 71

def self.detect_type(resource)
  if resource.empty?
    return :object_store
  end
  if resource[0,1] == ':'
    if resource[-1,1] == '/'
      :container_directory
    elsif resource.index('/')
      :object
    else
      :container
    end
  else
    if (resource.start_with?('http://') ||
        resource.start_with?('https://'))
      if resource[-1,1] == '/'
        :shared_directory
      else
        :shared_resource
      end
    elsif resource[-1,1] == '/'
      :directory
    elsif File.directory?(resource)
      :directory
    else
      :file
    end
  end
end

.is_local?(ftype) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/hpcloud/resource_factory.rb', line 101

def self.is_local?(ftype)
  return ResourceFactory::LOCAL_TYPES.include?(ftype)
end

.is_remote?(ftype) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/hpcloud/resource_factory.rb', line 105

def self.is_remote?(ftype)
  return ResourceFactory::REMOTE_TYPES.include?(ftype)
end