Class: Orange::Core

Inherits:
Object show all
Defined in:
lib/orange-core/core.rb

Overview

Core is one of two main sources of interaction for Orange Applications

All portions of Orange based code have access to the Core upon initialization. Orange allows access to individual resources, and also allows single point for event registration and firing.

Functionality of the core can be extended by loading resources, or by mixins that directly affect the Core. Generally, resources are the less convoluted (easier to debug) way to do it.

Constant Summary collapse

DEFAULT_CORE_OPTIONS =

Sets the default options for Orange Applications

{
  :contexts => [:live, :admin, :orange],
  :default_context => :live,
  :default_resource => :not_found,
  :default_database => 'sqlite3::memory:'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Core

Args will be set to the @options array. Block DSL style option setting also available:

orange = Orange::Core.new(:optional_option => 'foo') do
  haml true
  site_name "Banana"
  custom_router MyRouterClass.new
end

orange.options[:site_name] #=> "Banana"

This method calls afterLoad when it is done. Subclasses can override the afterLoad method for initialization needs.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/orange-core/core.rb', line 65

def initialize(*args, &block)
  @options = Options.new(*args, &block).hash.with_defaults(DEFAULT_CORE_OPTIONS)
  @resources = {}
  @application = false
  @stack = false
  @middleware = []
  @events = {}
  @file = __FILE__
  load(Orange::Parser.new, :parser)
  load(Orange::Mapper.new, :mapper)
  load(Orange::Scaffold.new, :scaffold)
  load(Orange::PageParts.new, :page_parts)
  Orange.plugins.each{|p| p.resources.each{|args| load(*args)} if p.has_resources?}
  self.register(:stack_loaded) do |s| 
    @middleware.each{|m| m.stack_init if m.respond_to?(:stack_init)}
    @application.stack_init if @application
  end
  self.register(:stack_reloading){|s| @middleware = []} # Dump middleware on stack reload
  # load(Orange::AdminResource.new, :admin)
  afterLoad
  self
end

Class Method Details

.add_pulp(inc) ⇒ Object

Includes module in the Packet class

Parameters:

  • inc (Module)

    module to be included



258
259
260
# File 'lib/orange-core/core.rb', line 258

def self.add_pulp(inc)
  Packet.mixin inc
end

.mixin(inc) ⇒ Object

Includes module in this class

Parameters:

  • inc (Module)

    module to be included



252
253
254
# File 'lib/orange-core/core.rb', line 252

def self.mixin(inc)
  include inc
end

Instance Method Details

#[](name, ignore = false) ⇒ Orange::Resource

Accesses resources array, stored as a hash => Resource instance,…

Parameters:

  • name (Symbol)

    the short name for the requested resource

  • ignore (optional, Boolean) (defaults to: false)

    Whether to ignore any calls to resource if not found (false is default). This will allow method calls to non-existent resources. Should be used with caution.

Returns:



230
231
232
233
234
235
236
# File 'lib/orange-core/core.rb', line 230

def [](name, ignore = false)
  if ignore && !loaded?(name)
    Ignore.new
  else
    @resources[name]
  end 
end

#add_pulp(inc) ⇒ Object

Includes module in the Packet class

Parameters:

  • inc (Module)

    module to be included



240
241
242
# File 'lib/orange-core/core.rb', line 240

def add_pulp(inc)
  self.class.add_pulp inc
end

#afterLoadObject

Called by initialize after finished loading



112
113
114
# File 'lib/orange-core/core.rb', line 112

def afterLoad
  true
end

#app_dir(*args) ⇒ String

Returns the directory of the currently executing file (using Dir.pwd), can be overriden using the option :app_dir in initialization

Returns:

  • (String)

    the directory name of the currently running application



103
104
105
106
107
108
109
# File 'lib/orange-core/core.rb', line 103

def app_dir(*args)
  if args
    File.join((options[:app_dir] ||= Dir.pwd), *args)
  else
    options[:app_dir] ||= Dir.pwd
  end
end

#application(app = false) ⇒ Object

Takes an instance of Orange::Application and saves it.



154
155
156
157
# File 'lib/orange-core/core.rb', line 154

def application(app = false)
  @application = app if app
  @application
end

#core_dir(*args) ⇒ String

Returns the orange library directory

Returns:

  • (String)

    the directory name indicating where the core file is located



91
92
93
94
95
96
97
# File 'lib/orange-core/core.rb', line 91

def core_dir(*args)
  if args
    File.join((options[:core_dir] ||= File.dirname(__FILE__)), *args)
  else
    options[:core_dir] ||= File.dirname(__FILE__)
  end
end

#fire(event, packet) ⇒ Boolean

Fires a callback for a given packet (or other object)

Parameters:

  • event (Symbol)

    name of event something has registered for

  • packet (Orange::Packet, object)

    Object, generally Orange::Packet, causing the fire. This is passed to each Proc registered.

Returns:

  • (Boolean)

    returns false if nothing has been registered for the event, otherwise true.



206
207
208
209
210
211
212
213
# File 'lib/orange-core/core.rb', line 206

def fire(event, packet)
  return false unless @events[event]
  @events[event].compact!
  for callback in @events[event]
    callback.call(packet)
  end
  true
end

#inspectObject



262
263
264
# File 'lib/orange-core/core.rb', line 262

def inspect
  "#<Orange::Core:0x#{self.object_id.to_s(16)}>"
end

#load(resource, name = false) ⇒ Object

Takes an instance of a Orange::Resource subclass, sets orange then adds it to the orange resources

It can be assigned a short name to be used for accessing later on. If no short name is assigned, one will be generated by downcasing the class name and changing it to a symbol

Resources must respond to set_orange, which is automatically used to create a link back to the Core, and to notify the resource of its assigned short name.

Parameters:

  • resource (Orange::Resource)

    An instance of Orange::Resource subclass

  • name (optional, Symbol, String) (defaults to: false)

    A short name to assign as key in Hash list of resources. Doesn’t necessarily need to be a symbol, but generally is. Set to the class name lowercase as a symbol by default.



139
140
141
142
143
# File 'lib/orange-core/core.rb', line 139

def load(resource, name = false)
  name = resource.orange_name if(!name)
  name = resource.class.to_s.gsub(/::/, '_').downcase.to_sym if(!name) 
  @resources[name] = resource.set_orange(self, name)
end

#loaded?(resource_name) ⇒ Boolean

Returns status of a given resource by short name

Parameters:

  • resource_name (Symbol)

    The short name of the resource

Returns:

  • (Boolean)

    result of has_key? in the resources list



119
120
121
# File 'lib/orange-core/core.rb', line 119

def loaded?(resource_name)
  @resources.has_key?(resource_name)
end

#middleware(middle = false) ⇒ Object

Takes an instance of Orange::Middleware::Base subclass and keeps it for later. This way we can provide introspection into the middleware instances (useful for calling stack_init on them)



148
149
150
151
# File 'lib/orange-core/core.rb', line 148

def middleware(middle = false)
  @middleware << middle if middle
  @middleware
end

#mixin(inc) ⇒ Object

Includes module in this class

Parameters:

  • inc (Module)

    module to be included



246
247
248
# File 'lib/orange-core/core.rb', line 246

def mixin(inc)
  self.class.mixin inc
end

#optionsHash

Returns options of the orange core

Returns:

  • (Hash)

    Hash of options



218
219
220
# File 'lib/orange-core/core.rb', line 218

def options
  @options
end

#orangeOrange::Core

Convenience self for consistent naming across middleware

Returns:



172
# File 'lib/orange-core/core.rb', line 172

def orange;     self;     end

#register(event, position = 0, &block) ⇒ Object

Registers interest in a callback for a named event.

Event registration is stored as a hash list of events and arrays of procs to be executed on each event.

Parameters:

  • event (Symbol)

    the name of the event registered for

  • position (optional, Integer) (defaults to: 0)

    the position to place the event in, by default goes to the front of the list. Doesn’t necessarily need to be exact count, empty spaces in array are taken out. Forcing the event to be at 99 or some such position will typically make sure it happens last in the firing process.

  • block (Block)

    The code to be executed upon event firing. Saved to an array of procs that are called when #fire is called. Block must accept one param, which is the intended to be the packet causing the block to fire, unless the event happens in setup.



189
190
191
192
193
194
195
196
197
# File 'lib/orange-core/core.rb', line 189

def register(event, position = 0, &block)
  if block_given?
    if @events[event] 
      @events[event].insert(position, Proc.new)
    else
      @events[event] = Array.new.insert(position, Proc.new)
    end
  end
end

#stack(new_stack = false) ⇒ Object

Takes an instance of Orange::Stack and saves it.



160
161
162
163
# File 'lib/orange-core/core.rb', line 160

def stack(new_stack = false)
  @stack = new_stack if new_stack
  @stack
end

#stack=(new_stack) ⇒ Object

Takes an instance of Orange::Stack and saves it.



166
167
168
# File 'lib/orange-core/core.rb', line 166

def stack=(new_stack)
  @stack = new_stack
end