Class: MonkeyLens::Policy::Waiver

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_lens/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: "*", target: "*", method: "*", reason:, expires_on: nil) ⇒ Waiver

Returns a new instance of Waiver.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/monkey_lens/policy.rb', line 11

def initialize(type: "*", target: "*", method: "*", reason:, expires_on: nil)
  raise ConfigurationError, "waiver reason is required" if reason.to_s.strip.empty?

  @type = type.to_s
  @target = target.to_s
  @method_pattern = method.to_s
  @reason = reason.to_s
  @expires_on = expires_on && Date.iso8601(expires_on.to_s)
rescue Date::Error
  raise ConfigurationError, "invalid waiver expiration #{expires_on.inspect}"
end

Instance Attribute Details

#expires_onObject (readonly)

Returns the value of attribute expires_on.



9
10
11
# File 'lib/monkey_lens/policy.rb', line 9

def expires_on
  @expires_on
end

#method_patternObject (readonly)

Returns the value of attribute method_pattern.



9
10
11
# File 'lib/monkey_lens/policy.rb', line 9

def method_pattern
  @method_pattern
end

#reasonObject (readonly)

Returns the value of attribute reason.



9
10
11
# File 'lib/monkey_lens/policy.rb', line 9

def reason
  @reason
end

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/monkey_lens/policy.rb', line 9

def target
  @target
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/monkey_lens/policy.rb', line 9

def type
  @type
end

Instance Method Details

#expired?(today: Date.today) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/monkey_lens/policy.rb', line 31

def expired?(today: Date.today)
  expires_on && expires_on < today
end

#match?(change, today: Date.today) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/monkey_lens/policy.rb', line 23

def match?(change, today: Date.today)
  return false if expired?(today:)

  matches?(type, change.type) &&
    matches?(target, change.target) &&
    matches?(method_pattern, change.method_id.to_s)
end

#to_hObject



35
36
37
38
39
40
41
42
43
# File 'lib/monkey_lens/policy.rb', line 35

def to_h
  {
    "type" => type,
    "target" => target,
    "method" => method_pattern,
    "reason" => reason,
    "expires_on" => expires_on&.iso8601
  }.compact
end