lib/attached/attachment.rb in attached-0.4.1 vs lib/attached/attachment.rb in attached-0.4.2
- old
+ new
@@ -1,6 +1,6 @@
-require 'guid'
+require 'identifier'
require 'attached/storage'
require 'attached/storage/error'
require 'attached/processor'
@@ -19,10 +19,11 @@
attr_reader :errors
attr_reader :path
attr_reader :missing
attr_reader :styles
attr_reader :default
+ attr_reader :strategy
attr_reader :medium
attr_reader :credentials
attr_reader :processors
attr_reader :processor
attr_reader :aliases
@@ -42,13 +43,10 @@
:path => ":name/:style/:identifier:extension",
:missing => ":name/:style/missing:extension",
:default => :original,
:medium => :local,
:credentials => {},
- :styles => {},
- :processors => [],
- :aliases => [],
}
end
# Initialize a new attachment by providing a name and the instance the attachment is associated with.
@@ -68,12 +66,16 @@
# * :alias - A string representing a fully qualified host alias
# * :processors - An array of processors
# * :aliases - An array of aliases
def initialize(name, instance, options = {})
- options = self.class.options.merge(options)
+ options = self.class.options.clone.merge(options)
+ options[:styles] ||= {}
+ options[:aliases] ||= []
+ options[:processors] ||= []
+
@name = name
@instance = instance
@queue = {}
@purge = []
@@ -81,19 +83,20 @@
@path = options[:path]
@missing = options[:missing]
@styles = options[:styles]
@default = options[:default]
+ @strategy = options[:strategy]
@medium = options[:medium]
@credentials = options[:credentials]
@processors = options[:processors]
@processor = options[:processor]
@aliases = options[:aliases]
@alias = options[:alias]
- @processors = self.processors + [self.processor] if self.processor
- @aliases = self.aliases + [self.alias] if self.alias
+ @aliases = self.aliases << self.alias if self.alias
+ @processors = self.processors << self.processor if self.processor
@storage = Attached::Storage.storage(self.medium, self.credentials)
@host = self.storage.host
end
@@ -148,23 +151,25 @@
#
# Usage:
#
# @object.avatar.assign(...)
- def assign(file, identifier = "#{Guid.new}")
+ def assign(file, identifier = Identifier.generate)
self.file = file
- extension ||= File.extname(file.original_filename) if file.respond_to?(:original_filename)
- extension ||= File.extname(file.path) if file.respond_to?(:path)
+ if file
+ extension ||= File.extname(file.original_filename) if file.respond_to?(:original_filename)
+ extension ||= File.extname(file.path) if file.respond_to?(:path)
+ end
@purge = [self.path, *self.styles.map { |style, options| self.path(style) }] if attached?
- instance_set :size, file.size
- instance_set :extension, extension
- instance_set :identifier, identifier
+ self.size = file ? file.size : nil
+ self.extension = file ? extension : nil
+ self.identifier = file ? identifier : nil
- process
+ process if file
end
# Save an attachment.
#
@@ -253,10 +258,21 @@
def size
return instance_get(:size)
end
+ # Access the status for an attachment.
+ #
+ # Usage:
+ #
+ # @object.avatar.status
+
+ def status
+ instance_get(:status)
+ end
+
+
# Access the extension for an attachment. It will first check the styles
# to see if one is specified before checking the instance.
#
# Usage:
#
@@ -282,9 +298,31 @@
style and
self.styles and
self.styles[style] and
self.styles[style][:identifier] or
instance_get(:identifier)
+ end
+
+
+ # Set the size for an attachment.
+ #
+ # Usage:
+ #
+ # @object.avatar.size = 1024
+
+ def size=(size)
+ instance_set(:size, size)
+ end
+
+
+ # Set the status for an attachment.
+ #
+ # Usage:
+ #
+ # @object.avatar.status = 'processing'
+
+ def status=(status)
+ instance_set(:size, status)
end
# Set the extension for an attachment. It will act independently of the
# defined style.
\ No newline at end of file