module Consul::Power::ClassMethods

Public Class Methods

thread_key(klass) click to toggle source
# File lib/consul/power.rb, line 90
def self.thread_key(klass)
  "consul|#{klass.to_s}.current"
end

Public Instance Methods

current() click to toggle source
# File lib/consul/power.rb, line 94
def current
  Thread.current[ClassMethods.thread_key(self)]
end
current=(power) click to toggle source
# File lib/consul/power.rb, line 98
def current=(power)
  Thread.current[ClassMethods.thread_key(self)] = power
end
define_ids_method(name) click to toggle source
# File lib/consul/power.rb, line 144
def define_ids_method(name)
  ids_method = power_ids_name(name)
  define_method(ids_method) { |*args| default_power_ids(name, *args) }
  # Memoize `ids_method` in addition to the collection method itself, since
  # #default_include_object? directly accesses `ids_method`.
  memoize ids_method
end
define_main_method(name, &block) click to toggle source
# File lib/consul/power.rb, line 152
def define_main_method(name, &block)
  define_method(name, &block)
  memoize name
end
define_power(name, &block) click to toggle source
# File lib/consul/power.rb, line 157
def define_power(name, &block)
  name = name.to_s
  if name.ends_with?('?')
    # The developer is trying to register an optimized query method
    # for singular object queries.
    name_without_suffix = name.chop
    define_query_and_bang_methods(name_without_suffix, :is_plural => false, &block)
  else
    define_main_method(name, &block)
    define_ids_method(name)
    define_query_and_bang_methods(name, :is_plural => true) { |*args| default_include_power?(name, *args) }
    begin
      singular = singularize_power_name(name)
      define_query_and_bang_methods(singular, :is_plural => false) { |*args| default_include_object?(name, *args) }
    rescue Consul::PowerNotSingularizable
      # We do not define singularized power methods if it would
      # override the collection method
    end
  end
  name
end
define_query_and_bang_methods(name, options, &query) click to toggle source
# File lib/consul/power.rb, line 124
def define_query_and_bang_methods(name, options, &query)
  is_plural = options.fetch(:is_plural)
  query_method = "#{name}?"
  bang_method = "#{name}!"
  define_method(query_method, &query)
  memoize query_method
  define_method(bang_method) do |*args|
    if is_plural
      if send(query_method, *args)
        send(name, *args)
      else
        powerless!(name, *args)
      end
    else
      send(query_method, *args) or powerless!(name, *args)
    end
  end
  # We don't memoize the bang method since memoizer can't memoize a thrown exception
end
power(*names, &block) click to toggle source
# File lib/consul/power.rb, line 80
def power(*names, &block)
  names.each do |name|
    define_power(name, &block)
  end
end
power_ids_name(name) click to toggle source
# File lib/consul/power.rb, line 86
def power_ids_name(name)
  "#{name.to_s.singularize}_ids"
end
singularize_power_name(name) click to toggle source
# File lib/consul/power.rb, line 179
def singularize_power_name(name)
  name = name.to_s
  singularized = name.singularize
  if singularized == name
    raise Consul::PowerNotSingularizable, "Power name can not have a singular form: #{name}"
  else
    singularized
  end
end
with_power(*args, **kwargs , &block) click to toggle source
# File lib/consul/power.rb, line 102
def with_power(*args, **kwargs , &block)
  inner_power = if args.first.is_a?(self)
    args.first
  elsif args.length == 1 && args.first.nil?
    nil
  elsif kwargs.empty?
    new(*args)
  else
    new(*args, **kwargs)
  end

  old_power = current
  self.current = inner_power
  block.call
ensure
  self.current = old_power
end
without_power(&block) click to toggle source
# File lib/consul/power.rb, line 120
def without_power(&block)
  with_power(nil, &block)
end