Module: Swee::ControllerFilter

Included in:
Controller
Defined in:
lib/swee/controller_filter.rb

Constant Summary collapse

@@fliter_methods =
Hash.new {|h1,k1| h1[k1] = [] }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fliter_methodsObject



6
7
8
# File 'lib/swee/controller_filter.rb', line 6

def self.fliter_methods
  @@fliter_methods
end

.included(base) ⇒ Object



10
11
12
# File 'lib/swee/controller_filter.rb', line 10

def self.included(base)
  base.extend self
end

Instance Method Details

#define_filterObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/swee/controller_filter.rb', line 52

def define_filter
  self.class.instance_eval do
    [:before_filter,
     # :skip_before_filter,  TODO:
     :after_filter, 
     # :skip_before_filter,  TODO:
     :round_filter, 
     # :skip_before_filter   TODO:
    ].each do |method_name|
      define_method(method_name) do |_method,options={}|
        filter! method_name.to_s.split("_").first.to_sym, _method, options
      end
    end
  end
end

#delete_duplicated_filter(_controller, _method) ⇒ Object



18
19
20
21
22
# File 'lib/swee/controller_filter.rb', line 18

def delete_duplicated_filter _controller,_method
  structs = find_controller _controller
  dup_struct = structs.select { |struct| struct.method == _method }.first
  structs.reject! { |struct| struct.equal?(dup_struct)  }
end

#filter!(_type, _method, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/swee/controller_filter.rb', line 46

def filter! _type, _method, options={}
  delete_duplicated_filter self.name, _method
  fliter_methods[self.name] << FilterStruct.new(_type, _method.to_sym, options) #     {_method => [:before,options] }
  fliter_methods[self.name].uniq!
end

#find_controller(_controller) ⇒ Object



14
15
16
# File 'lib/swee/controller_filter.rb', line 14

def find_controller _controller
  fliter_methods[_controller]
end

#find_filter_methods(_controller, _type, _action) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/swee/controller_filter.rb', line 24

def find_filter_methods _controller,_type,_action
  structs = find_controller _controller
  structs.select { |s|
    _actions = s.actions
    unless _actions.empty?
      if s.actions[:only]
        exist_action = s.actions[:only].map(&:to_sym).include?(_action.to_sym)
      elsif s.actions[:except]
        exist_action = !s.actions[:except].map(&:to_sym).include?(_action.to_sym)
      end
    end
    (_actions.empty?   && s.type == _type)  ||
    (_actions.empty?   && s.type == :round) ||
    ( s.type == _type  && exist_action )    ||
    ( s.type == :round &&  exist_action )
  }.map(&:method)
end

#fliter_methodsObject



42
43
44
# File 'lib/swee/controller_filter.rb', line 42

def fliter_methods
  @@fliter_methods
end