Class: AssetCloud::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_cloud/base.rb

Constant Summary collapse

VALID_PATHS =
%r{\A
(
  (\w)                        # Filename can be a single letter or underscore
  |                           # OR it is many and follows the below rules
  (
    (\.?[\w\[\]\(\)\-\@])     # It can start with a dot but it must have a following character
    (
      [\w\[\]\(\)\-\@]        # You can have a letter without any following conditions
      |
      [\ ][\w\[\]\(\)\-\@\.]  # If there is a space you need to have a normal letter afterward or a dot
      |
      [/][\w\[\]\(\)\-\@]     # If there is a slash you need to have a normal letter afterward
      |
      [/][\.][\w\[\]\(\)\-\@] # Though a slash could be followed by a dot
                              # so long as there is a normal letter afterward
      |
      [\.]+[\w\[\]\(\)\-\@]+  # One or more dots must be followed by one (or more) normal letters
    )*                        # Zero to many of these combinations.
  )
)\z}x
MATCH_BUCKET =
%r{^(\w+)(/|$)}
URI_PARSER =
URI::RFC2396_Parser.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, url = "/") ⇒ Base

Returns a new instance of Base.



106
107
108
109
# File 'lib/asset_cloud/base.rb', line 106

def initialize(root, url = "/")
  @root = root
  @url = url
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



36
37
38
# File 'lib/asset_cloud/base.rb', line 36

def root
  @root
end

#urlObject

Returns the value of attribute url.



36
37
38
# File 'lib/asset_cloud/base.rb', line 36

def url
  @url
end

Class Method Details

