class Moonrope::Base
Attributes
@return [Moonrope::Base] return a global instance
@return [Hash] authenticators
@return [Array] the array of defined controllers
@return [Moonrope::DSL::BaseDSL] the base DSL
@return [String] the moonrope environment
@return [Boolean] is SSL forced?
@return [Array] the array of defined helpers
@return [Array] the array of directories to load from (if relevant)
@return [Proc] a proc to execute before every request
@return [Array] the array of defined structures
Public Class Methods
Initialize a new instance of the Moonrope::Base
@yield instance evals the contents within the Base
DSL
# File lib/moonrope/base.rb, line 59 def initialize(&block) unload @environment = 'development' @load_directories = [] @dsl = Moonrope::DSL::BaseDSL.new(self) @dsl.instance_eval(&block) if block_given? end
Public Instance Methods
Add a dirctory to the directories to load
# File lib/moonrope/base.rb, line 146 def add_load_directory(directory) if load_directory(directory) self.load_directories << directory unless self.load_directories.include?(directory) true else false end end
Return a controller of the given name
@param name [Symbol] the name of the controller @return [Moonrope::Controller]
# File lib/moonrope/base.rb, line 173 def controller(name) controllers.select { |a| a.name == name }.first end
# File lib/moonrope/base.rb, line 79 def copy new_base = self.class.new new_base.copy_from(self) new_base end
Make a new base based on configuration
# File lib/moonrope/base.rb, line 70 def copy_from(other) @environment = other.environment @load_directories = other.load_directories @on_request = other.on_request other.request_callbacks.each { |block| self.register_request_callback(&block) } other.request_error_callbacks.each { |block| self.register_request_error_callback(&block) } other.external_errors.each { |error, block| self.register_external_error(error, &block) } end
Return all the external errors which are registered for this base
@return [Hash] a hash of external errors
# File lib/moonrope/base.rb, line 210 def external_errors @external_errors ||= {} end
Should SSL be forced?
# File lib/moonrope/base.rb, line 256 def force_ssl? @force_ssl || false end
Return a helper for the given name and, potentially controller
@param name [Symbol] the name of the helper @param controller [Moonrope::Controller] the controller scope
# File lib/moonrope/base.rb, line 194 def helper(name, controller = nil) if controller matched_helpers = @helpers.select do |h| h.name == name.to_sym && (h.controller.nil? || h.controller == controller) end else matched_helpers = @helpers.select { |h| h.name == name.to_sym && h.controller.nil? } end matched_helpers.first end
Reload this whole base API from the path
# File lib/moonrope/base.rb, line 100 def load(*directories) directories = self.load_directories if directories.empty? if directories.size > 0 unload new_directories = [] directories.each do |directory| if load_directory(directory) new_directories << directory end end self.load_directories = new_directories self else raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory" end end
Load from a given directory
# File lib/moonrope/base.rb, line 122 def load_directory(directory) if File.exist?(directory) @loaded_files = [] Dir[ "#{directory}/structures/**/*.rb", "#{directory}/shared_actions/**/*.rb", "#{directory}/controllers/**/*.rb", "#{directory}/helpers/**/*.rb", "#{directory}/authenticators/**/*.rb", "#{directory}/*.rb", ].each do |filename| next if @loaded_files.include?(filename) @loaded_files << filename self.dsl.instance_eval(File.read(filename), filename) end true else false end end
Register a new external error
@param error_class [Class] a class which should be caught
# File lib/moonrope/base.rb, line 219 def register_external_error(error_class, &block) self.external_errors[error_class] = block end
Set a block which will be executed whenever a request is received by moonrope.
# File lib/moonrope/base.rb, line 242 def register_request_callback(&block) request_callbacks << block end
Set a block which will be executed whenever an error occurs when running an API method
# File lib/moonrope/base.rb, line 227 def register_request_error_callback(&block) request_error_callbacks << block end
Create a new rack request for this API.
@return [Moonrope::Request] a new request object
# File lib/moonrope/base.rb, line 184 def request(*args) Moonrope::Request.new(self, *args) end
Return an array of request callbacks
# File lib/moonrope/base.rb, line 249 def request_callbacks @request_callbacks ||= [] end
Return an array of request errors
# File lib/moonrope/base.rb, line 234 def request_error_callbacks @request_error_callbacks ||= [] end
Return a structure of the given name
@param name [Symbol] the name of the structure @return [Moonrope::Structure]
# File lib/moonrope/base.rb, line 161 def structure(name) structures.select { |s| s.name == name }.first end
Reset the whole base to contain no data.
# File lib/moonrope/base.rb, line 88 def unload @structures = [] @controllers = [] @helpers = @helpers.is_a?(Array) ? @helpers.select { |h| h.options[:unloadable] == false } : [] @authenticators = {} @shared_actions = {} @default_access = nil end