Class: Sequel::Plugins::Paperclip::Attachment
- Inherits:
-
Object
- Object
- Sequel::Plugins::Paperclip::Attachment
- Defined in:
- lib/sequel_paperclip/attachment.rb
Constant Summary collapse
- STORAGE_UPDATE_SAVE =
1
- STORAGE_UPDATE_DELETE =
2
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#queued_file ⇒ Object
readonly
Returns the value of attribute queued_file.
Class Method Summary collapse
Instance Method Summary collapse
- #consistent? ⇒ Boolean
- #exists? ⇒ Boolean
-
#initialize(model, name, preprocessed_options) ⇒ Attachment
constructor
A new instance of Attachment.
- #path(style = :original) ⇒ Object
- #process ⇒ Object
- #update(file) ⇒ Object
- #update_storage ⇒ Object
- #url(style = :original) ⇒ Object
Constructor Details
#initialize(model, name, preprocessed_options) ⇒ Attachment
Returns a new instance of Attachment.
36 37 38 39 40 41 |
# File 'lib/sequel_paperclip/attachment.rb', line 36 def initialize(model, name, ) @model = model @name = name @options = @storage_updates = [] end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/sequel_paperclip/attachment.rb', line 5 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/sequel_paperclip/attachment.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/sequel_paperclip/attachment.rb', line 7 def @options end |
#queued_file ⇒ Object (readonly)
Returns the value of attribute queued_file.
8 9 10 |
# File 'lib/sequel_paperclip/attachment.rb', line 8 def queued_file @queued_file end |
Class Method Details
.preprocess_options(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sequel_paperclip/attachment.rb', line 13 def self.( = {}) unless [:styles] [:styles] = { :original => {} } end unless [:processors] [:processors] = [ { :type => :dummy, } ] end [:processors].each_with_index do |processor, i| if processor.is_a?(Hash) klass = "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}" [:processors][i] = klass.constantize.new(self, processor) end end end |
Instance Method Details
#consistent? ⇒ Boolean
70 71 72 73 74 75 |
# File 'lib/sequel_paperclip/attachment.rb', line 70 def consistent? [:styles].each_key do |style| return false unless File.exists?(path(style)) end true end |
#exists? ⇒ Boolean
66 67 68 |
# File 'lib/sequel_paperclip/attachment.rb', line 66 def exists? !!model.send("#{name}_basename") end |
#path(style = :original) ⇒ Object
77 78 79 |
# File 'lib/sequel_paperclip/attachment.rb', line 77 def path(style = :original) Interpolations.interpolate([:path], self, model, style) end |
#process ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sequel_paperclip/attachment.rb', line 85 def process return unless @queued_file src_path = @queued_file.path [:processors].each do |processor| processor.pre_runs(model, src_path) [:styles].each_pair do |style, | tmp_file = Tempfile.new("paperclip") Sequel::Plugins::Paperclip.logger.debug("processing #{name} for style #{style} with processor #{processor.name}") processor.run(style, , tmp_file) @storage_updates << { :type => STORAGE_UPDATE_SAVE, :src_file => tmp_file, :dst_path => path(style), } end processor.post_runs end @queued_file = nil end |
#update(file) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sequel_paperclip/attachment.rb', line 43 def update(file) if file if file.respond_to?(:tempfile) file = file.tempfile end unless file.is_a?(File) || file.is_a?(Tempfile) raise ArgumentError, "#{name}: #{file} is not a File" end else if exists? [:styles].each_pair do |style, | @storage_updates << { :type => STORAGE_UPDATE_DELETE, :path => path(style), } end end end @queued_file = file end |
#update_storage ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sequel_paperclip/attachment.rb', line 105 def update_storage @storage_updates.each do |update| case update[:type] when STORAGE_UPDATE_SAVE Sequel::Plugins::Paperclip.logger.debug("saving #{update[:dst_path]} (#{update[:src_file].size} bytes)") FileUtils.mkdir_p(File.dirname(update[:dst_path])) FileUtils.cp(update[:src_file].path, update[:dst_path]) FileUtils.chmod(0664, update[:dst_path]) update[:src_file].close! when STORAGE_UPDATE_DELETE Sequel::Plugins::Paperclip.logger.debug("deleting #{update[:path]}") begin FileUtils.rm(update[:path]) rescue Errno::ENOENT => error end else raise ArgumentError, "invalid type '#{update[:type]}'" end end @storage_updates = [] end |
#url(style = :original) ⇒ Object
81 82 83 |
# File 'lib/sequel_paperclip/attachment.rb', line 81 def url(style = :original) Interpolations.interpolate([:url], self, model, style) end |