Class: Ixtlan::Guard::ActionController::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/guard/guard_rails.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, options, &block) ⇒ Filter

Returns a new instance of Filter.



11
12
13
14
15
16
17
18
19
# File 'lib/ixtlan/guard/guard_rails.rb', line 11

def initialize(method, options, &block)
  @only = options[:only]
  @except = options[:except] || []
  @reference = options[:reference]
  @reference = @reference.to_sym if @reference
  @block = block
  @method = method.to_sym if method
  raise "illegal arguments: either block or method name #{method}" if block && method
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



10
11
12
# File 'lib/ixtlan/guard/guard_rails.rb', line 10

def block
  @block
end

Instance Method Details

#allowed?(action) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/ixtlan/guard/guard_rails.rb', line 59

def allowed?(action)
  action = action.to_sym
  (@only && @only.member?(action)) ||
    ((@only.nil? || @only.empty?) && ! @except.member?(action))
end

#proc(base, reference = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ixtlan/guard/guard_rails.rb', line 21

def proc(base, reference = nil)
  if @block
    if reference
      Proc.new do |groups|
        @block.call(groups, reference)
      end
    elsif @reference
      Proc.new do |groups|
         @block.call(groups, base.send(@reference))
      end
    else
      @block
    end
  else
    if base.respond_to?(@method) || base.private_methods.include?(@method.to_s) || base.private_methods.include?(@method.to_sym)
      if reference
        Proc.new do |groups|
          base.send(@method, groups, reference)
        end
      elsif @reference
        Proc.new do |groups|
          base.send(@method, groups, base.send(@reference))
        end
      else
        base.method(@method)
      end
    else
      if @reference
        Proc.new do |groups|
          base.class.send(@method, groups, base.send(@reference))
        end
      else
        base.class.method(@method)
      end
    end
  end
end