Class: Moonrope::DSL::ControllerDSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ ControllerDSL

Initialize a new ControllerDSL

Parameters:



10
11
12
# File 'lib/moonrope/dsl/controller_dsl.rb', line 10

def initialize(controller)
  @controller = controller
end

Instance Attribute Details

#controllerMoonrope::Controller (readonly)

Returns the associated controller.

Returns:



15
16
17
# File 'lib/moonrope/dsl/controller_dsl.rb', line 15

def controller
  @controller
end

Instance Method Details

#access(value = nil, &block) ⇒ Object

Defines the access required for controller methods which do not define their own access.



50
51
52
# File 'lib/moonrope/dsl/controller_dsl.rb', line 50

def access(value = nil, &block)
  @controller.access = block_given? ? block : value
end

#action(name) { ... } ⇒ Moonrope::Action

Defines a new action within the controller.

Parameters:

  • name (Symbol)

Yields:

  • instance evals the block within the ActionDSL

Returns:



24
25
26
27
28
29
# File 'lib/moonrope/dsl/controller_dsl.rb', line 24

def action(name, &block)
  action = Moonrope::Action.new(@controller, name)
  action.dsl.instance_eval(&block) if block_given?
  @controller.actions[name] = action
  action
end

#before(*actions) { ... } ⇒ Moonrope::BeforeAction

Defines a new before action within the controller.

Parameters:

  • actions (Symbol)

    the names of the actions to apply to (none for all)

Yields:

  • stores the block as the block to be executed

Returns:



38
39
40
41
42
43
44
# File 'lib/moonrope/dsl/controller_dsl.rb', line 38

def before(*actions, &block)
  before_action = Moonrope::BeforeAction.new(@controller)
  before_action.block = block
  before_action.actions = actions
  @controller.befores << before_action
  before_action
end

#helper(name, options = {}) { ... } ⇒ Object

Defines a new helper for this controller.

Parameters:

  • name (Symbol)

    the name of the helper

Yields:

  • stores the block to execute for the helper



60
61
62
63
64
65
66
# File 'lib/moonrope/dsl/controller_dsl.rb', line 60

def helper(name, options = {}, &block)
  if @controller.base.helper(name, @controller)
    raise Moonrope::Errors::HelperAlreadyDefined, "Helper has already been defined with name `#{name}`"
  end
  
  @controller.base.helpers << Moonrope::Helper.new(name, @controller, options, &block)
end