Class: Mayak::Predicates::Rule

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Helpers, T::Sig
Defined in:
lib/mayak/predicates/rule.rb

Constant Summary collapse

A =
type_member

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Rule

Returns a new instance of Rule.



17
18
19
# File 'lib/mayak/predicates/rule.rb', line 17

def initialize(&blk)
  @fn = T.let(blk, T.proc.params(arg0: A).returns(T::Boolean))
end

Instance Attribute Details

#fnObject (readonly)

Returns the value of attribute fn.



14
15
16
# File 'lib/mayak/predicates/rule.rb', line 14

def fn
  @fn
end

Instance Method Details

#any(another) ⇒ Object Also known as: |



35
36
37
38
39
# File 'lib/mayak/predicates/rule.rb', line 35

def any(another)
  Mayak::Predicates::Rule[A].new do |object|
    fn.call(object) || another.call(object)
  end
end

#both(another) ⇒ Object Also known as: &



27
28
29
30
31
# File 'lib/mayak/predicates/rule.rb', line 27

def both(another)
  Mayak::Predicates::Rule[A].new do |object|
    fn.call(object) && another.call(object)
  end
end

#call(object) ⇒ Object



22
23
24
# File 'lib/mayak/predicates/rule.rb', line 22

def call(object)
  fn.call(object)
end

#from(function) ⇒ Object



55
56
57
58
59
# File 'lib/mayak/predicates/rule.rb', line 55

def from(function)
  Mayak::Predicates::Rule.new do |object|
    fn.call(function.call(object))
  end
end

#negateObject Also known as: !



43
44
45
46
47
# File 'lib/mayak/predicates/rule.rb', line 43

def negate
  Mayak::Predicates::Rule[A].new do |object|
    !fn.call(object)
  end
end

#presenceObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/mayak/predicates/rule.rb', line 62

def presence
  Mayak::Predicates::Rule[T.nilable(A)].new do |object|
    case object
    when nil
      false
    else
      fn.call(object)
    end
  end
end