Class: Sqreen::CB

Inherits:
Object
  • Object
show all
Defined in:
lib/sqreen/cb.rb

Constant Summary collapse

DEFAULT_PRIORITY =

Callback class.

Three methods can be defined:

  • pre(*args, &block) To be called prior to the hooked method.

  • post(return_value, *args, &block) To be called after the hooked method. The return_value argument is the value returned by the hooked method.

  • failing(exception, …) To be called when the method raise

The method pre, post and exception may return nil or a Hash.

- nil: the original method is called and the callback has no further
  effect
- { :status => :skip }: we skip the original method call
- { :status => :raise}:

- nil: the original return value is returned, as if coallback had no
  effect
- { :status => :raise}:
- { :status => :override }:

- nil: reraise
- { :status => :reraise }: reraise
- { :status => :override }: eat exception
- { :retry => :retry }: try the block again

CB can also declare that they are whitelisted and should not be run at the moment.
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method) ⇒ CB

Returns a new instance of CB.

Raises:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sqreen/cb.rb', line 44

def initialize(klass, method)
  @method = method
  @klass = klass

  @has_pre  = respond_to? :pre
  @has_post = respond_to? :post
  @has_failing = respond_to? :failing
  @overtimeable = false

  raise(Sqreen::Exception, 'No callback provided') unless @has_pre || @has_post || @has_failing
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



41
42
43
# File 'lib/sqreen/cb.rb', line 41

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



41
42
43
# File 'lib/sqreen/cb.rb', line 41

def method
  @method
end

#overtimeableObject (readonly)

Returns the value of attribute overtimeable.



42
43
44
# File 'lib/sqreen/cb.rb', line 42

def overtimeable
  @overtimeable
end

Instance Method Details

#failing?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/sqreen/cb.rb', line 73

def failing?
  @has_failing
end

#frameworkObject



86
87
88
# File 'lib/sqreen/cb.rb', line 86

def framework
  nil
end

#overtime!Object



81
82
83
84
# File 'lib/sqreen/cb.rb', line 81

def overtime!
  Sqreen.log.debug { "#{self} is overtime!" }
  @overtimeable
end

#post?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/sqreen/cb.rb', line 69

def post?
  @has_post
end

#pre?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sqreen/cb.rb', line 65

def pre?
  @has_pre
end

#priorityObject

the lower, the closer to the beginning of the list



61
62
63
# File 'lib/sqreen/cb.rb', line 61

def priority
  DEFAULT_PRIORITY
end

#to_sObject



77
78
79
# File 'lib/sqreen/cb.rb', line 77

def to_s
  format('#<%s: %s.%s>', self.class, @klass, @method)
end

#whitelisted?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sqreen/cb.rb', line 56

def whitelisted?
  false
end