Class: Dragonfly::App

Inherits:
Object show all
Defined in:
lib/dragonfly/app.rb

Constant Summary collapse

SAVED_CONFIGS =
{
  :imagemagick => 'ImageMagick',
  :image_magick => 'ImageMagick',
  :rmagick => 'RMagick',
  :r_magick => 'RMagick',
  :rails => 'Rails',
  :heroku => 'Heroku'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



26
27
28
29
30
31
32
33
34
# File 'lib/dragonfly/app.rb', line 26

def initialize
  @analyser, @processor, @encoder, @generator = Analyser.new, Processor.new, Encoder.new, Generator.new
  @analyser.use_same_log_as(self)
  @processor.use_same_log_as(self)
  @encoder.use_same_log_as(self)
  @generator.use_same_log_as(self)
  @job_definitions = JobDefinitions.new
  @server = Dragonfly::SimpleEndpoint.new(self)
end

Instance Attribute Details

#analyserObject (readonly)

Returns the value of attribute analyser



56
57
58
# File 'lib/dragonfly/app.rb', line 56

def analyser
  @analyser
end

#encoderObject (readonly)

Returns the value of attribute encoder



58
59
60
# File 'lib/dragonfly/app.rb', line 58

def encoder
  @encoder
end

#generatorObject (readonly)

Returns the value of attribute generator



59
60
61
# File 'lib/dragonfly/app.rb', line 59

def generator
  @generator
end

#job_definitionsObject

Returns the value of attribute job_definitions



67
68
69
# File 'lib/dragonfly/app.rb', line 67

def job_definitions
  @job_definitions
end

#processorObject (readonly)

Returns the value of attribute processor



57
58
59
# File 'lib/dragonfly/app.rb', line 57

def processor
  @processor
end

#serverObject (readonly)

Returns the value of attribute server



60
61
62
# File 'lib/dragonfly/app.rb', line 60

def server
  @server
end

Class Method Details

.instance(name) ⇒ Object



12
13
14
# File 'lib/dragonfly/app.rb', line 12

def instance(name)
  apps[name] ||= new
end

Instance Method Details

#configurer_for(symbol) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/dragonfly/app.rb', line 78

def configurer_for(symbol)
  class_name = SAVED_CONFIGS[symbol]
  if class_name.nil?
    raise ArgumentError, "#{symbol.inspect} is not a known configuration - try one of #{SAVED_CONFIGS.keys.join(', ')}"
  end
  Config.const_get(class_name)
end

#define_macro(mod, macro_name) ⇒ Object



157
158
159
160
161
# File 'lib/dragonfly/app.rb', line 157

def define_macro(mod, macro_name)
  already_extended = (class << mod; self; end).included_modules.include?(ActiveModelExtensions)
  mod.extend(ActiveModelExtensions) unless already_extended
  mod.register_dragonfly_app(macro_name, self)
end

#define_macro_on_include(mod, macro_name) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dragonfly/app.rb', line 163

def define_macro_on_include(mod, macro_name)
  app = self
  (class << mod; self; end).class_eval do
    alias included_without_dragonfly included
    define_method :included_with_dragonfly do |mod|
      included_without_dragonfly(mod)
      app.define_macro(mod, macro_name)
    end
    alias included included_with_dragonfly
  end
end

#endpoint(job = nil, &block) ⇒ Object



90
91
92
# File 'lib/dragonfly/app.rb', line 90

def endpoint(job=nil, &block)
  block ? RoutedEndpoint.new(self, &block) : JobEndpoint.new(job)
end

#job(name, &block) ⇒ Object



94
95
96
# File 'lib/dragonfly/app.rb', line 94

def job(name, &block)
  job_definitions.add(name, &block)
end

#mime_type_for(format) ⇒ Object



114
115
116
# File 'lib/dragonfly/app.rb', line 114

def mime_type_for(format)
  registered_mime_types[file_ext_string(format)]
end

#mount_pathObject



126
127
128
# File 'lib/dragonfly/app.rb', line 126

def mount_path
  url_path_prefix.blank? ? '/' : url_path_prefix
end

#new_job(content = nil, opts = {}) ⇒ Object



86
87
88
# File 'lib/dragonfly/app.rb', line 86

def new_job(content=nil, opts={})
  content ? Job.new(self, TempObject.new(content, opts)) : Job.new(self)
end

#register_mime_type(format, mime_type) ⇒ Object



105
106
107
# File 'lib/dragonfly/app.rb', line 105

def register_mime_type(format, mime_type)
  registered_mime_types[file_ext_string(format)] = mime_type
end

#registered_mime_typesObject



110
111
112
# File 'lib/dragonfly/app.rb', line 110

def registered_mime_types
  @registered_mime_types ||= Rack::Mime::MIME_TYPES.dup
end

#resolve_mime_type(temp_object) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/dragonfly/app.rb', line 118

def resolve_mime_type(temp_object)
  mime_type_for(temp_object.format)                                   ||
    (mime_type_for(temp_object.ext) if infer_mime_type_from_file_ext) ||
    analyser.analyse(temp_object, :mime_type)                         ||
    mime_type_for(analyser.analyse(temp_object, :format))             ||
    fallback_mime_type
end

#store(object, opts = {}) ⇒ Object



99
100
101
102
103
# File 'lib/dragonfly/app.rb', line 99

def store(object, opts={})
  temp_object = object.is_a?(TempObject) ? object : TempObject.new(object)
  temp_object.extract_attributes_from(opts)
  datastore.store(temp_object, opts)
end

#url_for(job, *args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dragonfly/app.rb', line 130

def url_for(job, *args)
  if (args.length == 1 && args.first.kind_of?(Hash)) || args.empty?
    opts = args.first ? args.first.dup : {}
    host = opts.delete(:host) || url_host
    suffix = opts.delete(:suffix) || url_suffix
    suffix = suffix.call(job) if suffix.respond_to?(:call)
    path_prefix = opts.delete(:path_prefix) || url_path_prefix
    path = "#{host}#{path_prefix}#{job.to_path}#{suffix}"
    query = opts
    query.merge!(server.required_params_for(job)) if protect_from_dos_attacks
    path << "?#{Rack::Utils.build_query(query)}" if query.any?
    path
  else
    # Deprecation stuff - will be removed!!!
    case args[0]
    when /^(\d+)?x(\d+)?/
      log.warn("DEPRECATED USE OF url_for and will be removed in the future - please use thumb(#{args.map{|a|a.inspect}.join(', ')}).url")
      args[1] ? job.thumb(args[0], args[1]).url : job.thumb(args[0]).url
    when :gif, :png, :jpg, :jpeg
      log.warn("DEPRECATED USE OF url_for and will be removed in the future - please use encode(#{args.first.inspect}).url")
      job.encode(args[0]).url
    else
      raise "DEPRECATED USE OF url_for - will be removed in future versions - please consult the docs"
    end
  end
end