class Moonrope::Base

Attributes

instance[RW]

@return [Moonrope::Base] return a global instance

authenticators[RW]

@return [Hash] authenticators

controllers[RW]

@return [Array] the array of defined controllers

dsl[RW]

@return [Moonrope::DSL::BaseDSL] the base DSL

environment[RW]

@return [String] the moonrope environment

force_ssl[RW]

@return [Boolean] is SSL forced?

helpers[RW]

@return [Array] the array of defined helpers

load_directories[RW]

@return [Array] the array of directories to load from (if relevant)

on_request[RW]

@return [Proc] a proc to execute before every request

shared_actions[RW]

@return [Hash] global shared actions

structures[R]

@return [Array] the array of defined structures

Public Class Methods

load(path) click to toggle source

Load a set of Moonrope configuration files from a given directory.

@param path [String] the path to a directory containing Moonrope files @return [Moonrope::Base]

# File lib/moonrope/base.rb, line 13
def self.load(path)
  api = self.new
  api.load(path)
  api
end
new(&block) click to toggle source

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

/(name)
Alias for: controller
[](name)
Alias for: structure
add_load_directory(directory) click to toggle source

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
controller(name) click to toggle source

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
Also aliased as: /
copy() click to toggle source
# File lib/moonrope/base.rb, line 79
def copy
  new_base = self.class.new
  new_base.copy_from(self)
  new_base
end
copy_from(other) click to toggle source

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
external_errors() click to toggle source

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
force_ssl?() click to toggle source

Should SSL be forced?

# File lib/moonrope/base.rb, line 256
def force_ssl?
  @force_ssl || false
end
helper(name, controller = nil) click to toggle source

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
load(*directories) click to toggle source

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
Also aliased as: reload
load_directory(directory) click to toggle source

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_external_error(error_class, &block) click to toggle source

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
register_request_callback(&block) click to toggle source

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
register_request_error_callback(&block) click to toggle source

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
reload(*directories)
Alias for: load
request(*args) click to toggle source

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
request_callbacks() click to toggle source

Return an array of request callbacks

# File lib/moonrope/base.rb, line 249
def request_callbacks
  @request_callbacks ||= []
end
request_error_callbacks() click to toggle source

Return an array of request errors

# File lib/moonrope/base.rb, line 234
def request_error_callbacks
  @request_error_callbacks ||= []
end
structure(name) click to toggle source

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
Also aliased as: []
unload() click to toggle source

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