Class: Himari::Decisions::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/himari/decisions/base.rb

Direct Known Subclasses

Authentication, Authorization, Claims

Defined Under Namespace

Classes: DecisionAlreadyMade, InvalidEffect

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/himari/decisions/base.rb', line 15

def initialize
  @rule_name = nil
  @effect = nil
  raise "#{self.class.name}.valid_effects is missing [BUG]" unless self.class.valid_effects
end

Instance Attribute Details

#effectObject (readonly)

Returns the value of attribute effect.



21
22
23
# File 'lib/himari/decisions/base.rb', line 21

def effect
  @effect
end

#effect_commentObject (readonly)

Returns the value of attribute effect_comment.



21
22
23
# File 'lib/himari/decisions/base.rb', line 21

def effect_comment
  @effect_comment
end

#effect_suggestionObject (readonly)

Returns the value of attribute effect_suggestion.



21
22
23
# File 'lib/himari/decisions/base.rb', line 21

def effect_suggestion
  @effect_suggestion
end

#effect_user_facing_messageObject (readonly)

Returns the value of attribute effect_user_facing_message.



21
22
23
# File 'lib/himari/decisions/base.rb', line 21

def effect_user_facing_message
  @effect_user_facing_message
end

#rule_nameObject (readonly)

Returns the value of attribute rule_name.



21
22
23
# File 'lib/himari/decisions/base.rb', line 21

def rule_name
  @rule_name
end

Class Method Details

.allow_effects(*effects) ⇒ Object



7
8
9
# File 'lib/himari/decisions/base.rb', line 7

def self.allow_effects(*effects)
  @valid_effects = effects
end

.valid_effectsObject



11
12
13
# File 'lib/himari/decisions/base.rb', line 11

def self.valid_effects
  @valid_effects
end

Instance Method Details

#allow!(*args, **kwargs) ⇒ Object



63
# File 'lib/himari/decisions/base.rb', line 63

def allow!(*args, **kwargs); decide!(:allow, *args, **kwargs); end

#as_logObject



38
39
40
# File 'lib/himari/decisions/base.rb', line 38

def as_log
  to_h
end

#continue!(*args, **kwargs) ⇒ Object



64
# File 'lib/himari/decisions/base.rb', line 64

def continue!(*args, **kwargs); decide!(:continue, *args, **kwargs); end

#decide!(effect, comment = "", user_facing_message: nil, suggest: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/himari/decisions/base.rb', line 52

def decide!(effect, comment = "", user_facing_message: nil, suggest: nil)
  raise DecisionAlreadyMade, "decision can only be made once per rule (#{rule_name})" if @effect
  raise InvalidEffect, "this effect is not valid under this rule. Valid effects: #{self.class.valid_effects.inspect} (#{rule_name})" unless self.class.valid_effects.include?(effect)
  raise InvalidEffect, "only deny effect can have suggestion" if suggest&& effect != :deny
  @effect = effect
  @effect_comment = comment
  @effect_user_facing_message = user_facing_message
  @effect_suggestion = suggest
  nil
end

#deny!(*args, **kwargs) ⇒ Object



65
# File 'lib/himari/decisions/base.rb', line 65

def deny!(*args, **kwargs); decide!(:deny, *args, **kwargs); end

#evolve(rule_name) ⇒ Object



42
43
44
# File 'lib/himari/decisions/base.rb', line 42

def evolve(rule_name)
  self.class.new(**to_evolve_args).set_rule_name(rule_name)
end

#set_rule_name(rule_name) ⇒ Object



46
47
48
49
50
# File 'lib/himari/decisions/base.rb', line 46

def set_rule_name(rule_name)
  raise "cannot override rule_name" if @rule_name
  @rule_name = rule_name
  self
end

#skip!(*args, **kwargs) ⇒ Object



66
# File 'lib/himari/decisions/base.rb', line 66

def skip!(*args, **kwargs); decide!(:skip, *args, **kwargs); end

#to_evolve_argsObject

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/himari/decisions/base.rb', line 23

def to_evolve_args
  raise NotImplementedError
end

#to_hObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/himari/decisions/base.rb', line 27

def to_h
  {
    rule_name: rule_name,
    effect: effect,
    effect_comment: effect_comment,
  }.tap do |x|
    x[:effect_user_facing_message] = effect_user_facing_message if effect_user_facing_message
    x[:effect_suggestion] = effect_suggestion if effect_suggestion
  end
end