Class: Moonrope::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize { ... } ⇒ Base

Initialize a new instance of the Moonrope::Base

Yields:

  • instance evals the contents within the Base DSL



59
60
61
62
63
64
65
# 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

Class Attribute Details

.instanceMoonrope::Base

Return a global instance

Returns:



21
22
23
# File 'lib/moonrope/base.rb', line 21

def instance
  @instance
end

Instance Attribute Details

#authenticatorsHash

Returns authenticators.

Returns:

  • (Hash)

    authenticators



37
38
39
# File 'lib/moonrope/base.rb', line 37

def authenticators
  @authenticators
end

#controllersArray

Returns the array of defined controllers.

Returns:

  • (Array)

    the array of defined controllers



28
29
30
# File 'lib/moonrope/base.rb', line 28

def controllers
  @controllers
end

#dslMoonrope::DSL::BaseDSL

Returns the base DSL.

Returns:



34
35
36
# File 'lib/moonrope/base.rb', line 34

def dsl
  @dsl
end

#environmentString

Returns the moonrope environment.

Returns:

  • (String)

    the moonrope environment



46
47
48
# File 'lib/moonrope/base.rb', line 46

def environment
  @environment
end

#force_sslBoolean

Returns is SSL forced?.

Returns:

  • (Boolean)

    is SSL forced?



52
53
54
# File 'lib/moonrope/base.rb', line 52

def force_ssl
  @force_ssl
end

#helpersArray

Returns the array of defined helpers.

Returns:

  • (Array)

    the array of defined helpers



31
32
33
# File 'lib/moonrope/base.rb', line 31

def helpers
  @helpers
end

#load_directoriesArray

Returns the array of directories to load from (if relevant).

Returns:

  • (Array)

    the array of directories to load from (if relevant)



43
44
45
# File 'lib/moonrope/base.rb', line 43

def load_directories
  @load_directories
end

#on_requestProc

Returns a proc to execute before every request.

Returns:

  • (Proc)

    a proc to execute before every request



49
50
51
# File 'lib/moonrope/base.rb', line 49

def on_request
  @on_request
end

#shared_actionsHash

Returns global shared actions.

Returns:

  • (Hash)

    global shared actions



40
41
42
# File 'lib/moonrope/base.rb', line 40

def shared_actions
  @shared_actions
end

#structuresArray (readonly)

Returns the array of defined structures.

Returns:

  • (Array)

    the array of defined structures



25
26
27
# File 'lib/moonrope/base.rb', line 25

def structures
  @structures
end

Class Method Details

.load(path) ⇒ Moonrope::Base

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

Parameters:

  • path (String)

    the path to a directory containing Moonrope files

Returns:



13
14
15
16
17
# File 'lib/moonrope/base.rb', line 13

def self.load(path)
  api = self.new
  api.load(path)
  api
end

Instance Method Details

#add_load_directory(directory) ⇒ Object

Add a dirctory to the directories to load



146
147
148
149
150
151
152
153
# 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) ⇒ Moonrope::Controller Also known as: /

Return a controller of the given name

Parameters:

  • name (Symbol)

    the name of the controller

Returns:



173
174
175
# File 'lib/moonrope/base.rb', line 173

def controller(name)
  controllers.select { |a| a.name == name }.first
end

#copyObject



79
80
81
82
83
# 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) ⇒ Object

Make a new base based on configuration



70
71
72
73
74
75
76
77
# 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_errorsHash

Return all the external errors which are registered for this base

Returns:

  • (Hash)

    a hash of external errors



210
211
212
# File 'lib/moonrope/base.rb', line 210

def external_errors
  @external_errors ||= {}
end

#force_ssl?Boolean

Should SSL be forced?

Returns:

  • (Boolean)


256
257
258
# File 'lib/moonrope/base.rb', line 256

def force_ssl?
  @force_ssl || false
end

#helper(name, controller = nil) ⇒ Object

Return a helper for the given name and, potentially controller

Parameters:

  • name (Symbol)

    the name of the helper

  • controller (Moonrope::Controller) (defaults to: nil)

    the controller scope



194
195
196
197
198
199
200
201
202
203
# 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) ⇒ Object Also known as: reload

Reload this whole base API from the path



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_directory(directory) ⇒ Object

Load from a given directory



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object

Register a new external error

Parameters:

  • error_class (Class)

    a class which should be caught



219
220
221
# 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) ⇒ Object

Set a block which will be executed whenever a request is received by moonrope.



242
243
244
# File 'lib/moonrope/base.rb', line 242

def register_request_callback(&block)
  request_callbacks << block
end

#register_request_error_callback(&block) ⇒ Object

Set a block which will be executed whenever an error occurs when running an API method



227
228
229
# File 'lib/moonrope/base.rb', line 227

def register_request_error_callback(&block)
  request_error_callbacks << block
end

#request(*args) ⇒ Moonrope::Request

Create a new rack request for this API.

Returns:



184
185
186
# File 'lib/moonrope/base.rb', line 184

def request(*args)
  Moonrope::Request.new(self, *args)
end

#request_callbacksObject

Return an array of request callbacks



249
250
251
# File 'lib/moonrope/base.rb', line 249

def request_callbacks
  @request_callbacks ||= []
end

#request_error_callbacksObject

Return an array of request errors



234
235
236
# File 'lib/moonrope/base.rb', line 234

def request_error_callbacks
  @request_error_callbacks ||= []
end

#structure(name) ⇒ Moonrope::Structure Also known as: []

Return a structure of the given name

Parameters:

  • name (Symbol)

    the name of the structure

Returns:



161
162
163
# File 'lib/moonrope/base.rb', line 161

def structure(name)
  structures.select { |s| s.name == name }.first
end

#unloadObject

Reset the whole base to contain no data.



88
89
90
91
92
93
94
95
# 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