Class: Orange::Core
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
-
.add_pulp(inc) ⇒ Object
Includes module in the Packet class.
-
.mixin(inc) ⇒ Object
Includes module in this class.
Instance Method Summary collapse
-
#[](name, ignore = false) ⇒ Orange::Resource
Accesses resources array, stored as a hash => Resource instance,….
-
#add_pulp(inc) ⇒ Object
Includes module in the Packet class.
-
#afterLoad ⇒ Object
Called by initialize after finished loading.
-
#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.
-
#application(app = false) ⇒ Object
Takes an instance of Orange::Application and saves it.
-
#core_dir(*args) ⇒ String
Returns the orange library directory.
-
#fire(event, packet) ⇒ Boolean
Fires a callback for a given packet (or other object).
-
#initialize(*args, &block) ⇒ Core
constructor
Args will be set to the @options array.
- #inspect ⇒ Object
-
#load(resource, name = false) ⇒ Object
Takes an instance of a Orange::Resource subclass, sets orange then adds it to the orange resources.
-
#loaded?(resource_name) ⇒ Boolean
Returns status of a given resource by short name.
-
#middleware(middle = false) ⇒ Object
Takes an instance of Orange::Middleware::Base subclass and keeps it for later.
-
#mixin(inc) ⇒ Object
Includes module in this class.
-
#options ⇒ Hash
Returns options of the orange core.
-
#orange ⇒ Orange::Core
Convenience self for consistent naming across middleware.
-
#register(event, position = 0, &block) ⇒ Object
Registers interest in a callback for a named event.
-
#stack(new_stack = false) ⇒ Object
Takes an instance of Orange::Stack and saves it.
-
#stack=(new_stack) ⇒ Object
Takes an instance of Orange::Stack and saves it.
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.[: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.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
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
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,…
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
240 241 242 |
# File 'lib/orange-core/core.rb', line 240 def add_pulp(inc) self.class.add_pulp inc end |
#afterLoad ⇒ Object
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
103 104 105 106 107 108 109 |
# File 'lib/orange-core/core.rb', line 103 def app_dir(*args) if args File.join(([:app_dir] ||= Dir.pwd), *args) else [: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
91 92 93 94 95 96 97 |
# File 'lib/orange-core/core.rb', line 91 def core_dir(*args) if args File.join(([:core_dir] ||= File.dirname(__FILE__)), *args) else [:core_dir] ||= File.dirname(__FILE__) end end |
#fire(event, packet) ⇒ Boolean
Fires a callback for a given packet (or other object)
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 |
#inspect ⇒ Object
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.
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
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
246 247 248 |
# File 'lib/orange-core/core.rb', line 246 def mixin(inc) self.class.mixin inc end |
#options ⇒ Hash
Returns options of the orange core
218 219 220 |
# File 'lib/orange-core/core.rb', line 218 def end |
#orange ⇒ Orange::Core
Convenience self for consistent naming across middleware
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.
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 |