class Consul::Guard::ActionMap

Public Class Methods

crud(resource, custom_map) click to toggle source
# 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
new(default_power, custom_mappings) click to toggle source
# 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
normalize_map(map) click to toggle source
# 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

Public Instance Methods

power_name(action_name) click to toggle source
# 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