Class: Factbase::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/term.rb

Overview

Term.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, operands) ⇒ Term

Ctor.

Parameters:

  • operator (Symbol)

    Operator

  • operands (Array)

    Operands



36
37
38
39
# File 'lib/factbase/term.rb', line 36

def initialize(operator, operands)
  @op = operator
  @operands = operands
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



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

def op
  @op
end

#operandsObject (readonly)

Returns the value of attribute operands.



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

def operands
  @operands
end

Instance Method Details

#matches?(fact) ⇒ bool

Does it match the fact?

Parameters:

Returns:

  • (bool)

    TRUE if matches



44
45
46
# File 'lib/factbase/term.rb', line 44

def matches?(fact)
  send(@op, fact)
end

#to_sString

Turns it into a string.

Returns:

  • (String)

    The string of it



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/factbase/term.rb', line 50

def to_s
  items = []
  items << @op
  items += @operands.map do |o|
    if o.is_a?(String)
      "'#{o.gsub("'", "\\\\'").gsub('"', '\\\\"')}'"
    elsif o.is_a?(Time)
      o.utc.iso8601
    else
      o.to_s
    end
  end
  "(#{items.join(' ')})"
end