Class: Attached::Attachment
- Inherits:
-
Object
- Object
- Attached::Attachment
- Defined in:
- lib/attached/attachment.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#file ⇒ Object
Returns the value of attribute file.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#medium ⇒ Object
readonly
Returns the value of attribute medium.
-
#missing ⇒ Object
readonly
Returns the value of attribute missing.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path(style = self.default) ⇒ Object
readonly
Access the path for an attachment.
-
#processor ⇒ Object
readonly
Returns the value of attribute processor.
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
-
#purge ⇒ Object
readonly
Returns the value of attribute purge.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
Class Method Summary collapse
-
.options ⇒ Object
A default set of options that can be extended to customize the path, storage or credentials.
Instance Method Summary collapse
-
#assign(file, identifier = Identifier.generate) ⇒ Object
Assign an attachment to a file.
-
#attached? ⇒ Boolean
Check if an attachment is present.
-
#changed? ⇒ Boolean
Check if an attachment has been modified.
-
#destroy ⇒ Object
Destroy an attachment.
-
#extension(style = nil) ⇒ Object
Access the extension for an attachment.
-
#extension=(extension) ⇒ Object
Set the extension for an attachment.
-
#file? ⇒ Boolean
Check if an attachment is present.
-
#identifier(style = nil) ⇒ Object
Access the identifier for an attachment.
-
#identifier=(identifier) ⇒ Object
Set the identifier for an attachment.
-
#initialize(name, instance, options = {}) ⇒ Attachment
constructor
Initialize a new attachment by providing a name and the instance the attachment is associated with.
-
#reprocess! ⇒ Object
Access the original file .
-
#save ⇒ Object
Save an attachment.
-
#size ⇒ Object
Access the size for an attachment.
-
#size=(size) ⇒ Object
Set the size for an attachment.
-
#status ⇒ Object
Access the status for an attachment.
-
#status=(status) ⇒ Object
Set the status for an attachment.
-
#url(style = self.default) ⇒ Object
Acesss the URL for an attachment.
Constructor Details
#initialize(name, instance, options = {}) ⇒ Attachment
Initialize a new attachment by providing a name and the instance the attachment is associated with.
Parameters:
-
name - The name for the attachment such as ‘avatar’ or ‘photo’
-
instance - The instance the attachment is attached to
Options:
-
:path - The location where the attachment is stored
-
:styles - A hash containing optional parameters including extension and identifier
-
:credentials - A file, hash, or path used to authenticate with the specified storage medium
-
:medium - A symbol or subclass of ‘Attached::Storage::Base’ to be used
-
:processor - A symbol or subclass of ‘Attached::Processor::Base’ to be used
-
:alias - A string representing a fully qualified host alias
-
:processors - An array of processors
-
:aliases - An array of aliases
70 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 100 101 102 |
# File 'lib/attached/attachment.rb', line 70 def initialize(name, instance, = {}) = self.class..clone.merge() [:styles] ||= {} [:aliases] ||= [] [:processors] ||= [] @name = name @instance = instance @queue = {} @purge = [] @errors = [] @path = [:path] @missing = [:missing] @styles = [:styles] @default = [:default] @strategy = [:strategy] @medium = [:medium] @credentials = [:credentials] @processors = [:processors] @processor = [:processor] @aliases = [:aliases] @alias = [: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 |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
30 31 32 |
# File 'lib/attached/attachment.rb', line 30 def alias @alias end |
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
29 30 31 |
# File 'lib/attached/attachment.rb', line 29 def aliases @aliases end |
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
26 27 28 |
# File 'lib/attached/attachment.rb', line 26 def credentials @credentials end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
23 24 25 |
# File 'lib/attached/attachment.rb', line 23 def default @default end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
19 20 21 |
# File 'lib/attached/attachment.rb', line 19 def errors @errors end |
#file ⇒ Object
Returns the value of attribute file.
14 15 16 |
# File 'lib/attached/attachment.rb', line 14 def file @file end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
32 33 34 |
# File 'lib/attached/attachment.rb', line 32 def host @host end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
16 17 18 |
# File 'lib/attached/attachment.rb', line 16 def instance @instance end |
#medium ⇒ Object (readonly)
Returns the value of attribute medium.
25 26 27 |
# File 'lib/attached/attachment.rb', line 25 def medium @medium end |
#missing ⇒ Object (readonly)
Returns the value of attribute missing.
21 22 23 |
# File 'lib/attached/attachment.rb', line 21 def missing @missing end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/attached/attachment.rb', line 15 def name @name end |
#path(style = self.default) ⇒ Object (readonly)
Access the path for an attachment.
Usage:
@object.avatar.url
@object.avatar.url(:small)
@object.avatar.url(:large)
240 241 242 |
# File 'lib/attached/attachment.rb', line 240 def path @path end |
#processor ⇒ Object (readonly)
Returns the value of attribute processor.
28 29 30 |
# File 'lib/attached/attachment.rb', line 28 def processor @processor end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
27 28 29 |
# File 'lib/attached/attachment.rb', line 27 def processors @processors end |
#purge ⇒ Object (readonly)
Returns the value of attribute purge.
18 19 20 |
# File 'lib/attached/attachment.rb', line 18 def purge @purge end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
17 18 19 |
# File 'lib/attached/attachment.rb', line 17 def queue @queue end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
31 32 33 |
# File 'lib/attached/attachment.rb', line 31 def storage @storage end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
24 25 26 |
# File 'lib/attached/attachment.rb', line 24 def strategy @strategy end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
22 23 24 |
# File 'lib/attached/attachment.rb', line 22 def styles @styles end |
Class Method Details
.options ⇒ Object
A default set of options that can be extended to customize the path, storage or credentials.
Usage:
Attached::Attachment. = { :storage => :fs, :path => ":name/:style/:identifier:extension" }
41 42 43 44 45 46 47 48 49 |
# File 'lib/attached/attachment.rb', line 41 def self. @options ||= { :path => ":name/:style/:identifier:extension", :missing => ":name/:style/missing:extension", :default => :original, :medium => :local, :credentials => {}, } end |
Instance Method Details
#assign(file, identifier = Identifier.generate) ⇒ Object
Assign an attachment to a file.
Usage:
@object.avatar.assign(...)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/attached/attachment.rb', line 156 def assign(file, identifier = Identifier.generate) self.file = file 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, | self.path(style) }] if attached? self.size = file ? file.size : nil self.extension = file ? extension : nil self.identifier = file ? identifier : nil process if file end |
#attached? ⇒ Boolean
Check if an attachment is present.
Usage:
@object.avatar.attached?
133 134 135 |
# File 'lib/attached/attachment.rb', line 133 def attached? not identifier.blank? end |
#changed? ⇒ Boolean
Check if an attachment has been modified.
Usage:
@object.avatar.changed?
111 112 113 |
# File 'lib/attached/attachment.rb', line 111 def changed? instance.changed.include? "#{name}_identifier" end |
#destroy ⇒ Object
Destroy an attachment.
Usage:
@object.avatar.destroy
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/attached/attachment.rb', line 201 def destroy if attached? self.storage.destroy(self.path) self.styles.each do |style, | self.storage.destroy(self.path(style)) end end @purge = [] @queue = {} end |
#extension(style = nil) ⇒ Object
Access the extension for an attachment. It will first check the styles to see if one is specified before checking the instance.
Usage:
281 282 283 284 285 286 287 |
# File 'lib/attached/attachment.rb', line 281 def extension(style = nil) style and self.styles and self.styles[style] and self.styles[style][:extension] or instance_get(:extension) end |
#extension=(extension) ⇒ Object
Set the extension for an attachment. It will act independently of the defined style.
Usage:
335 336 337 |
# File 'lib/attached/attachment.rb', line 335 def extension=(extension) instance_set(:extension, extension) end |
#file? ⇒ Boolean
Check if an attachment is present.
Usage:
@object.avatar.file?
122 123 124 |
# File 'lib/attached/attachment.rb', line 122 def file? not identifier.blank? end |
#identifier(style = nil) ⇒ Object
Access the identifier for an attachment. It will first check the styles to see if one is specified before checking the instance.
Usage:
297 298 299 300 301 302 303 |
# File 'lib/attached/attachment.rb', line 297 def identifier(style = nil) style and self.styles and self.styles[style] and self.styles[style][:identifier] or instance_get(:identifier) end |
#identifier=(identifier) ⇒ Object
Set the identifier for an attachment. It will act independently of the defined style.
Usage:
347 348 349 |
# File 'lib/attached/attachment.rb', line 347 def identifier=(identifier) instance_set(:identifier, identifier) end |
#reprocess! ⇒ Object
Access the original file .
354 355 356 357 358 |
# File 'lib/attached/attachment.rb', line 354 def reprocess! self.file = self.storage.retrieve(self.path) process end |
#save ⇒ Object
Save an attachment.
Usage:
@object.avatar.save
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/attached/attachment.rb', line 180 def save self.queue.each do |style, file| path = self.path(style) self.storage.save(file, path) if file and path end self.purge.each do |path| self.storage.destroy(path) end @purge = [] @queue = {} end |
#size ⇒ Object
Access the size for an attachment.
Usage:
258 259 260 |
# File 'lib/attached/attachment.rb', line 258 def size return instance_get(:size) end |
#size=(size) ⇒ Object
Set the size for an attachment.
Usage:
312 313 314 |
# File 'lib/attached/attachment.rb', line 312 def size=(size) instance_set(:size, size) end |
#status ⇒ Object
Access the status for an attachment.
Usage:
269 270 271 |
# File 'lib/attached/attachment.rb', line 269 def status instance_get(:status) end |
#status=(status) ⇒ Object
Set the status for an attachment.
Usage:
323 324 325 |
# File 'lib/attached/attachment.rb', line 323 def status=(status) instance_set(:size, status) end |
#url(style = self.default) ⇒ Object
Acesss the URL for an attachment.
Usage:
@object.avatar.url
@object.avatar.url(:small)
@object.avatar.url(:large)
222 223 224 225 226 227 228 229 |
# File 'lib/attached/attachment.rb', line 222 def url(style = self.default) path = self.path(style) host = self.host host = self.aliases[path.hash % self.aliases.count] unless self.aliases.empty? return "#{host}#{path}" end |