Class: CanCan::RulesCompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan/rules_compressor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ RulesCompressor

Returns a new instance of RulesCompressor



6
7
8
9
# File 'lib/cancan/rules_compressor.rb', line 6

def initialize(rules)
  @initial_rules = rules
  @rules_collapsed = compress(@initial_rules)
end

Instance Attribute Details

#initial_rulesObject (readonly)

Returns the value of attribute initial_rules



4
5
6
# File 'lib/cancan/rules_compressor.rb', line 4

def initial_rules
  @initial_rules
end

#rules_collapsedObject (readonly)

Returns the value of attribute rules_collapsed



4
5
6
# File 'lib/cancan/rules_compressor.rb', line 4

def rules_collapsed
  @rules_collapsed
end

Instance Method Details

#compress(array) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/cancan/rules_compressor.rb', line 11

def compress(array)
  idx = array.rindex(&:catch_all?)
  return array unless idx
  value = array[idx]
  array[idx..-1]
    .drop_while { |n| n.base_behavior == value.base_behavior }
    .tap { |a| a.unshift(value) unless value.cannot_catch_all? }
end