Class: Orange::Application

Inherits:
Object show all
Extended by:
ClassInheritableAttributes
Defined in:
lib/orange-core/application.rb

Overview

Orange::Application is the main class used for building an Orange App. Typically you will not initialize it directly, but use the app method, which returns the entire Orange stack, with all middleware and the Orange::Application as the main receiver

To override the stack generated by default, you can use the self.stack method, which will be used to create a new Orange::Stack

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassInheritableAttributes

cattr_accessor, cattr_reader, cattr_writer, eval_in_accessor_module, fetch_value, store_value

Constructor Details

#initialize(core = false, *opts, &block) ⇒ Application

Initialize will set the core, and additionally accept any other options to be added in to the opts array

Parameters:

  • core (Orange::Core) (defaults to: false)

    the orange core instance that this application will use

  • *opts (Hash)

    the optional arguments



18
19
20
21
22
23
24
# File 'lib/orange-core/application.rb', line 18

def initialize(core = false, *opts, &block)
  @core = core || self.class.core
  @options ||= {}
  @options = Orange::Options.new(*opts, &block).hash.with_defaults(self.class.opts)
  @core.application(self) # Register self into core
  init
end

Class Method Details

.app(c = false, &block) ⇒ Object

Returns an instance of Orange::Stack to be run by Rack

Usually, you’ll call this in the rackup file: ‘run MyApplication.app`



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/orange-core/application.rb', line 109

def self.app(c = false, &block)
  if c
    self.core = c 
  else
    if block_given?
      self.core = Orange::Core.new(&block)
    else
      self.core ||= Orange::Core.new
    end
  end
  return self.core.stack unless self.core.stack.blank?
  if self.stack_block.instance_of?(Proc)
    Orange::Stack.new self, self.core, &self.stack_block   # turn saved proc into a block arg
  else
    Orange::Stack.new self, self.core
  end
end

.optsObject

Gives access to class defined options.



95
96
97
# File 'lib/orange-core/application.rb', line 95

def self.opts
  @class_opts ||= {}
end

.set(key, v = true) ⇒ Object

Used to set optional values at class level. Will be merged into the options given at initialization time



89
90
91
92
# File 'lib/orange-core/application.rb', line 89

def self.set(key, v = true)
  @class_opts ||= {}
  @class_opts[key] = v
end

.stack(core = false, &block) ⇒ Object

Changes the stack that will be used when #app is called

Each call to stack overrides the previous one.



131
132
133
134
135
# File 'lib/orange-core/application.rb', line 131

def self.stack(core = false, &block)
  self.core.stack = nil if self.core   # Wipe old stack from core
  self.core = core if core
  self.stack_block = Proc.new           # pulls in the block and makes it a proc
end

Instance Method Details

#call(env) ⇒ Object

The standard call as required by rack. This will make an Orange::Packet object (if necessary) and then send it to the appropriate router for routing.

If the :self_routing option is true (default) then the packet will be routed by the application if there is not already another class volunteering for that role. (Routers declare themselves in the orange env to be called by the application)



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/orange-core/application.rb', line 60

def call(env)
  packet = Orange::Packet.new(@core, env)
  # Set up this application as router if nothing else has
  # assumed routing responsibility (for Sinatra DSL like routing)
  self_routing = opts[:self_routing] || true
  if (!packet['route.router'] && self_routing)
    packet['route.router'] = self
  end
  packet.route
  packet.finish
end

#initObject

This method is called by initialize, subclasses should override this method for their own initialization needs.

It will usually be better to use stack_init, which gives full access to the initialized stack



37
38
# File 'lib/orange-core/application.rb', line 37

def init
end

#optsHash

Gives access to options for the application, both from the class and the instance level.

Returns:

  • (Hash)

    the options hash



102
103
104
# File 'lib/orange-core/application.rb', line 102

def opts
  @options
end

#orangeOrange::Core

Returns the core

Returns:

  • (Orange::Core)

    the core instance set for the application



74
75
76
# File 'lib/orange-core/application.rb', line 74

def orange
  @core
end

#route(packet) ⇒ Object

The default route method for the application. Must be overridden in subclasses.

This method will raise a RuntimeError if not overridden. The intent is for the application subclass to override this method and use it to handle packets not routed by Stack middleware.



83
84
85
# File 'lib/orange-core/application.rb', line 83

def route(packet)
  raise "default response from Orange::Application.route"
end

#set_core(core) ⇒ Object

Set the orange core to be a new core

Generally, the core should be set during initialization, rather than with this method.

Parameters:



45
46
47
# File 'lib/orange-core/application.rb', line 45

def set_core(core)
  @core = core
end

#stack_initObject

stack_init is ONLY called after the #app method is called, loading a stack (just in case the middleware stack added necessary functionality, etc)



29
30
# File 'lib/orange-core/application.rb', line 29

def stack_init
end