Class: Fear::PartialFunction::Guard Private

Inherits:
Object
  • Object
show all
Defined in:
lib/fear/partial_function/guard.rb,
lib/fear/partial_function/guard/or.rb,
lib/fear/partial_function/guard/and.rb,
lib/fear/partial_function/guard/and3.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Guard represents PartialFunction guardian

Direct Known Subclasses

And, And3, Or

Defined Under Namespace

Classes: And, And3, Or

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(condition) ⇒ Guard

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Guard


55
56
57
# File 'lib/fear/partial_function/guard.rb', line 55

def initialize(condition)
  @condition = condition
end

Class Method Details

.and(conditions) ⇒ Fear::PartialFunction::Guard

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fear/partial_function/guard.rb', line 32

def and(conditions)
  case conditions.size
  when 1 then and1(*conditions)
  when 2 then and2(*conditions)
  when 3 then and3(*conditions)
  when 0 then Any
  else
    head, *tail = conditions
    tail.reduce(new(head)) { |acc, condition| acc.and(new(condition)) }
  end
end

.and1(c) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


26
27
28
# File 'lib/fear/partial_function/guard.rb', line 26

def and1(c)
  c
end

.and2(c1, c2) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Optimized version for combination of two guardians Two guarding is a very common situation. For example checking for Some, and checking a value withing container.


18
19
20
# File 'lib/fear/partial_function/guard.rb', line 18

def and2(c1, c2)
  Guard::And.new(c1, c2)
end

.and3(c1, c2, c3) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


22
23
24
# File 'lib/fear/partial_function/guard.rb', line 22

def and3(c1, c2, c3)
  Guard::And3.new(c1, c2, c3)
end

.or(conditions) ⇒ Fear::PartialFunction::Guard

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


46
47
48
49
50
51
# File 'lib/fear/partial_function/guard.rb', line 46

def or(conditions)
  return Any if conditions.empty?

  head, *tail = conditions
  tail.reduce(new(head)) { |acc, condition| acc.or(new(condition)) }
end

Instance Method Details

#===(arg) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


75
76
77
# File 'lib/fear/partial_function/guard.rb', line 75

def ===(arg)
  condition === arg
end

#and(other) ⇒ Fear::PartialFunction::Guard

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


63
64
65
# File 'lib/fear/partial_function/guard.rb', line 63

def and(other)
  Guard::And.new(condition, other)
end

#or(other) ⇒ Fear::PartialFunction::Guard

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


69
70
71
# File 'lib/fear/partial_function/guard.rb', line 69

def or(other)
  Guard::Or.new(condition, other)
end