class Permission

@!visibility private

Attributes

actions[R]
conditions[R]
object[R]

Public Class Methods

new(object, *actions, **conditions) click to toggle source

@param [Object] object @param [*Symbol] actions @param [**Hash] conditions @return [Permission] New instance of {Permission}.

# File lib/ratify/permission.rb, line 9
def initialize(object, *actions, **conditions)
  @object = object
  @actions = actions
  @conditions = conditions
end

Public Instance Methods

permits?(object, *actions, scope: nil) click to toggle source

@param [Object] object @param [*Symbol] actions @param [Object] scope @return [true | false] Whether or not the object is permitted.

# File lib/ratify/permission.rb, line 19
def permits?(object, *actions, scope: nil)
  object_matches?(object) &&
  action_matches?(actions) &&
  conditions_match?(object, scope, actions)
end

Private Instance Methods

action_matches?(actions) click to toggle source

@param [Array<Symbol>] actions @return [true | false] If the given actions are all in the permission.

# File lib/ratify/permission.rb, line 29
def action_matches?(actions)
  self.actions.include?(:full_access) ||

  actions.all? { |action| self.actions.include?(action) }
end
condition_matches?(name, condition, object, scope, actions) click to toggle source

@param [Symbol] name @param [Proc | Symbol | Object] condition @param [Object] object @param [Object] scope @param [Array<Symbol>] actions @return [true | false] Whether or not the condition matches the permission.

# File lib/ratify/permission.rb, line 41
def condition_matches?(name, condition, object, scope, actions)
  match = case condition
  when Proc then scope.instance_exec(object, *actions, &condition)
  when Symbol then scope.send(condition)
  else scope.send(name) == condition
  end

  name == :unless ? !match : match && true
end
conditions_match?(object, scope, actions) click to toggle source

@param [Object] object @param [Object] scope @param [Array<Symbol>] actions @return [true | false] Whether or not all of the conditions match.

# File lib/ratify/permission.rb, line 55
def conditions_match?(object, scope, actions)
  conditions.empty? || conditions.all? do |name, condition|
    condition_matches?(name, condition, object, scope, actions)
  end
end
object_as_constant() click to toggle source

@return [Constant] The object as a constant.

# File lib/ratify/permission.rb, line 62
def object_as_constant
  return unless object.is_a?(String) || object.is_a?(Symbol)

  Object.const_get(object, false) rescue nil
end
object_matches?(object) click to toggle source

@param [Object] object @return [true | false] Whether or not the object matches.

# File lib/ratify/permission.rb, line 70
def object_matches?(object)
  self.object === object || object_as_constant === object
end