Class: Bali::Judger::Judge

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

Direct Known Subclasses

NegativeJudge, PositiveJudge

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unconstructable = true) ⇒ Judge

this class is abstract, shouldn’t be initialized



34
35
36
37
38
39
# File 'lib/bali/foundations/judger/judge.rb', line 34

def initialize(unconstructable = true)
  if unconstructable
    raise Bali::Error, "Bali::Judge::Judger is unconstructable, properly construct by using build to get a concrete class!"
  end
  self
end

Instance Attribute Details

#cross_checkingObject

determine if this judger should not call other judger



31
32
33
# File 'lib/bali/foundations/judger/judge.rb', line 31

def cross_checking
  @cross_checking
end

#operationObject

Returns the value of attribute operation.



26
27
28
# File 'lib/bali/foundations/judger/judge.rb', line 26

def operation
  @operation
end

#original_subtargetObject

Returns the value of attribute original_subtarget.



24
25
26
# File 'lib/bali/foundations/judger/judge.rb', line 24

def original_subtarget
  @original_subtarget
end

#recordObject

record can be the class, or an instance of a class



28
29
30
# File 'lib/bali/foundations/judger/judge.rb', line 28

def record
  @record
end

#subtargetObject

Returns the value of attribute subtarget.



25
26
27
# File 'lib/bali/foundations/judger/judge.rb', line 25

def subtarget
  @subtarget
end

Class Method Details

.build(auth_level, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bali/foundations/judger/judge.rb', line 41

def self.build(auth_level, options = {})
  judge = nil
  if auth_level == :can
    judge = Bali::Judger::PositiveJudge.new
  elsif auth_level == :cannot
    judge = Bali::Judger::NegativeJudge.new
  else
    raise Bali::Error, "Unable to find judge for `#{auth_level}` case"
  end

  judge.original_subtarget = options[:original_subtarget]
  judge.subtarget = options[:subtarget]
  judge.operation = options[:operation]
  judge.record = options[:record]
  judge.cross_checking = false

  judge
end

Instance Method Details

#clone(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bali/foundations/judger/judge.rb', line 60

def clone(options = {})
  if options[:reverse]
    new_judge = Bali::Judger::Judge.build(self.reverse_auth_level)
  else
    new_judge = Bali::Judger::Judge.build(self.auth_level)
  end

  new_judge.subtarget = subtarget
  new_judge.operation = operation
  new_judge.record = record
  new_judge.cross_checking = cross_checking
  new_judge.original_subtarget = original_subtarget

  new_judge
end

#judgement(options = {}) ⇒ Object

return either true or false options can specify if returning raw, by specifying holy: true



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/bali/foundations/judger/judge.rb', line 127

def judgement(options = {})
  # the divine judgement will come to thee, O Thou 
  # the doer of truth. return raw, untranslated to true/false.
  our_holy_judgement = nil

  # default of can? is false whenever RuleClass for that class is undefined
  # or RuleGroup for that subtarget is not defined
  if rule_group.nil?
    if other_rule_group.nil?
      # no more chance for checking
      our_holy_judgement = natural_value 
    end
  end

  if our_holy_judgement.nil? && need_to_check_for_intervention? 
    our_holy_judgement = check_intervention
  end

  if our_holy_judgement.nil? && 
      rule_group && rule_group.plant? &&
      rule.nil? && otherly_rule.nil?
    our_holy_judgement = natural_value
  end

  if our_holy_judgement.nil? && rule.nil?
    cross_check_value = nil
    # default if can? for undefined rule is false, after related clause
    # cannot be found in cannot?
    unless cross_checking
      reversed_self = self.clone reverse: true
      reversed_self.cross_checking = true
      cross_check_value = reversed_self.judgement holy: true
    end 

    # if cross check value nil, then the reverse rule is not defined,
    # let's determine whether he is zeus or plant
    if cross_check_value.nil?
      # rule_group can be nil for when user checking under undefined rule-group
      if rule_group
        if rule_group.plant?
          our_holy_judgement = plant_return_value
        end

        if rule_group.zeus?
          our_holy_judgement = zeus_return_value
        end 
      end # if rule_group exist
    else
      # process value from cross checking
      
      if can_use_otherly_rule?(cross_check_value, cross_checking)
        # give chance to check at others block
        self.rule = otherly_rule
      else
        our_holy_judgement = cross_check_reverse_value(cross_check_value)
      end
    end
  end # if our judgement nil and rule is nil

  # if our holy judgement is still nil, but rule is defined
  if our_holy_judgement.nil? && rule
    if rule.has_decider?
      our_holy_judgement = get_decider_result(rule, original_subtarget, record)
    else
      our_holy_judgement = default_positive_return_value
    end
  end

  # return fuzy if otherly rule defines contrary to this auth_level
  if our_holy_judgement.nil? && rule.nil? && (other_rule_group && other_rule_group.get_rule(reverse_auth_level, operation))
    if rule_group && (rule_group.zeus? || rule_group.plant?)
      # don't overwrite our holy judgement with fuzy value if rule group
      # zeus/plant, because zeus/plant is more definite than any fuzy values
      # eventhough the rule is abstractly defined
    else
      our_holy_judgement = default_negative_fuzy_return_value
    end
  end

  # if at this point still nil, well, 
  # return the natural value for this judge
  if our_holy_judgement.nil?
    if otherly_rule
      our_holy_judgement = BALI_FUZY_TRUE
    else
      our_holy_judgement = natural_value
    end
  end

  holy = !!options[:holy]
  return holy ? our_holy_judgement : translate_holy_judgement(our_holy_judgement)
end

#other_rule_groupObject



88
89
90
91
92
93
94
# File 'lib/bali/foundations/judger/judge.rb', line 88

def other_rule_group
  unless @other_rule_group_checked
    @other_rule_group = Bali::Integrator::RuleGroup.for(record_class, "__*__")
    @other_rule_group_checked = true
  end
  @other_rule_group
end

#otherly_ruleObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/bali/foundations/judger/judge.rb', line 114

def otherly_rule
  unless @otherly_rule_checked
    if other_rule_group
      # retrieve rule from others group
      @otherly_rule = other_rule_group.get_rule(auth_level, operation)
      @otherly_rule_checked = true
    end
  end
  @otherly_rule
end

#record_classObject



76
77
78
# File 'lib/bali/foundations/judger/judge.rb', line 76

def record_class
  record.is_a?(Class) ? record : record.class
end

#ruleObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bali/foundations/judger/judge.rb', line 96

def rule
  unless @rule_checked
    # rule group may be nil, for when user checking for undefined rule group
    if rule_group
      @rule = rule_group.get_rule(auth_level, operation)
    else
      self.rule = nil
    end
  end
  @rule
end

#rule=(the_rule) ⇒ Object



108
109
110
111
112
# File 'lib/bali/foundations/judger/judge.rb', line 108

def rule=(the_rule)
  @rule = the_rule
  @rule_checked = true
  @rule
end

#rule_groupObject



80
81
82
83
84
85
86
# File 'lib/bali/foundations/judger/judge.rb', line 80

def rule_group
  unless @rule_group_checked
    @rule_group = Bali::Integrator::RuleGroup.for(record_class, subtarget)
    @rule_group_checked = true
  end
  @rule_group
end