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

API:

  • private

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.

Parameters:

API:

  • private



51
52
53
# File 'lib/fear/partial_function/guard.rb', line 51

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.

Parameters:

Returns:

API:

  • private



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fear/partial_function/guard.rb', line 28

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.

API:

  • private



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

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.

API:

  • private



14
15
16
# File 'lib/fear/partial_function/guard.rb', line 14

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.

API:

  • private



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

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.

Parameters:

Returns:

API:

  • private



42
43
44
45
46
47
# File 'lib/fear/partial_function/guard.rb', line 42

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.

Parameters:

Returns:

API:

  • private



71
72
73
# File 'lib/fear/partial_function/guard.rb', line 71

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.

Parameters:

Returns:

API:

  • private



59
60
61
# File 'lib/fear/partial_function/guard.rb', line 59

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.

Parameters:

Returns:

API:

  • private



65
66
67
# File 'lib/fear/partial_function/guard.rb', line 65

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