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 }
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, *args) ⇒ 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(*args, &block) ⇒ Mash
Returns options of the orange core.
-
#orange ⇒ Orange::Core
Convenience self for consistent naming across middleware.
- #plugins ⇒ Object
-
#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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/orange-core/core.rb', line 86 def initialize(*args, &block) @resources = {} @application = false @stack = false @middleware = [] @events = {} @file = __FILE__ @options = Mash.new(Orange::Options.new(self, *args, &block).hash.with_defaults(DEFAULT_CORE_OPTIONS)) load(Orange::Parser.new, :parser) load(Orange::Mapper.new, :mapper) load(Orange::Scaffold.new, :scaffold) load(Orange::PageParts.new, :page_parts) Orange.plugins(@options['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 afterLoad [:development_mode] = true if ENV['RACK_ENV'] && ENV['RACK_ENV'] == 'development' self end |
Class Method Details
.add_pulp(inc) ⇒ Object
Includes module in the Packet class
296 297 298 |
# File 'lib/orange-core/core.rb', line 296 def self.add_pulp(inc) Packet.mixin inc end |
.mixin(inc) ⇒ Object
Includes module in this class
290 291 292 |
# File 'lib/orange-core/core.rb', line 290 def self.mixin(inc) include inc end |
Instance Method Details
#[](name, ignore = false) ⇒ Orange::Resource
Accesses resources array, stored as a hash => Resource instance,…
268 269 270 271 272 273 274 |
# File 'lib/orange-core/core.rb', line 268 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
278 279 280 |
# File 'lib/orange-core/core.rb', line 278 def add_pulp(inc) self.class.add_pulp inc end |
#afterLoad ⇒ Object
Called by initialize after finished loading
134 135 136 |
# File 'lib/orange-core/core.rb', line 134 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
125 126 127 128 129 130 131 |
# File 'lib/orange-core/core.rb', line 125 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.
188 189 190 191 |
# File 'lib/orange-core/core.rb', line 188 def application(app = false) @application = app if app @application end |
#core_dir(*args) ⇒ String
Returns the orange library directory
113 114 115 116 117 118 119 |
# File 'lib/orange-core/core.rb', line 113 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, *args) ⇒ Boolean
Fires a callback for a given packet (or other object)
243 244 245 246 247 248 249 250 |
# File 'lib/orange-core/core.rb', line 243 def fire(event, packet, *args) return false unless @events[event] @events[event].compact! for callback in @events[event] callback.call(packet, *args) end true end |
#inspect ⇒ Object
300 301 302 |
# File 'lib/orange-core/core.rb', line 300 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.
0.7 Feature - Passing a Carton class instead of an Orange::Resource instance will call Carton#as_resource and then load the ModelResource for that carton
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/orange-core/core.rb', line 164 def load(resource, name = false) if(resource.instance_of?(Class) && (resource < Orange::Carton)) carton = resource # Resource isn't really ar resource carton.as_resource resource_class = Object.const_get("#{carton.to_s}_Resource") resource = resource_class.new name = carton.to_s.gsub(/::/, '_').downcase.to_sym if(!name) end 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
141 142 143 |
# File 'lib/orange-core/core.rb', line 141 def loaded?(resource_name) @resources.has_key?(resource_name) && (!@resources[resource_name].blank?) 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)
182 183 184 185 |
# File 'lib/orange-core/core.rb', line 182 def middleware(middle = false) @middleware << middle if middle @middleware end |
#mixin(inc) ⇒ Object
Includes module in this class
284 285 286 |
# File 'lib/orange-core/core.rb', line 284 def mixin(inc) self.class.mixin inc end |
#options(*args, &block) ⇒ Mash
Returns options of the orange core
255 256 257 258 |
# File 'lib/orange-core/core.rb', line 255 def (*args, &block) @options.merge(Options.new(self, *args, &block).hash) if (args.size > 0 || block_given?) @options end |
#orange ⇒ Orange::Core
Convenience self for consistent naming across middleware
209 |
# File 'lib/orange-core/core.rb', line 209 def orange; self; end |
#plugins ⇒ Object
304 305 306 |
# File 'lib/orange-core/core.rb', line 304 def plugins Orange.plugins(['plugins']) 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.
226 227 228 229 230 231 232 233 234 |
# File 'lib/orange-core/core.rb', line 226 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.
194 195 196 197 198 |
# File 'lib/orange-core/core.rb', line 194 def stack(new_stack = false) @stack = new_stack if new_stack @middleware = [] if new_stack @stack end |
#stack=(new_stack) ⇒ Object
Takes an instance of Orange::Stack and saves it.
201 202 203 204 205 |
# File 'lib/orange-core/core.rb', line 201 def stack=(new_stack) @stack = new_stack @middleware = [] @stack end |