Class: Oedipus::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/oedipus/comparison.rb,
lib/oedipus/comparison/shortcuts.rb

Overview

Represents a comparison operator and value.

Direct Known Subclasses

Between, Equal, GT, GTE, In, LT, LTE, Not, NotEqual, NotIn, Outside

Defined Under Namespace

Modules: Shortcuts Classes: Between, Equal, GT, GTE, In, LT, LTE, Not, NotEqual, NotIn, Outside

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ Comparison

Initialize a new Comparison for v.

Parameters:

  • v (Object)

    any ruby object to compare



55
56
57
# File 'lib/oedipus/comparison.rb', line 55

def initialize(v)
  @v = v
end

Instance Attribute Details

#vObject (readonly)

Returns the value of attribute v.



49
50
51
# File 'lib/oedipus/comparison.rb', line 49

def v
  @v
end

Class Method Details

.of(v) ⇒ Object

Return a suitable comparison object for v.

The conversions are:

- leave Comparison objects unchanged
- convert real ranges to Between comparisons
- convert Infinity-bounded exclusive ranges to GT/LT comparisons
- convert Infinity-bounded inclusive ranges to GTE/LTE comparisons.
- convert everything else to an Equal comparison

Parameters:

  • v (Object)

    a ruby object to be compared

  • a (Comparison)

    comparison suitable for comparing the input



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oedipus/comparison.rb', line 29

def of(v)
  case v
  when Comparison
    v
  when Range
    if v.end == Float::INFINITY
      v.exclude_end? ? Shortcuts.gt(v.first) : Shortcuts.gte(v.first)
    elsif v.first == -Float::INFINITY
      v.exclude_end? ? Shortcuts.lt(v.end) : Shortcuts.lte(v.end)
    else
      Shortcuts.between(v)
    end
  when Enumerable
    Shortcuts.in(v)
  else
    Shortcuts.eq(v)
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare two comparisons for equality.

Parameters:

  • other (Comparison)

    another comparison to check

Returns:

  • (Boolean)

    true if the comparisons are the same



66
67
68
# File 'lib/oedipus/comparison.rb', line 66

def ==(other)
  other.class == self.class && other.v == v
end

#inverseComparison

Return the exact inverse of this comparison.

Returns:

  • (Comparison)

    the inverse of the current comparison

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/oedipus/comparison.rb', line 76

def inverse
  raise NotImplementedError, "Comparison#inverse must be defined by subclasses"
end

#to_sqlArray

Represent the comparison as SQL arguments.

Returns:

  • (Array)

    an SQL expression to compare a LHS against v

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/oedipus/comparison.rb', line 84

def to_sql
  raise NotImplementedError, "Comparison#to_sql must be defined by subclasses"
end