Module: Fastr::Filter

Includes:
Log
Included in:
Controller
Defined in:
lib/fastr/filter.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

create_logger, level=

Class Method Details

.execute_filter(filter, obj, *args) ⇒ Object

Executes a filter.

Parameters:

  • filter (Hash)
  • obj (Object)
  • args

Returns:

  • (Object)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastr/filter.rb', line 45

def self.execute_filter(filter, obj, *args)
  if args.length == 0
    state = nil
  else
    state = *args
  end
  
  filter[:methods].each do |method|
    if args.length == 0
      state = obj.send(method)
    else
      state = obj.send(method, state)
    end
  end
  
  return state
end

.get_filters(klass, type) ⇒ Array

Gets the filters for a class.

Parameters:

  • klass (Class)

    The class to get the filters from

  • type (Symbol)

    The type of filters to get

Returns:

  • (Array)


78
79
80
81
82
83
84
85
# File 'lib/fastr/filter.rb', line 78

def self.get_filters(klass, type)
  filter_var = "@filters_#{type}".to_sym
  if klass.instance_variable_defined? filter_var
    return klass.instance_variable_get(filter_var)
  else
    return []
  end
end

.get_filters_for_action(klass, type, action) ⇒ Array

Get all the filters for an action.

Parameters:

  • klass (Class)

    The class for the action

  • type (Symbol)

    Type of filters

  • action (Symbol)

    The action for the class

Returns:

  • (Array)


69
70
71
# File 'lib/fastr/filter.rb', line 69

def self.get_filters_for_action(klass, type, action)
  get_filters(klass, type).find_all { |f| run_filter?(f, action) }
end

.included(kls) ⇒ Object



5
6
7
# File 'lib/fastr/filter.rb', line 5

def self.included(kls)
  kls.extend(ClassMethods)
end

.run_after_filters(controller, klass, action, response) ⇒ Array

Run the response through all after filters and return the new response.

Parameters:

  • controller (Fastr::Controller)

    The controller instance for the action

  • klass (Class)

    The controller class for the action

  • action (Symbol)

    The action that the filters should run for

  • response (Array)

Returns:

  • (Array)

    The new response



31
32
33
34
35
36
37
# File 'lib/fastr/filter.rb', line 31

def self.run_after_filters(controller, klass, action, response)
  new_response = response
  get_filters_for_action(klass, :after, action).each do |filter|
    new_response = execute_filter(filter, controller, new_response)
  end
  new_response
end

.run_before_filters(controller, klass, action) ⇒ Hash

Run through all the before filters and maybe return a response.

Parameters:

  • controller (Fastr::Controller)

    The controller instance for the action

  • klass (Class)

    The controller class for the action

  • action (Symbol)

    The action that the filters should run for

  • response (Array)

Returns:

  • (Hash)

    The new response



16
17
18
19
20
21
22
# File 'lib/fastr/filter.rb', line 16

def self.run_before_filters(controller, klass, action)
  new_response = nil
  get_filters_for_action(klass, :before, action).each do |filter|
    new_response = execute_filter(filter, controller)
  end
  new_response
end

.run_filter?(filter, action) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/fastr/filter.rb', line 89

def self.run_filter?(filter, action)
  return true if filter.has_key? :all and filter[:all] == true
  return true if filter.has_key? :only and filter[:only].include? action
  return true if filter.has_key? :except and not filter[:except].include? action
  return false
end

Instance Method Details

#filter_continueObject

Continue through the filter chain



103
104
105
# File 'lib/fastr/filter.rb', line 103

def filter_continue
  nil
end

#filter_stop(response) ⇒ Object

Halt the filter chain and return a response.

Parameters:

  • response (Hash)

    Rack response



98
99
100
# File 'lib/fastr/filter.rb', line 98

def filter_stop(response)
  response
end