module Consul::Power

Public Class Methods

included(base) click to toggle source
# File lib/consul/power.rb, line 5
def self.included(base)
  base.extend ClassMethods
  base.send :include, Memoized
end

Private Instance Methods

boolean_or_nil?(value) click to toggle source
# File lib/consul/power.rb, line 55
def boolean_or_nil?(value)
  [TrueClass, FalseClass, NilClass].include?(value.class)
end
check_number_of_arguments_in_include_object(power_name, given_arguments) click to toggle source
# File lib/consul/power.rb, line 67
def check_number_of_arguments_in_include_object(power_name, given_arguments)
  # check unmemoized methods as Memoizer wraps methods and masks the arity.
  unmemoized_power_name = respond_to?("_unmemoized_#{power_name}") ? "_unmemoized_#{power_name}" : power_name
  power_arity = method(unmemoized_power_name).arity
  expected_arity = power_arity + 1 # one additional argument for the context
  if power_arity >= 0 && expected_arity != given_arguments
    raise ArgumentError.new("wrong number of arguments (given #{given_arguments}, expected #{expected_arity})")
  end
end
database_touched() click to toggle source
# File lib/consul/power.rb, line 59
def database_touched
  # spy for tests
end
default_include_object?(power_name, *args) click to toggle source
# File lib/consul/power.rb, line 24
def default_include_object?(power_name, *args)
  check_number_of_arguments_in_include_object(power_name, args.length)
  object = args.pop
  context = args
  power_value = send(power_name, *context)
  if power_value.nil?
    false
  elsif Util.scope?(power_value)
    if Util.scope_selects_all_records?(power_value)
      true
    else
      power_ids_name = self.class.power_ids_name(power_name)
      send(power_ids_name, *context).include?(object.id)
    end
  elsif Util.collection?(power_value)
    power_value.include?(object)
  else
    raise Consul::NoCollection, "can only call #include_object? on a collection, but power was of type #{power_value.class.name}"
  end
end
default_include_power?(power_name, *context) click to toggle source
# File lib/consul/power.rb, line 12
def default_include_power?(power_name, *context)
  result = send(power_name, *context)
  # Everything that is not nil is considered as included.
  # We are short-circuiting for #scoped first since sometimes
  # has_many associations (which behave scopish) trigger their query
  # when you try to negate them, compare them or even retrieve their
  # class. Unfortunately we can only reproduce this in live Rails
  # apps, not in Consul tests. Might be some standard gem that is not
  # loaded in Consul tests.
  result.respond_to?(:load_target, true) || !!result
end
default_power_ids(power_name, *args) click to toggle source
# File lib/consul/power.rb, line 45
def default_power_ids(power_name, *args)
  scope = send(power_name, *args)
  database_touched
  scope.collect_ids
end
powerless!(*args) click to toggle source
# File lib/consul/power.rb, line 51
def powerless!(*args)
  raise Consul::Powerless.new("No power to #{[*args].inspect}")
end
singularize_power_name(name) click to toggle source
# File lib/consul/power.rb, line 63
def singularize_power_name(name)
  self.class.singularize_power_name(name)
end