Module: OmniAuth::Strategy::ClassMethods

Defined in:
lib/omniauth/strategy.rb

Instance Method Summary collapse

Instance Method Details

#args(args = nil) ⇒ Object

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.



82
83
84
85
86
87
88
89
# File 'lib/omniauth/strategy.rb', line 82

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

#compile_stack(ancestors, method, context) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/omniauth/strategy.rb', line 107

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

#configure(options = nil) {|Options| ... } ⇒ Object

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

Examples:

Using a yield to configure the default options.


class MyStrategy
  include OmniAuth::Strategy

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

Using a hash to configure the default options.


class MyStrategy
  include OmniAuth::Strategy
  configure foo: 'bar'
end

Parameters:

  • options (Hash) (defaults to: nil)

    If supplied, these will be the default options (deep-merged into the superclass's default options).

Yields:

  • (Options)

    The options Mash that allows you to set your defaults as you'd like.



52
53
54
55
56
57
58
# File 'lib/omniauth/strategy.rb', line 52

def configure(options = nil)
  if block_given?
    yield default_options
  else
    default_options.deep_merge!(options)
  end
end

#default_optionsObject

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



24
25
26
27
28
# File 'lib/omniauth/strategy.rb', line 24

def default_options
  # existing = superclass.default_options if superclass.respond_to?(:default_options)
  existing = superclass.respond_to?(:default_options) ? superclass.default_options : {}
  @default_options ||= OmniAuth::Strategy::Options.new(existing)
end

#option(name, value = nil) ⇒ Object

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.

Examples:


class MyStrategy
  include OmniAuth::Strategy

  option :foo, 'bar'
  option
end

Parameters:

  • name (Symbol)

    The key of the default option in your configuration hash.

  • value (Object) (defaults to: nil)

    The value your object defaults to. Nil if not provided.



75
76
77
# File 'lib/omniauth/strategy.rb', line 75

def option(name, value = nil)
  default_options[name] = value
end