.asset_extensions(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/asset_cloud/base.rb', line 75

def asset_extensions(*args)
  opts = args.last.is_a?(Hash) ? args.pop.slice(:only, :except) : {}
  opts.each do |k, v|
    opts[k] = [v].flatten.map(&:to_sym)
  end

  args.each do |klass|
    klass = convert_to_class_name_if_possible(klass)
    self.asset_extension_classes = asset_extension_classes.merge(klass => opts).freeze
  end
end

.bucket(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/asset_cloud/base.rb', line 51

def bucket(*args)
  asset_class = if args.last.is_a?(Hash)
    convert_to_class_name_if_possible(args.pop[:asset_class])
  end

  bucket_class = if args.last.is_a?(Class)
    convert_to_class_name_if_possible(args.pop)
  else
    raise ArgumentError, "requires a bucket class"
  end

  if (bucket_name = args.first)
    self.bucket_classes = bucket_classes.merge(bucket_name.to_sym => bucket_class).freeze
    self.asset_classes = asset_classes.merge(bucket_name.to_sym => asset_class).freeze if asset_class
  else
    self.root_bucket_class = bucket_class
    if asset_class
      raise ArgumentError, "asset_class on the root bucket cannot be a proc" if asset_class.is_a?(Proc)

      self.root_asset_class = asset_class
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



221
222
223
# File 'lib/asset_cloud/base.rb', line 221

def [](key)
  asset_at!(key)
end

#[]=(key, value) ⇒ Object



215
216
217
218
219
# File 'lib/asset_cloud/base.rb', line 215

def []=(key, value)
  asset = self[key]
  asset.value = value
  asset.store
end

#asset_at(*args) ⇒ Object



129
130
131
# File 'lib/asset_cloud/base.rb', line 129

def asset_at(*args)
  asset_class_for(args.first).at(self, *args)
end

#asset_at!(*args) ⇒ Object



133
134
135
136
# File 'lib/asset_cloud/base.rb', line 133

def asset_at!(*args)
  check_key_for_errors(args.first)
  asset_at(*args)
end

#asset_class_for(key) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/asset_cloud/base.rb', line 242

def asset_class_for(key)
  klass = self.class.asset_classes[bucket_symbol_for_key(key)]
  klass = klass.call(key) if klass.is_a?(Proc)
  klass ||= self.class.root_asset_class

  constantize_if_necessary(klass)
end

#asset_extension_classes_for_bucket(bucket) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/asset_cloud/base.rb', line 250

def asset_extension_classes_for_bucket(bucket)
  bucket = bucket.to_sym
  extensions = self.class.asset_extension_classes
  klasses = extensions.keys.select do |ext|
    opts = extensions[ext]
    (opts.key?(:only) ? opts[:only].include?(bucket) : true) &&
      (opts.key?(:except) ? !opts[:except].include?(bucket) : true)
  end
  klasses.map { |klass| constantize_if_necessary(klass) }
end

#bucket_for(key) ⇒ Object



210
211
212
213
# File 'lib/asset_cloud/base.rb', line 210

def bucket_for(key)
  bucket = buckets[bucket_symbol_for_key(key)]
  bucket ? bucket : root_bucket
end

#bucketsObject



98
99
100
101
102
103
104
# File 'lib/asset_cloud/base.rb', line 98

def buckets
  @buckets ||= Hash.new do |hash, key|
    hash[key] = if (klass = self.class.bucket_classes[key])
      constantize_if_necessary(klass).new(self, key)
    end
  end
end

#build(key, value = nil, &block) ⇒ Object



156
157
158
159
# File 'lib/asset_cloud/base.rb', line 156

def build(key, value = nil, &block)
  logger&.info { "  [#{self.class.name}] Building asset #{key}" }
  asset_class_for(key).new(self, key, value, Metadata.non_existing, &block)
end

#copy(source, destination) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/asset_cloud/base.rb', line 148

def copy(source, destination)
  return if source == destination

  object = build(destination, read(source))
  object.store
  object
end

#delete(key) ⇒ Object



204
205
206
207
208
# File 'lib/asset_cloud/base.rb', line 204

def delete(key)
  logger&.info { "  [#{self.class.name}] Deleting #{key}" }

  bucket_for(key).delete(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
# File 'lib/asset_cloud/base.rb', line 192

def exist?(key)
  if (fp = stat(key))
    fp.exist?
  else
    false
  end
end

#find(key) ⇒ Object



123
124
125
126
127
# File 'lib/asset_cloud/base.rb', line 123

def find(key)
  asset = asset_at(key)
  asset.value
  asset
end

#ls(key) ⇒ Object



186
187
188
189
190
# File 'lib/asset_cloud/base.rb', line 186

def ls(key)
  logger&.info { "  [#{self.class.name}] Listing objects in #{key}" }

  bucket_for(key).ls(key)
end

#move(source, destination) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/asset_cloud/base.rb', line 138

def move(source, destination)
  return if source == destination

  object = copy(source, destination)
  if object.errors.none?
    asset_at(source).delete
  end
  object
end

#pathObject



119
120
121
# File 'lib/asset_cloud/base.rb', line 119

def path
  root
end

#path_for(key) ⇒ Object



115
116
117
# File 'lib/asset_cloud/base.rb', line 115

def path_for(key)
  File.join(path, key)
end

#read(key) ⇒ Object



174
175
176
177
178
# File 'lib/asset_cloud/base.rb', line 174

def read(key)
  logger&.info { "  [#{self.class.name}] Reading from #{key}" }

  bucket_for(key).read(key)
end

#read_version(key, version) ⇒ Object

versioning



227
228
229
230
# File 'lib/asset_cloud/base.rb', line 227

def read_version(key, version)
  logger&.info { "  [#{self.class.name}] Reading from #{key} at version #{version}" }
  bucket_for(key).read_version(key, version)
end

#stat(key) ⇒ Object



180
181
182
183
184
# File 'lib/asset_cloud/base.rb', line 180

def stat(key)
  logger&.info { "  [#{self.class.name}] Statting #{key}" }

  bucket_for(key).stat(key)
end

#supports?(key) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/asset_cloud/base.rb', line 200

def supports?(key)
  key =~ VALID_PATHS
end

#url_for(key, options = {}) ⇒ Object



111
112
113
# File 'lib/asset_cloud/base.rb', line 111

def url_for(key, options = {})
  File.join(@url, URI_PARSER.escape(key))
end

#version_details(key) ⇒ Object



237
238
239
240
# File 'lib/asset_cloud/base.rb', line 237

def version_details(key)
  logger&.info { "  [#{self.class.name}] Getting all version details for #{key}" }
  bucket_for(key).version_details(key)
end

#versions(key) ⇒ Object



232
233
234
235
# File 'lib/asset_cloud/base.rb', line 232

def versions(key)
  logger&.info { "  [#{self.class.name}] Getting all versions for #{key}" }
  bucket_for(key).versions(key)
end

#write(key, value) ⇒ Object



161
162
163
164
165
166
# File 'lib/asset_cloud/base.rb', line 161

def write(key, value)
  check_key_for_errors(key)
  logger&.info { "  [#{self.class.name}] Writing #{value.size} bytes to #{key}" }

  bucket_for(key).write(key, value)
end

#write!(key, value) ⇒ Object



168
169
170
171
172
# File 'lib/asset_cloud/base.rb', line 168

def write!(key, value)
  asset = self[key]
  asset.value = value
  asset.store!
end