Module: Dobby::Strategy::ClassMethods

Defined in:
lib/dobby/strategy.rb

Overview

Extensible configuration for implementers.

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 Dobby Strategies. Dobby::Options will also use this, via #cli_options, to configure any command line options.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dobby/strategy.rb', line 97

def args(args = nil)
  if args
    @args = Array(args)
    return
  end
  existing = begin
               superclass.args
             rescue StandardError
               []
             end
  (instance_variable_defined?(:@args) && @args) || existing
end

#cli_optionsObject

By default, all args are automatically built out as k/v CLI options. For more advanced behavior, override cli_options in the implementing class. The return of this method is passed directly to Dobby::Options#options



113
114
115
# File 'lib/dobby/strategy.rb', line 113

def cli_options
  args.map { |arg| "--#{arg.to_s.tr('_', '-')} VALUE" }
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 Dobby::Strategy

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

Using a hash to configure the default options.


class MyStrategy
  include Dobby::Strategy
  configure foo: 'bar'

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.



67
68
69
70
71
72
73
# File 'lib/dobby/strategy.rb', line 67

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

#default_optionsOptions

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

Returns:



35
36
37
38
39
40
41
42
# File 'lib/dobby/strategy.rb', line 35

def default_options
  existing = begin
               superclass.default_options
             rescue StandardError
               {}
             end
  @default_options ||= Dobby::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 Dobby::Strategy

  option :foo, 'bar'
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.



89
90
91
# File 'lib/dobby/strategy.rb', line 89

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