Class: Dragonfly::App

Inherits:
Object show all
Extended by:
Configurable, Forwardable
Defined in:
lib/dragonfly/app.rb

Defined Under Namespace

Classes: UnregisteredDataStore

Constant Summary collapse

DEFAULT_NAME =
:default

Instance Attribute Summary collapse

Attributes included from Configurable

#configurer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

set_up_config

Constructor Details

#initialize(name) ⇒ App

Returns a new instance of App



54
55
56
57
58
59
60
61
# File 'lib/dragonfly/app.rb', line 54

def initialize(name)
  @name = name
  @analysers, @processors, @generators = Register.new, Register.new, Register.new
  @server = Server.new(self)
  @job_methods = Module.new
  @shell = Shell.new
  @env = {}
end

Instance Attribute Details

#allow_legacy_urlsObject

Returns the value of attribute allow_legacy_urls



266
267
268
# File 'lib/dragonfly/app.rb', line 266

def allow_legacy_urls
  @allow_legacy_urls
end

#analysersObject (readonly)

Returns the value of attribute analysers



120
121
122
# File 'lib/dragonfly/app.rb', line 120

def analysers
  @analysers
end

#datastoreObject



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

def datastore
  @datastore ||= FileDataStore.new
end

#envObject (readonly)

Returns the value of attribute env



63
64
65
# File 'lib/dragonfly/app.rb', line 63

def env
  @env
end

#generatorsObject (readonly)

Returns the value of attribute generators



122
123
124
# File 'lib/dragonfly/app.rb', line 122

def generators
  @generators
end

#job_methodsObject (readonly)

Returns the value of attribute job_methods



181
182
183
# File 'lib/dragonfly/app.rb', line 181

def job_methods
  @job_methods
end

#nameObject (readonly)

Returns the value of attribute name



63
64
65
# File 'lib/dragonfly/app.rb', line 63

def name
  @name
end

#processorsObject (readonly)

Returns the value of attribute processors



121
122
123
# File 'lib/dragonfly/app.rb', line 121

def processors
  @processors
end

#secretObject

Returns the value of attribute secret



266
267
268
# File 'lib/dragonfly/app.rb', line 266

def secret
  @secret
end

#serverObject (readonly)

Returns the value of attribute server



123
124
125
# File 'lib/dragonfly/app.rb', line 123

def server
  @server
end

#shellObject (readonly)

Returns the value of attribute shell



175
176
177
# File 'lib/dragonfly/app.rb', line 175

def shell
  @shell
end

Class Method Details

.[](name) ⇒ Object



32
33
34
# File 'lib/dragonfly/app.rb', line 32

def [](name)
  raise "Dragonfly::App[#{name.inspect}] is deprecated - use Dragonfly.app (for the default app) or Dragonfly.app(#{name.inspect}) (for extra named apps) instead. See docs at http://markevans.github.io/dragonfly for details"
end

.appsObject



36
37
38
# File 'lib/dragonfly/app.rb', line 36

def apps
  @apps ||= {}
end

.available_datastoresObject



48
49
50
# File 'lib/dragonfly/app.rb', line 48

def available_datastores
  @available_datastores ||= {}
end

.destroy_appsObject



40
41
42
# File 'lib/dragonfly/app.rb', line 40

def destroy_apps
  apps.clear
end

.instance(name = nil) ⇒ Object



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

def instance(name=nil)
  name ||= DEFAULT_NAME
  name = name.to_sym
  apps[name] ||= new(name)
end

.register_datastore(symbol, &block) ⇒ Object



44
45
46
# File 'lib/dragonfly/app.rb', line 44

def register_datastore(symbol, &block)
  available_datastores[symbol] = block
end

Instance Method Details

#add_analyser(name, callable = nil, &block) ⇒ Object



161
162
163
164
# File 'lib/dragonfly/app.rb', line 161

def add_analyser(name, callable=nil, &block)
  analysers.add(name, callable, &block)
  define(name){ analyse(name) }
end

#add_generator(*args, &block) ⇒ Object



143
144
145
# File 'lib/dragonfly/app.rb', line 143

def add_generator(*args, &block)
  generators.add(*args, &block)
end

#add_mime_type(format, mime_type) ⇒ Object



201
202
203
# File 'lib/dragonfly/app.rb', line 201

def add_mime_type(format, mime_type)
  mime_types[file_ext_string(format)] = mime_type
end

#add_processor(name, callable = nil, &block) ⇒ Object



151
152
153
154
155
# File 'lib/dragonfly/app.rb', line 151

def add_processor(name, callable=nil, &block)
  processors.add(name, callable, &block)
  define(name){|*args| process(name, *args) }
  define("#{name}!"){|*args| process!(name, *args) }
end

#analyser_methodsObject



254
255
256
# File 'lib/dragonfly/app.rb', line 254

def analyser_methods
  analysers.names
end

#define(method, &block) ⇒ Object



