Module: WarningShot::Resolver::ClassMethods Private

Defined in:
lib/warningshot/resolver.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#add_dependency(type, req_name, dep_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

creates a list of gems that the resolver is dependent on for different features

The goal of this is that WarningShot will not need a bunch of libraries installed unless
the end users needs that specific functionality.  If a corelib/gem is missing the logger will
receive warnings if a particular functionality that needs that library is enabled and its missing
The missing GEMS can be installed with: warningshot --build-deps
All resolvers' dependencies can be viewed with: warningshot --list-deps

Parameters:

  • type (Symbol[:core|:gem])

    the type of library it depends on, core lib or gem

  • req_name (String)

    What would normally go in ‘require’

  • dep_opts (Hash) (defaults to: {})

    :disable [Boolean] Default: true

    Should the resolver be disabled if the gem is missing
    

    :unregister [Array]

    Test / Resolutions to unregister
    

    :name [String]

    Alternate name to use to install missing gem with warningshot --build-deps
    Example: require 'net/scp' would need gem install net-scp
    

    :version [String]

    The version to install
    

    :source [String]

    Alternate gem source
    


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/warningshot/resolver.rb', line 42

def add_dependency(type,req_name,dep_opts={})
  require req_name
  dep_opts[:installed] = true
rescue LoadError => ex
  self.disable! unless dep_opts[:disable] === false
  dep_opts[:installed] = false        
ensure
  @dependent_libs ||= {:core => [], :gem => []}

  #if an alternate name isn't specified refer to it by the req_name
  dep_opts[:name] ||= req_name
  @dependent_libs[type].push dep_opts
end

#after(type, &block) ⇒ Object

add after filters to test/resolution blocks

Parameters:

  • type

    Symbol run after :test | :resolution

  • block (lambda)

    block to run after tests or resolutions



420
421
422
423
# File 'lib/warningshot/resolver.rb', line 420

def after(type,&block)
  @after_filters ||= {:test=>[],:resolution=>[]}
  @after_filters[type] << block
end

#after_filters(type) ⇒ Array[Proc]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

gets after filters for type

Parameters:

  • type (Symbol)

    Type of filters to get

Returns:

  • (Array[Proc])

    after filters



408
409
410
# File 'lib/warningshot/resolver.rb', line 408

def after_filters(type)
  @after_filters ? @after_filters[type] : []
end

#before(type, &block) ⇒ Object

add before filters to test/resolution blocks

Parameters:

  • type

    Symbol run before :test | :resolution

  • block (lambda)

    block to run before tests or resolutions



385
386
387
388
# File 'lib/warningshot/resolver.rb', line 385

def before(type,&block)
  @before_filters ||= {:test=>[],:resolution=>[]}
  @before_filters[type] << block
end

#before_filters(type) ⇒ Array[Proc]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

gets before filters for type

Parameters:

  • type (Symbol)

    Type of filters to get

Returns:

  • (Array[Proc])

    Before filters



397
398
399
# File 'lib/warningshot/resolver.rb', line 397

def before_filters(type)
  @before_filters ? @before_filters[type] : []
end

#branch(b = nil) ⇒ String

Setter/Getter for resolver branch, used in outputting info and for determine which yaml to apply

Examples:

class MyResolver
  include WarningShot::Resolver
  branch :mock
end

Parameters:

  • b (~to_s) (defaults to: nil)

    branch in dependency tree to use

Returns:

  • (String)

    Resolver’s branch



105
106
107
108
# File 'lib/warningshot/resolver.rb', line 105

def branch(b=nil)
  @branch = b unless b.nil?
  @branch
end

#cli(*opts, &block) ⇒ Object

provides shortcut to WarningShot::Config.cli

Parameters:

  • *opts (Array)
  • &block (Proc)

See Also:



83
84
85
86
87
# File 'lib/warningshot/resolver.rb', line 83

def cli(*opts,&block)
  return if self.disabled?
  
  WarningShot::Config.cli(*opts, &block)
end

#depends_onHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

list the gems the resolver relies on

Returns:



59
60
61
# File 'lib/warningshot/resolver.rb', line 59

def depends_on
  @dependent_libs ||= {:core => [], :gem => []}
end

#description(d = nil) ⇒ String

Setter/Getter for resolver description,

Resolver’s description

Examples:

class MyResolver
  include WarningShot::Resolver
  description 'Mock resolver for rspec testing'
end

Parameters:

  • d (~to_s) (defaults to: nil)

Returns:

  • (String)

    Resolver’s description



125
126
127
128
# File 'lib/warningshot/resolver.rb', line 125

def description(d=nil)
  @description = d unless d.nil?
  @description
end

#detailsArray(Object)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Outputs class static details

Returns:



429
430
431
# File 'lib/warningshot/resolver.rb', line 429

def details
  [self.name,self.description,self.order,self.disabled?]
end

#disable!Object

Sets a resolver to disabled mode, won’t be processed

Examples:

class MyResolver
  include WarningShot::Resolver
  disable!
end


161
162
163
# File 'lib/warningshot/resolver.rb', line 161

def disable!
  @disabled = true
end

#disabled?Boolean

Determines if resolver is disabled

Returns:

  • (Boolean)

    is it disabled



188
189
190
# File 'lib/warningshot/resolver.rb', line 188

def disabled?
  @disabled ||=false
end

#enable!Object

enables a resolver (enabled by default)

Examples:

class MyResolver
  include WarningShot::Resolver
  disable!
end

