module OmniHooks::Strategy::ClassMethods

Attributes

namespace[RW]

Public Instance Methods

all(callable = Proc.new) click to toggle source
# File lib/omnihooks/strategy.rb, line 100
def all(callable = Proc.new)
  backend.subscribe(nil, callable)
end
args(args = nil) click to toggle source

Sets (and retrieves) option key names for initializer arguments to be recorded as. This takes care of 90% of the use cases for overriding the initializer in OmniAuth Strategies.

# File lib/omnihooks/strategy.rb, line 52
def args(args = nil)
  if args
    @args = Array(args)
    return
  end
  existing = superclass.respond_to?(:args) ? superclass.args : []
  (instance_variable_defined?(:@args) && @args) || existing
end
configure() { |self| ... } click to toggle source
# File lib/omnihooks/strategy.rb, line 90
def configure(&block)
  raise ArgumentError, "must provide a block" unless block_given?
  block.arity.zero? ? instance_eval(&block) : yield(self)
end
Also aliased as: setup
configure_options(options = nil) { |default_options| ... } click to toggle source

This allows for more declarative subclassing of strategies by allowing default options to be set using a simple configure call.

@param options [Hash] If supplied, these will be the default options (deep-merged into the superclass's default options). @yield [Options] The options Mash that allows you to set your defaults as you'd like.

@example Using a yield to configure the default options.

class MyStrategy
  include OmniHooks::Strategy

  configure_options do |c|
    c.foo = 'bar'
  end
end

@example Using a hash to configure the default options.

class MyStrategy
  include OmniHooks::Strategy
  configure_options foo: 'bar'
end
# File lib/omnihooks/strategy.rb, line 82
def configure_options(options = nil)
  if block_given?
    yield default_options
  else
    default_options.deep_merge!(options)
  end
end
default_options() click to toggle source

Returns an inherited set of default options set at the class-level for each strategy.

# File lib/omnihooks/strategy.rb, line 24
def default_options
  return @default_options if instance_variable_defined?(:@default_options) && @default_options
  existing = superclass.respond_to?(:default_options) ? superclass.default_options : {}
  @default_options = OmniHooks::Strategy::Options.new(existing)
end
instrument(event_type, event_object) click to toggle source
# File lib/omnihooks/strategy.rb, line 123
def instrument(event_type, event_object)
  backend.instrument(namespace.call(event_type), event_object)
end
listening?(name) click to toggle source
# File lib/omnihooks/strategy.rb, line 104
def listening?(name)
  namespaced_name = namespace.call(name)
  backend.notifier.listening?(namespaced_name)
end
option(name, value = nil) click to toggle source

Directly declare a default option for your class. This is a useful from a documentation perspective as it provides a simple line-by-line analysis of the kinds of options your strategy provides by default.

@param name [Symbol] The key of the default option in your configuration hash. @param value [Object] The value your object defaults to. Nil if not provided.

@example

class MyStrategy
  include OmniAuth::Strategy

  option :foo, 'bar'
  option
end
# File lib/omnihooks/strategy.rb, line 45
def option(name, value = nil)
  default_options[name] = value
end
setup(&block)
Alias for: configure
subscribe(name, callable = Proc.new) click to toggle source
# File lib/omnihooks/strategy.rb, line 96
def subscribe(name, callable = Proc.new)
  backend.subscribe(namespace.to_regexp(name), adapter.call(callable))
end

Private Instance Methods

adapter() click to toggle source
# File lib/omnihooks/strategy.rb, line 137
def adapter
  default_options.adapter
end
backend() click to toggle source
# File lib/omnihooks/strategy.rb, line 141
def backend
  default_options.backend
end
compile_stack(ancestors, method, context) click to toggle source
# File lib/omnihooks/strategy.rb, line 129
def compile_stack(ancestors, method, context)
  stack = ancestors.inject([]) do |a, ancestor|
    a << context.instance_eval(&ancestor.send(method)) if ancestor.respond_to?(method) && ancestor.send(method)
    a
  end
  stack.reverse!
end