Module: Kernel

Defined in:
lib/lab42/data_class/kernel.rb,
lib/lab42/data_class/constraints/kernel.rb

Constant Summary collapse

Constraint =
Lab42::DataClass::Constraint
Maker =

TODO: Move Maker to Lab42::DataClass:ConstraintMaker

Lab42::DataClass::Proxy::Constraints::Maker
Anything =
Constraint.new(name: "Anything", function: ->(_) { true })
Boolean =
Constraint.new(name: "Boolean", function: -> { [false, true].member?(_1) })

Instance Method Summary collapse

Instance Method Details

#All?(constraint = nil, &blk) ⇒ Boolean

Returns:



9
10
11
12
13
14
15
# File 'lib/lab42/data_class/constraints/kernel.rb', line 9

def All?(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> do
    _1.all?(&constraint)
  end
  Constraint.new(name: "All?(#{constraint})", function: f)
end

#Any?(constraint = nil, &blk) ⇒ Boolean

Returns:



17
18
19
20
21
22
23
# File 'lib/lab42/data_class/constraints/kernel.rb', line 17

def Any?(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> do
    _1.any?(&constraint)
  end
  Constraint.new(name: "Any?(#{constraint})", function: f)
end

#Choice(*constraints) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lab42/data_class/constraints/kernel.rb', line 25

def Choice(*constraints)
  constraints = constraints.map{ Maker.make_constraint _1 }
  f = ->(value) do
    constraints.any?{ _1.(value) }
  end
  Constraint.new(name: "Choice(#{constraints.join(', ')})", function: f)
end

#Contains(str) ⇒ Object



33
34
35
36
# File 'lib/lab42/data_class/constraints/kernel.rb', line 33

def Contains(str)
  f = -> { _1.include?(str) rescue false }
  Constraint.new(name: "Contains(#{str})", function: f)
end

#DataClass(*args, **kwds, &blk) ⇒ Object



4
5
6
7
# File 'lib/lab42/data_class/kernel.rb', line 4

def DataClass(*args, **kwds, &blk)
  proxy = Lab42::DataClass::Proxy.new(*args, **kwds, &blk)
  proxy.define_class!
end

#EndsWith(str) ⇒ Object



38
39
40
41
# File 'lib/lab42/data_class/constraints/kernel.rb', line 38

def EndsWith(str)
  f = -> { _1.end_with?(str) rescue false }
  Constraint.new(name: "EndsWith(#{str})", function: f)
end

#Lambda(arity) ⇒ Object



43
44
45
46
47
48
# File 'lib/lab42/data_class/constraints/kernel.rb', line 43

def Lambda(arity)
  f = -> do
    _1.arity == arity rescue false
  end
  Constraint.new(name: "Lambda(#{arity})", function: f)
end

#NilOr(constraint = nil, &blk) ⇒ Object



50
51
52
53
54
# File 'lib/lab42/data_class/constraints/kernel.rb', line 50

def NilOr(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> { _1.nil? || constraint.(_1) }
  Constraint.new(name: "NilOr(#{constraint})", function: f)
end

#Not(constraint = nil, &blk) ⇒ Object



56
57
58
59
60
# File 'lib/lab42/data_class/constraints/kernel.rb', line 56

def Not(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> { !constraint.(_1) }
  Constraint.new(name: "Not(#{constraint})", function: f)
end

#Pair(first, second) ⇒ Object



9
10
11
# File 'lib/lab42/data_class/kernel.rb', line 9

def Pair(first, second)
  Lab42::Pair.new(first, second)
end

#PairOf(fst, snd) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/lab42/data_class/constraints/kernel.rb', line 62

def PairOf(fst, snd)
  fst_constraint = Maker.make_constraint(fst)
  snd_constraint = Maker.make_constraint(snd)
  f = -> do
    Lab42::Pair === _1 && fst_constraint.(_1.first) && snd_constraint.(_1.second)
  end
  Constraint.new(name: "PairOf(#{fst_constraint}, #{snd_constraint})", function: f)
end

#StartsWith(str) ⇒ Object



71
72
73
74
# File 'lib/lab42/data_class/constraints/kernel.rb', line 71

def StartsWith(str)
  f = -> { _1.start_with?(str) rescue false }
  Constraint.new(name: "StartsWith(#{str})", function: f)
end

#Triple(first, second, third) ⇒ Object



13
14
15
# File 'lib/lab42/data_class/kernel.rb', line 13

def Triple(first, second, third)
  Lab42::Triple.new(first, second, third)
end

#TripleOf(fst, snd, trd) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/lab42/data_class/constraints/kernel.rb', line 76

def TripleOf(fst, snd, trd)
  fst_constraint = Maker.make_constraint(fst)
  snd_constraint = Maker.make_constraint(snd)
  trd_constraint = Maker.make_constraint(trd)
  f = -> do
    Lab42::Triple === _1 && fst_constraint.(_1.first) && snd_constraint.(_1.second) && trd_constraint.(_1.third)
  end
  Constraint.new(name: "TripleOf(#{fst_constraint}, #{snd_constraint}, #{trd_constraint})", function: f)
end