Module: Factbase::Term::Math

Included in:
Factbase::Term
Defined in:
lib/factbase/terms/math.rb

Overview

Math terms.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#arithmetic(op, fact, maps) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/factbase/terms/math.rb', line 89

def arithmetic(op, fact, maps)
  assert_args(2)
  lefts = the_values(0, fact, maps)
  return nil if lefts.nil?
  raise 'Too many values at first position, one expected' unless lefts.size == 1
  rights = the_values(1, fact, maps)
  return nil if rights.nil?
  raise 'Too many values at second position, one expected' unless rights.size == 1
  v = lefts[0]
  r = rights[0]
  if v.is_a?(Time) && r.is_a?(String)
    (num, units) = r.split
    num = num.to_i
    r =
      case units
      when 'seconds', 'second'
        num
      when 'minutes', 'minute'
        num * 60
      when 'hours', 'hour'
        num * 60 * 60
      when 'days', 'day'
        num * 60 * 60 * 24
      when 'weeks', 'week'
        num * 60 * 60 * 24 * 7
      else
        raise "Unknown time unit '#{units}' in '#{r}"
      end
  end
  v.send(op, r)
end

#cmp(op, fact, maps) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/factbase/terms/math.rb', line 74

def cmp(op, fact, maps)
  assert_args(2)
  lefts = the_values(0, fact, maps)
  return false if lefts.nil?
  rights = the_values(1, fact, maps)
  return false if rights.nil?
  lefts.any? do |l|
    l = l.floor if l.is_a?(Time) && op == :==
    rights.any? do |r|
      r = r.floor if r.is_a?(Time) && op == :==
      l.send(op, r)
    end
  end
end

#div(fact, maps) ⇒ Object



43
44
45
# File 'lib/factbase/terms/math.rb', line 43

def div(fact, maps)
  arithmetic(:/, fact, maps)
end

#eq(fact, maps) ⇒ Object



54
55
56
# File 'lib/factbase/terms/math.rb', line 54

def eq(fact, maps)
  cmp(:==, fact, maps)
end

#gt(fact, maps) ⇒ Object



62
63
64
# File 'lib/factbase/terms/math.rb', line 62

def gt(fact, maps)
  cmp(:>, fact, maps)
end

#gte(fact, maps) ⇒ Object



70
71
72
# File 'lib/factbase/terms/math.rb', line 70

def gte(fact, maps)
  cmp(:>=, fact, maps)
end

#lt(fact, maps) ⇒ Object



58
59
60
# File 'lib/factbase/terms/math.rb', line 58

def lt(fact, maps)
  cmp(:<, fact, maps)
end

#lte(fact, maps) ⇒ Object



66
67
68
# File 'lib/factbase/terms/math.rb', line 66

def lte(fact, maps)
  cmp(:<=, fact, maps)
end

#minus(fact, maps) ⇒ Object



35
36
37
# File 'lib/factbase/terms/math.rb', line 35

def minus(fact, maps)
  arithmetic(:-, fact, maps)
end

#plus(fact, maps) ⇒ Object



31
32
33
# File 'lib/factbase/terms/math.rb', line 31

def plus(fact, maps)
  arithmetic(:+, fact, maps)
end

#times(fact, maps) ⇒ Object



39
40
41
# File 'lib/factbase/terms/math.rb', line 39

def times(fact, maps)
  arithmetic(:*, fact, maps)
end

#zero(fact, maps) ⇒ Object



47
48
49
50
51
52
# File 'lib/factbase/terms/math.rb', line 47

def zero(fact, maps)
  assert_args(1)
  vv = the_values(0, fact, maps)
  return false if vv.nil?
  vv.any? { |v| (v.is_a?(Integer) || v.is_a?(Float)) && v.zero? }
end