Class: Bali::Judge

Inherits:
Object
  • Object
show all
Defined in:
lib/bali/judge.rb

Constant Summary collapse

FUZY_FALSE =

Fuzy value is possible when the evaluation is not yet clear cut, for example in this case:

role :finance do

cant :view

end

others do

can :view
can :index

end

Checking cant view for finance role results in a definite false, but checking on index for the same role results in FUZY_TRUE. Eventually, all FUZY value will be normal TRUE or FALSE if no definite counterpart is found.

-2
FUZY_TRUE =
2
DEFINITE_FALSE =
-1
DEFINITE_TRUE =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term:, actor:, role:, operation:, record:, should_cross_check: true) ⇒ Judge

Returns a new instance of Judge.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bali/judge.rb', line 64

def initialize(term:,
  actor:,
  role:,
  operation:,
  record:,
  should_cross_check: true)

  @term = term
  @role = role
  @actor = actor
  @operation = operation
  @record = record
  @should_cross_check = should_cross_check
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



22
23
24
# File 'lib/bali/judge.rb', line 22

def actor
  @actor
end

#operationObject

Returns the value of attribute operation.



22
23
24
# File 'lib/bali/judge.rb', line 22

def operation
  @operation
end

#recordObject

Returns the value of attribute record.



22
23
24
# File 'lib/bali/judge.rb', line 22

def record
  @record
end

#roleObject

Returns the value of attribute role.



22
23
24
# File 'lib/bali/judge.rb', line 22

def role
  @role
end

#should_cross_checkObject

Returns the value of attribute should_cross_check.



22
23
24
# File 'lib/bali/judge.rb', line 22

def should_cross_check
  @should_cross_check
end

#termObject

Returns the value of attribute term.



22
23
24
# File 'lib/bali/judge.rb', line 22

def term
  @term
end

Class Method Details

.check(term, actor_or_roles, operation, record) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bali/judge.rb', line 37

def check(term, actor_or_roles, operation, record)
  if operation.nil?
    # eg: user.can? :sign_in
    operation = actor_or_roles
    actor_or_roles = nil
  end

  judgement_value = default_value = default_judgement_value(term)
  roles = Bali::Role.formalize actor_or_roles

  roles.each do |role|
    judge = Bali::Judge.new(
      term: term,
      role: role,
      actor: actor_or_roles,
      operation: operation,
      record: record
    )

    judgement_value = judge.judgement
    break if judgement_value != default_value
  end

  judgement_value
end

.default_judgement_value(term) ⇒ Object



30
31
32
33
34
35
# File 'lib/bali/judge.rb', line 30

def default_judgement_value(term)
  case term
  when :can then false
  when :cant then true
  end
end

Instance Method Details

#judgementObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bali/judge.rb', line 79

def judgement
  judgement = natural_value if no_rule_group?

  if judgement.nil? && rule.nil?
    cross_check_value = nil
    # default if can? for undefined rule is false, after related clause
    # cant be found in cant?
    cross_check_value = cross_check_judge.judgement if should_cross_check

    # if cross check value nil, then the reverse rule is not defined,
    # let's determine whether they can do anything or not
    if cross_check_value.nil?
      judgement = deduce_from_defined_disposition
    else
      # process value from cross checking
      if otherly_rule && (cross_check_value == FUZY_FALSE || cross_check_value == FUZY_TRUE)
        # give chance to check at others block
        @rule = otherly_rule
      else
        judgement = reverse_value(cross_check_value)
      end
    end
  end

  judgement ||= deduce_by_evaluation ||
    deduce_from_fuzy_rules ||
    natural_value

  return !should_cross_check ?
    judgement :

    # translate response for value above to traditional true/false
    # holy judgement refer to non-standard true/false being used inside Bali
    # which need to be translated from other beings to know
    judgement > 0
end