Class: Errapi::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/errapi/condition.rb

Direct Known Subclasses

ErrorCheck, SimpleCheck

Defined Under Namespace

Classes: ErrorCheck, SimpleCheck

Constant Summary collapse

ALLOWED_CONDITIONALS =
i(if unless).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conditional, predicate, options = {}) ⇒ Condition

Returns a new instance of Condition.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/errapi/condition.rb', line 10

def initialize conditional, predicate, options = {}

  @conditional = resolve_conditional conditional
  raise ArgumentError, "Conditional must be either :if or :unless" unless ALLOWED_CONDITIONALS.include? @conditional

  @predicate = predicate
end

Class Method Details

.conditionalsObject

Raises:

  • (LoadError)


4
5
6
7
8
# File 'lib/errapi/condition.rb', line 4

def self.conditionals
  h = const_get('CONDITIONALS')
  raise LoadError, "The CONDITIONALS constant in class #{self} is of the wrong type (#{h.class}). Either make it a Hash or override #{self}.conditionals to return a list of symbols." unless h.kind_of? Hash
  h.keys
end

Instance Method Details

#check(predicate, value, context, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/errapi/condition.rb', line 28

def check predicate, value, context, options = {}
  raise NotImplementedError, "Subclasses should implement the #check method to check whether the value matches the predicate of the condition"
end

#fulfilled?(*args) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/errapi/condition.rb', line 18

def fulfilled? *args
  result = check @predicate, *args
  result = !result if @conditional == :unless
  result
end

#resolve_conditional(conditional) ⇒ Object



24
25
26
# File 'lib/errapi/condition.rb', line 24

def resolve_conditional conditional
  conditional
end