193
194
195
# File 'lib/dragonfly/app.rb', line 193

def define(method, &block)
  job_methods.send(:define_method, method, &block)
end

#define_macro(klass, name) ⇒ Object

Raises:

  • (NoMethodError)


268
269
270
# File 'lib/dragonfly/app.rb', line 268

def define_macro(klass, name)
  raise NoMethodError, "define_macro is deprecated - instead of defining #{name}, just extend #{klass.name} with Dragonfly::Model and use dragonfly_accessor"
end

#define_macro_on_include(mod, name) ⇒ Object

Raises:

  • (NoMethodError)


272
273
274
# File 'lib/dragonfly/app.rb', line 272

def define_macro_on_include(mod, name)
  raise NoMethodError, "define_macro_on_include is deprecated - instead of defining #{name}, just extend the relevant class with Dragonfly::Model and use dragonfly_accessor"
end

#define_url(&block) ⇒ Object



227
228
229
# File 'lib/dragonfly/app.rb', line 227

def define_url(&block)
  @url_proc = block
end

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



177
178
179
# File 'lib/dragonfly/app.rb', line 177

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

#ext_for(mime_type) ⇒ Object



213
214
215
216
217
# File 'lib/dragonfly/app.rb', line 213

def ext_for(mime_type)
  return 'txt' if mime_type == 'text/plain'
  ext = key_for(mime_types, mime_type)
  ext.tr('.', '') if ext
end

#fallback_mime_typeObject



262
263
264
# File 'lib/dragonfly/app.rb', line 262

def fallback_mime_type
  'application/octet-stream'
end

#generator_methodsObject



250
251
252
# File 'lib/dragonfly/app.rb', line 250

def generator_methods
  generators.names
end

#get_analyser(name) ⇒ Object



166
167
168
# File 'lib/dragonfly/app.rb', line 166

def get_analyser(name)
  analysers.get(name)
end

#get_generator(name) ⇒ Object



147
148
149
# File 'lib/dragonfly/app.rb', line 147

def get_generator(name)
  generators.get(name)
end

#get_processor(name) ⇒ Object



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

def get_processor(name)
  processors.get(name)
end

#inspectObject



258
259
260
# File 'lib/dragonfly/app.rb', line 258

def inspect
  "<#{self.class.name} name=#{name.inspect} >"
end

#job_classObject



183
184
185
186
187
188
189
190
191
# File 'lib/dragonfly/app.rb', line 183

def job_class
  @job_class ||= begin
    app = self
    Class.new(Job).class_eval do
      include app.job_methods
      self
    end
  end
end

#mime_type_for(format) ⇒ Object



209
210
211
# File 'lib/dragonfly/app.rb', line 209

def mime_type_for(format)
  mime_types[file_ext_string(format)] || fallback_mime_type
end

#mime_typesObject



205
206
207
# File 'lib/dragonfly/app.rb', line 205

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

#new_job(content = "", meta = {}) ⇒ Object Also known as: create



170
171
172
# File 'lib/dragonfly/app.rb', line 170

def new_job(content="", meta={})
  job_class.new(self, content, meta)
end

#processor_methodsObject

Reflection



246
247
248
# File 'lib/dragonfly/app.rb', line 246

def processor_methods
  processors.names
end

#remote_url_for(uid, opts = {}) ⇒ Object



239
240
241
242
243
# File 'lib/dragonfly/app.rb', line 239

def remote_url_for(uid, opts={})
  datastore.url_for(uid, opts)
rescue NoMethodError
  raise NotImplementedError, "The datastore doesn't support serving content directly - #{datastore.inspect}"
end

#response_header(key, value = nil, &block) ⇒ Object



219
220
221
# File 'lib/dragonfly/app.rb', line 219

def response_header(key, value=nil, &block)
  response_headers[key] = value || block
end

#response_headersObject



223
224
225
# File 'lib/dragonfly/app.rb', line 223

def response_headers
  @response_headers ||= {}
end

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



197
198
199
# File 'lib/dragonfly/app.rb', line 197

def store(object, meta={}, opts={})
  create(object, meta).store(opts)
end

#url_for(job, opts = {}) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/dragonfly/app.rb', line 231

def url_for(job, opts={})
  if @url_proc
    @url_proc.call(self, job, opts)
  else
    server.url_for(job, opts)
  end
end

#use_datastore(store, *args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dragonfly/app.rb', line 130

def use_datastore(store, *args)
  self.datastore = if store.is_a?(Symbol)
    get_klass = self.class.available_datastores[store]
    raise UnregisteredDataStore, "the datastore '#{store}' is not registered" unless get_klass
    klass = get_klass.call
    klass.new(*args)
  else
    raise ArgumentError, "datastore only takes 1 argument unless you use a symbol" if args.any?
    store
  end
  raise "datastores have a new interface (read/write/destroy) - see docs at http://markevans.github.io/dragonfly for details" if datastore.respond_to?(:store) && !datastore.respond_to?(:write)
end