Class: Consul::Guard::ActionMap

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_power, custom_mappings) ⇒ ActionMap

Returns a new instance of ActionMap.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/consul/guard.rb', line 6

def initialize(default_power, custom_mappings)
  @default_power = default_power
  @map = {}
  if custom_mappings.present?
    custom_mappings.each do |action_or_actions, power|
      Array.wrap(action_or_actions).each do |action|
        action = action.to_s
        @map[action] = power
      end
    end
  end
end

Class Method Details

.crud(resource, custom_map) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/consul/guard.rb', line 19

def self.crud(resource, custom_map)
  map = {}
  map[[:show, :index]] = resource.to_sym
  map[[:new, :create]] = "creatable_#{resource}".to_sym
  map[[:edit, :update]] = "updatable_#{resource}".to_sym
  map[:destroy] = "destroyable_#{resource}".to_sym
  map = normalize_map(map).merge(normalize_map(custom_map)) # allow people to override the defaults
  new(resource, map)
end

.normalize_map(map) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/consul/guard.rb', line 29

def self.normalize_map(map)
  normalized_map = {}
  if map.present?
      map.each do |action_or_actions, power|
      Array.wrap(action_or_actions).each do |action|
        action = action.to_s
        normalized_map[action] = power
      end
    end
  end
  normalized_map
end

Instance Method Details

#power_name(action_name) ⇒ Object



42
43
44
45
# File 'lib/consul/guard.rb', line 42

def power_name(action_name)
  action_name = action_name.to_s
  @map[action_name] || @default_power or raise Consul::UnmappedAction, "Could not map the action ##{action_name} to a power"
end