# Maybe you only want to enable it under some conditions
MyResolver.enabled! if my_merb_or_rails_env == 'production'
MyResolver.enabled! if @pickles.count == @chickens.count


178
179
180
# File 'lib/warningshot/resolver.rb', line 178

def enable!
  @disabled = false
end

#flush!Object

removes all test/resolutions from a resolver



358
359
360
361
# File 'lib/warningshot/resolver.rb', line 358

def flush!
  flush_tests!
  flush_resolutions!
end

#flush_resolutions!Object

Removes all resolutions from a resolver



373
374
375
# File 'lib/warningshot/resolver.rb', line 373

def flush_resolutions!
  @registered_blocks[:resolution] = []
end

#flush_tests!Object

Removes all tests from a resolver



366
367
368
# File 'lib/warningshot/resolver.rb', line 366

def flush_tests!
  @registered_blocks[:test] = []
end

#logger~Logger

Provides resolver access to logger

Returns:

  • (~Logger)

    WarningShots logger



198
199
200
# File 'lib/warningshot/resolver.rb', line 198

def logger
  @logger || Logger.new(STDOUT)
end

#logger=(log) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets



203
204
205
# File 'lib/warningshot/resolver.rb', line 203

def logger=(log)
  @logger = log
end

#optionsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

provides shortcut to WarningShot::Config.cli_options

Returns:

See Also:



69
70
71
# File 'lib/warningshot/resolver.rb', line 69

def options
  WarningShot::Config.cli_options
end

#order(p = nil) ⇒ Fixnum

Setter/Getter for resolver order, determines order in which resolver is checked

Examples:

class MyResolver
  include WarningShot::Resolver
  order 1 # Would attempt to run this resolver check first
  #order 1000
end

Parameters:

  • p (Fixnum) (defaults to: nil)

    (optional; default 100) Resolver’s order, lower the earlier it is run

Returns:

  • (Fixnum)

    Resolver’s order



147
148
149
150
# File 'lib/warningshot/resolver.rb', line 147

def order(p=nil)
  @order = p unless p.nil?
  @order || 100
end

#register(type, meta = {}, &block) ⇒ Object

register :resolution do |dependency|

  #Access to current dependency via dependency
  my_method_that_would_resolve dependency
end

register :resolution, :if => lambda{|dependency| 
  #This will determin if resolution should be attempted
  my_special_instance == true
} do |dependency|
    my_method_that_would_resolve dependency
end


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/warningshot/resolver.rb', line 300

def register(type, meta={}, &block)
  if meta[:if] && meta[:unless]
    raise Exception, ":if and :unless cannot be specified on the same resolution"
  end
  
  @registered_blocks  ||= {:test => [], :resolution => []}
  meta[type] = block

  # If a condition is given add to begining of array, if no condition
  #   add it to end.  This makes it so we dont have to sort later on :if|:unless
  #   to get non-condition resolutions to run last
  
  if meta[:if] || meta[:unless]
    @registered_blocks[type].unshift meta
  else
    @registered_blocks[type] << meta
  end
end

#resolutions(resolution_name = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lists current resolver resolutions

Parameters:

  • resolution_name (Symbol) (defaults to: nil)

    find a test by name (if a name was given)



345
346
347
348
349
350
351
352
353
# File 'lib/warningshot/resolver.rb', line 345

def resolutions(resolution_name=nil)
  unless resolution_name
    @registered_blocks[:resolution] ||= []
  else
    return @registered_blocks[:resolution].find do |registered_resolution|
      registered_resolution[:name] == resolution_name
    end
  end
end

#tests(test_name = nil) ⇒ Hash|Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lists current resolver tests

Parameters:

  • test_name (Symbol) (defaults to: nil)

    find a test by name (if a name was given)

Returns:

  • (Hash|Array)

    When name given, returns test Hash When name not give, returns all test Hashes in an array



329
330
331
332
333
334
335
336
337
# File 'lib/warningshot/resolver.rb', line 329

def tests(test_name=nil)
  unless test_name
    @registered_blocks[:test] ||= []
  else
    return @registered_blocks[:test].find do |registered_test|
      registered_test[:name] == test_name
    end
  end
end

#typecast(klass = nil, &block) ⇒ Object

Defines how to cast YAML parsed data to an object

the resolver can work with

Parameters:

  • klass (Class) (defaults to: nil)

    Type of class to perform casting against,

    if nil, the method is used on all types not matched
    
  • block (lambda)

    How to cast the data to an object



218
219
220
221
222
223
224
225
# File 'lib/warningshot/resolver.rb', line 218

def typecast(klass=nil,&block)
  if klass.nil?
    klass = :default
  else
    klass = klass.name.to_sym
  end
  (@yaml_to_object_methods||={})[klass] = block
end

#yaml_to_object(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

calls the block defined by Resolver#typecast to convert

the yaml data to an object

Parameters:

  • data (~YAML::load)

    The data parsed from YAML::load

Returns:

  • (Object)

    The casted objects



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/warningshot/resolver.rb', line 241

def yaml_to_object(data)       
  @yaml_to_object_methods||={} 
  klass = data.class.name.to_sym

  # if klass has a registered casting method, do it
  if @yaml_to_object_methods.key? klass
    return @yaml_to_object_methods[klass].call(data)
  # elsif there is a regsitered default method, do it.
  elsif @yaml_to_object_methods.key? :default
    return @yaml_to_object_methods[:default].call(data)
  else
  # else return original data
    return data
  end
end