Class: Moonrope::Base
- Inherits:
-
Object
- Object
- Moonrope::Base
- Defined in:
- lib/moonrope/base.rb
Instance Attribute Summary collapse
-
#authenticator ⇒ Proc
The authentictor.
-
#controllers ⇒ Array
The array of defined controllers.
-
#default_access ⇒ Proc
The default access condition.
-
#dsl ⇒ Moonrope::DSL::BaseDSL
The base DSL.
-
#environment ⇒ String
The moonrope environment.
-
#helpers ⇒ Array
The array of defined helpers.
-
#loaded_from ⇒ String
The directory the base was loaded from (if relevant).
-
#structures ⇒ Array
readonly
The array of defined structures.
Class Method Summary collapse
-
.load(path) ⇒ Moonrope::Base
Load a set of Moonrope configuration files from a given directory.
Instance Method Summary collapse
-
#controller(name) ⇒ Moonrope::Controller
(also: #/)
Return a controller of the given name.
-
#external_errors ⇒ Hash
Return all the external errors which are registered for this base.
-
#helper(name, controller = nil) ⇒ Object
Return a helper for the given name and, potentially controller.
-
#initialize { ... } ⇒ Base
constructor
Initialize a new instance of the Moonrope::Base.
-
#load(directory = nil) ⇒ Object
(also: #reload)
Reload this whole base API from the path.
-
#register_external_error(error_class, &block) ⇒ Object
Register a new external error.
-
#request(*args) ⇒ Moonrope::Request
Create a new rack request for this API.
-
#structure(name) ⇒ Moonrope::Structure
(also: #[])
Return a structure of the given name.
-
#unload ⇒ Object
Reset the whole base to contain no data.
Constructor Details
Instance Attribute Details
#authenticator ⇒ Proc
Returns the authentictor.
30 31 32 |
# File 'lib/moonrope/base.rb', line 30 def authenticator @authenticator end |
#controllers ⇒ Array
Returns the array of defined controllers.
21 22 23 |
# File 'lib/moonrope/base.rb', line 21 def controllers @controllers end |
#default_access ⇒ Proc
Returns the default access condition.
33 34 35 |
# File 'lib/moonrope/base.rb', line 33 def default_access @default_access end |
#dsl ⇒ Moonrope::DSL::BaseDSL
Returns the base DSL.
27 28 29 |
# File 'lib/moonrope/base.rb', line 27 def dsl @dsl end |
#environment ⇒ String
Returns the moonrope environment.
39 40 41 |
# File 'lib/moonrope/base.rb', line 39 def environment @environment end |
#helpers ⇒ Array
Returns the array of defined helpers.
24 25 26 |
# File 'lib/moonrope/base.rb', line 24 def helpers @helpers end |
#loaded_from ⇒ String
Returns the directory the base was loaded from (if relevant).
36 37 38 |
# File 'lib/moonrope/base.rb', line 36 def loaded_from @loaded_from end |
#structures ⇒ Array (readonly)
Returns the array of defined structures.
18 19 20 |
# File 'lib/moonrope/base.rb', line 18 def structures @structures end |
Class Method Details
.load(path) ⇒ Moonrope::Base
Load a set of Moonrope configuration files from a given directory.
11 12 13 14 15 |
# File 'lib/moonrope/base.rb', line 11 def self.load(path) api = self.new api.load(path) api end |
Instance Method Details
#controller(name) ⇒ Moonrope::Controller Also known as: /
Return a controller of the given name
101 102 103 |
# File 'lib/moonrope/base.rb', line 101 def controller(name) controllers.select { |a| a.name == name }.first end |
#external_errors ⇒ Hash
Return all the external errors which are registered for this base
138 139 140 |
# File 'lib/moonrope/base.rb', line 138 def external_errors @external_errors ||= {} end |
#helper(name, controller = nil) ⇒ Object
Return a helper for the given name and, potentially controller
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/moonrope/base.rb', line 122 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(directory = nil) ⇒ Object Also known as: reload
Reload this whole base API from the path
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/moonrope/base.rb', line 67 def load(directory = nil) directory = self.loaded_from if directory.nil? if directory unload Dir["#{directory}/**/*.rb"].each do |filename| self.dsl.instance_eval(File.read(filename), filename) end self.loaded_from = directory self else raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory" end end |
#register_external_error(error_class, &block) ⇒ Object
Register a new external error
147 148 149 |
# File 'lib/moonrope/base.rb', line 147 def register_external_error(error_class, &block) self.external_errors[error_class] = block end |
#request(*args) ⇒ Moonrope::Request
Create a new rack request for this API.
112 113 114 |
# File 'lib/moonrope/base.rb', line 112 def request(*args) Moonrope::Request.new(self, *args) end |
#structure(name) ⇒ Moonrope::Structure Also known as: []
Return a structure of the given name
89 90 91 |
# File 'lib/moonrope/base.rb', line 89 def structure(name) structures.select { |s| s.name == name }.first end |
#unload ⇒ Object
Reset the whole base to contain no data.
56 57 58 59 60 61 62 |
# File 'lib/moonrope/base.rb', line 56 def unload @structures = [] @controllers = [] @helpers = @helpers.is_a?(Array) ? @helpers.select { |h| h.[:unloadable] == false } : [] @authenticator = nil @default_access = nil end |