Module: Dobby::Strategy Abstract

Included in:
PackageSource::AbstractPackageSource, VulnSource::AbstractVulnSource
Defined in:
lib/dobby/strategy.rb

Overview

This module is abstract.

Include as a mixin to implement a Strategy compatible with the library

The Strategy provides base functionality for defining how Dobby takes in the data it needs to do its job. Each strategy should include this mixin to ensure compatibility with the rest of the library.

In particular, the mixin provides a DSL for configuring strategies and standardizes some strategy behavior (such as #inspect).

Much of this borrowed from Omniauth::Strategy https://github.com/omniauth/omniauth/blob/master/lib/omniauth/strategy.rb

Defined Under Namespace

Modules: ClassMethods Classes: Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



118
119
120
# File 'lib/dobby/strategy.rb', line 118

def options
  @options
end

Class Method Details

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/dobby/strategy.rb', line 19

def self.included(base)
  Dobby.strategies << base

  base.extend ClassMethods
  base.class_eval do
    option :setup, false
    option :test_mode, false
  end
end

Instance Method Details

#initialize(*args) {|Options| ... } ⇒ Object

Initialize the strategy, creating an [Options] hash if the last argument is a Hash.

Parameters:

  • args (Hash)

Yields:

Raises:

  • (ArgumentError)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dobby/strategy.rb', line 126

def initialize(*args)
  @options = self.class.default_options.dup
  options.deep_merge!(args.pop) if args.last.is_a?(Hash)
  options[:name] ||= self.class.to_s.split('::').last.downcase

  self.class.args.each do |arg|
    break if args.empty?

    options[arg] = args.shift
  end

  raise ArgumentError, "Received too many arguments. #{args.inspect}" unless args.empty?

  setup

  yield options if block_given?
end

#inspectString

Returns:

  • (String)


150
151
152
# File 'lib/dobby/strategy.rb', line 150

def inspect
  "#<#{self.class}>"
end

#log(level, message) ⇒ Object

Access to the Dobby logger, automatically prefixed with the strategy's name.

Examples:

log :fatal, 'This is a fatal error.'
log :error, 'This is an error.'
log :warn, 'This is a warning.'

Parameters:

  • level (Symbol)

    syslog level

  • message (String)


164
165
166
# File 'lib/dobby/strategy.rb', line 164

def log(level, message)
  Dobby.logger.send(level, "(#{self.class.name}) #{message}")
end

#setupObject

Callback placeholder so that 'super' during initialize is unnecessary in implementing classes. This is the other 10% of the use case for overriding initialize.



147
# File 'lib/dobby/strategy.rb', line 147

def setup; end