module Consul::Controller::ClassMethods

Public Instance Methods

current_power_initializer() click to toggle source
# File lib/consul/controller.rb, line 20
def current_power_initializer
  @current_power_initializer || (superclass.respond_to?(:current_power_initializer) && superclass.current_power_initializer)
end
current_power_initializer=(initializer) click to toggle source
# File lib/consul/controller.rb, line 24
def current_power_initializer=(initializer)
  @current_power_initializer = initializer
end

Private Instance Methods

consul_features_module() click to toggle source

Instead of using define_method on the controller we’re enhancing, we define dynamic method in a module and have the controller include that. This way the controller can override our generated method and access the original implenentation with super().

See thepugautomatic.com/2013/07/dsom/ for more examples on this technique.

# File lib/consul/controller.rb, line 93
def consul_features_module
  name = :ConsulFeatures
  # Each controller class should get its own FeatureModule, even when
  # we already inherit one from our parent.
  if const_defined?(name, (_search_ancestors = false))
    const_get(name, _search_ancestors = false)
  else
    mod = Module.new
    const_set(name, mod)
    include(mod)
    mod
  end
end
consul_power_args() click to toggle source

On first access we inherit .consul_power_args from our ancestor classes. We also copy inherited args so we don’t change our parent’s .consul_power_args

# File lib/consul/controller.rb, line 109
def consul_power_args
  unless @consul_power_args_initialized
    if superclass && superclass.respond_to?(:consul_power_args, true)
      @consul_power_args = superclass.send(:consul_power_args).dup
    else
      @consul_power_args = []
    end
    @consul_power_args_initialized = true
  end
  @consul_power_args
end
current_power(&initializer) click to toggle source
# File lib/consul/controller.rb, line 39
def current_power(&initializer)
  self.current_power_initializer = initializer
  Util.around_action(self, :with_current_power)

  if respond_to?(:helper_method)
    helper_method :current_power
  end
end
power(*args) click to toggle source
# File lib/consul/controller.rb, line 48
def power(*args)
  guard = Consul::Guard.new(*args)
  controller = self

  # One .power directive will skip the check for all actions, even
  # if that .power directive has :only or :except options.
  skip_power_check

  # Store arguments for testing
  consul_power_args << args

  Util.before_action(self, guard.filter_options) do |controller|
    guard.ensure!(controller, controller.action_name)
  end

  if guard.direct_access_method
    consul_features_module.module_eval do
      # It's dangerous to re-define direct access methods like this:
      #
      #     power :one, as: :my_power
      #     power :two, as: :my_power
      #
      # The method would always check the last power only.
      # To prevent this we're raising an error.
      if method_defined?(guard.direct_access_method)
        raise DuplicateMethod, "Method #{direct_access_method} is already defined on #{controller.name}"
      end

      define_method guard.direct_access_method do
        guard.power_value(self, action_name)
      end

      private guard.direct_access_method
    end
  end

end
require_power_check(options = {}) click to toggle source
# File lib/consul/controller.rb, line 30
def require_power_check(options = {})
  Util.before_action(self, :unchecked_power, options)
end
skip_power_check(options = {}) click to toggle source

This is badly named, since it doesn’t actually skip the :check_power filter

# File lib/consul/controller.rb, line 35
def skip_power_check(options = {})
  Util.skip_before_action(self, :unchecked_power, options)
end