Class: Bulldog::Attachment::Base
- Defined in:
- lib/bulldog/attachment/base.rb
Instance Attribute Summary
Attributes inherited from Maybe
#name, #record, #saved, #stream, #value
Class Method Summary collapse
-
.try(record, name, stream) ⇒ Object
Return an instance of the record if the stream is valid, nil otherwise.
Instance Method Summary collapse
-
#content_type ⇒ Object
Return the content type of the data.
-
#destroy ⇒ Object
Called when the attachment is destroyed, or just before another attachment is saved in its place.
-
#examine ⇒ Object
Ensure the file examination has been run, and return the result.
-
#file_name ⇒ Object
Return the original file name of the attached file.
-
#file_size ⇒ Object
Return the size of the attached file.
-
#initialize(record, name, stream) ⇒ Base
constructor
A new instance of Base.
-
#path(style_name = reflection.default_style) ⇒ Object
Return the path on the file system where the given style’s image is to be stored.
-
#process(event_name, options = {}) ⇒ Object
Run the processors for the named event.
-
#process!(event_name, options = {}, &block) ⇒ Object
Like #process, but raise ActiveRecord::RecordInvalid if there are any errors.
-
#save ⇒ Object
Called when the attachment is saved.
- #unload ⇒ Object
-
#url(style_name = reflection.default_style) ⇒ Object
Return the URL where the given style’s image is to be found.
Methods inherited from Maybe
#==, #copy_for, handle, #interpolate_path, #interpolate_url, #load, #read_storable_attributes, #reflection, #reload, #saved?, #type
Constructor Details
#initialize(record, name, stream) ⇒ Base
Returns a new instance of Base.
13 14 15 16 |
# File 'lib/bulldog/attachment/base.rb', line 13 def initialize(record, name, stream) super @examination_result = nil end |
Class Method Details
.try(record, name, stream) ⇒ Object
Return an instance of the record if the stream is valid, nil otherwise.
8 9 10 11 |
# File 'lib/bulldog/attachment/base.rb', line 8 def self.try(record, name, stream) = new(record, name, stream) .send(:examine) ? : nil end |
Instance Method Details
#content_type ⇒ Object
Return the content type of the data.
80 81 82 |
# File 'lib/bulldog/attachment/base.rb', line 80 def content_type @content_type ||= stream.content_type end |
#destroy ⇒ Object
Called when the attachment is destroyed, or just before another attachment is saved in its place.
98 99 100 |
# File 'lib/bulldog/attachment/base.rb', line 98 def destroy delete_files_and_empty_parent_directories end |
#examine ⇒ Object
Ensure the file examination has been run, and return the result.
113 114 115 116 117 118 119 |
# File 'lib/bulldog/attachment/base.rb', line 113 def examine if @examination_result.nil? @examination_result = run_examination else @examination_result end end |
#file_name ⇒ Object
Return the original file name of the attached file.
73 74 75 |
# File 'lib/bulldog/attachment/base.rb', line 73 def file_name @file_name ||= stream.file_name end |
#file_size ⇒ Object
Return the size of the attached file.
66 67 68 |
# File 'lib/bulldog/attachment/base.rb', line 66 def file_size stream.size end |
#path(style_name = reflection.default_style) ⇒ Object
Return the path on the file system where the given style’s image is to be stored.
52 53 54 |
# File 'lib/bulldog/attachment/base.rb', line 52 def path(style_name = reflection.default_style) interpolate_path(style_name) end |
#process(event_name, options = {}) ⇒ Object
Run the processors for the named event.
Return true if no errors were encountered, false otherwise.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bulldog/attachment/base.rb', line 23 def process(event_name, ={}) reflection.events[event_name].each do |event| if (types = event.) next unless types.include?(type) end processor_type = event.processor_type || default_processor_type processor_class = Processor.const_get(processor_type.to_s.camelize) processor = processor_class.new(self, [:input] || stream.path) styles = reflection.styles names = [:styles] || event.styles and styles = reflection.styles.slice(*names) processor.process(styles, , &event.callback) end record.errors.empty? end |
#process!(event_name, options = {}, &block) ⇒ Object
Like #process, but raise ActiveRecord::RecordInvalid if there are any errors.
43 44 45 46 |
# File 'lib/bulldog/attachment/base.rb', line 43 def process!(event_name, ={}, &block) process(event_name, , &block) or raise ActiveRecord::RecordInvalid, record end |
#save ⇒ Object
Called when the attachment is saved.
87 88 89 90 91 92 |
# File 'lib/bulldog/attachment/base.rb', line 87 def save return if saved? self.saved = true original_path = interpolate_path(:original) stream.write_to(original_path) end |
#unload ⇒ Object
121 122 123 124 |
# File 'lib/bulldog/attachment/base.rb', line 121 def unload super @examination_result = nil end |
#url(style_name = reflection.default_style) ⇒ Object
Return the URL where the given style’s image is to be found.
59 60 61 |
# File 'lib/bulldog/attachment/base.rb', line 59 def url(style_name = reflection.default_style) interpolate_url(style_name) end |