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.

Parameters:

  • condition (#===)


53
54
55
# File 'lib/fear/partial_function/guard.rb', line 53

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:

  • conditions (<#===>)

Returns:



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

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.inject(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.



24
25
26
# File 'lib/fear/partial_function/guard.rb', line 24

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.



16
17
18
# File 'lib/fear/partial_function/guard.rb', line 16

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.



20
21
22
# File 'lib/fear/partial_function/guard.rb', line 20

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:

  • conditions (<#===>)

Returns:



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

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

  head, *tail = conditions
  tail.inject(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:

  • arg (any)

Returns:

  • (Boolean)


73
74
75
# File 'lib/fear/partial_function/guard.rb', line 73

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.



61
62
63
# File 'lib/fear/partial_function/guard.rb', line 61

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.



67
68
69
# File 'lib/fear/partial_function/guard.rb', line 67

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