Class: Moonrope::Base

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

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



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

def initialize(&block)
  unload
  @environment = 'development'
  @dsl = Moonrope::DSL::BaseDSL.new(self)
  @dsl.instance_eval(&block) if block_given?
end

Instance Attribute Details

#authenticatorProc

Returns the authentictor.

Returns:

  • (Proc)

    the authentictor



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

def authenticator
  @authenticator
end

#controllersArray

Returns the array of defined controllers.

Returns:

  • (Array)

    the array of defined controllers



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

def controllers
  @controllers
end

#default_accessProc

Returns the default access condition.

Returns:

  • (Proc)

    the default access condition



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

def default_access
  @default_access
end

#dslMoonrope::DSL::BaseDSL

Returns the base DSL.

Returns:



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

def dsl
  @dsl
end

#environmentString

Returns the moonrope environment.

Returns:

  • (String)

    the moonrope environment



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

def environment
  @environment
end

#helpersArray

Returns the array of defined helpers.

Returns:

  • (Array)

    the array of defined helpers



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

def helpers
  @helpers
end

#loaded_fromString

Returns the directory the base was loaded from (if relevant).

Returns:

  • (String)

    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

#structuresArray (readonly)

Returns the array of defined structures.

Returns:

  • (Array)

    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.

Parameters:

  • path (String)

    the path to a directory containing Moonrope files

Returns:



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

Parameters:

  • name (Symbol)

    the name of the controller

Returns:



101
102
103
# File 'lib/moonrope/base.rb', line 101

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

#external_errorsHash

Return all the external errors which are registered for this base

Returns:

  • (Hash)

    a hash of external errors



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

Parameters:

  • name (Symbol)

    the name of the helper

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

    the controller scope



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

Parameters:

  • error_class (Class)

    a class which should be caught



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.

Returns:



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

Parameters:

  • name (Symbol)

    the name of the structure

Returns:



89
90
91
# File 'lib/moonrope/base.rb', line 89

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

#unloadObject

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.options[:unloadable] == false } : []
  @authenticator = nil
  @default_access = nil
end