Class: DeclarativePolicy::Rule::Or

Inherits:
Base
  • Object
show all
Defined in:
lib/declarative_policy/rule.rb

Overview

Logical `or`. Mirrors And.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#and, #inspect, make, #negate, #or

Constructor Details

#initialize(rules) ⇒ Or

Returns a new instance of Or.


232
233
234
# File 'lib/declarative_policy/rule.rb', line 232

def initialize(rules)
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules


231
232
233
# File 'lib/declarative_policy/rule.rb', line 231

def rules
  @rules
end

Instance Method Details

#cached_pass?(context) ⇒ Boolean

Returns:

  • (Boolean)

255
256
257
258
259
260
261
262
263
# File 'lib/declarative_policy/rule.rb', line 255

def cached_pass?(context)
  @rules.each do |rule|
    pass = rule.cached_pass?(context)

    return pass if pass.nil? || pass == true
  end

  false
end

#pass?(context) ⇒ Boolean

Returns:

  • (Boolean)

236
237
238
239
240
241
# File 'lib/declarative_policy/rule.rb', line 236

def pass?(context)
  cached = cached_pass?(context)
  return cached unless cached.nil?

  @rules.any? { |r| r.pass?(context) }
end

#reprObject


271
272
273
# File 'lib/declarative_policy/rule.rb', line 271

def repr
  "any?(#{@rules.map(&:repr).join(', ')})"
end

#score(context) ⇒ Object


265
266
267
268
269
# File 'lib/declarative_policy/rule.rb', line 265

def score(context)
  return 0 unless cached_pass?(context).nil?

  @rules.map { |r| r.score(context) }.inject(0, :+)
end

#simplifyObject


243
244
245
246
247
248
249
250
251
252
253
# File 'lib/declarative_policy/rule.rb', line 243

def simplify
  simplified_rules = @rules.flat_map do |rule|
    simplified = rule.simplify
    case simplified
    when Or then simplified.rules
    else [simplified]
    end
  end

  Or.new(simplified_rules)
end