class ActionParameter::Base

Attributes

params[RW]

Public Class Methods

new(params) click to toggle source

initialize: Initialize parameter class and creates controller_name and action_name helpers.

Options

  • params - The ActionController::Parameters instance from the controller who initialize this.

# File lib/action_parameter/base.rb, line 11
def initialize(params)
  @params = params
  create_base_helpers
end

Public Instance Methods

locals(locals = {}) click to toggle source

locals: Creates helper methods for the ActionParameter instace.

Options

  • locals - Hash used to create helper methods available for the ActionParameter instance.

Examples

  • locals(new_method: @value, another_method: @other_value) # => ‘ActionParameter instace’

Returns the ActionParameter instace.

# File lib/action_parameter/base.rb, line 28
def locals(locals = {})
  create_methods(locals)
  self
end

Protected Instance Methods

create_base_helpers() click to toggle source

create_base_helpers: Creates controller_name and action_name helper methods, every time an ActionParameter instace is created.

# File lib/action_parameter/base.rb, line 36
def create_base_helpers
  locals = { controller_name: params[:controller],
             action_name:     params[:action] }
  create_methods(locals)
end
create_methods(locals = {}) click to toggle source

create_methods: Creates instance methods using locals hash’s keys and values.

Options

  • locals - Hash used to create helper methods available for the ActionParameter instance. Methods will be named using the hash keys and they’ll return the hash values.

Examples

  create_methods(current_user: @user, another_key: @another_variable)

Will create 'current_user' and 'another_key' instance methods.
This methods will be aviable only in the current parameter class where create_method was called.
'current_user'    will return @user.
'another_key'     will return @another_variable
# File lib/action_parameter/base.rb, line 56
def create_methods(locals = {})
  locals = {} unless locals

  klass = class << self; self; end

  locals.each do |method_name, value|
    klass.send(:define_method, method_name) do
      value
    end
  end
end