Class: Oedipus::Comparison
- Inherits:
-
Object
- Object
- Oedipus::Comparison
- Defined in:
- lib/oedipus/comparison.rb,
lib/oedipus/comparison/shortcuts.rb
Overview
Represents a comparison operator and value.
Defined Under Namespace
Modules: Shortcuts Classes: Between, Equal, GT, GTE, In, LT, LTE, Not, NotEqual, NotIn, Outside
Instance Attribute Summary collapse
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Class Method Summary collapse
-
.of(v) ⇒ Object
Return a suitable comparison object for
v
.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare two comparisons for equality.
-
#initialize(v) ⇒ Comparison
constructor
Initialize a new Comparison for
v
. -
#inverse ⇒ Comparison
Return the exact inverse of this comparison.
-
#to_sql ⇒ Array
Represent the comparison as SQL arguments.
Constructor Details
#initialize(v) ⇒ Comparison
Initialize a new Comparison for v
.
55 56 57 |
# File 'lib/oedipus/comparison.rb', line 55 def initialize(v) @v = v end |
Instance Attribute Details
#v ⇒ Object (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
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.
66 67 68 |
# File 'lib/oedipus/comparison.rb', line 66 def ==(other) other.class == self.class && other.v == v end |
#inverse ⇒ Comparison
Return the exact inverse of this comparison.
76 77 78 |
# File 'lib/oedipus/comparison.rb', line 76 def inverse raise NotImplementedError, "Comparison#inverse must be defined by subclasses" end |
#to_sql ⇒ Array
Represent the comparison as SQL arguments.
84 85 86 |
# File 'lib/oedipus/comparison.rb', line 84 def to_sql raise NotImplementedError, "Comparison#to_sql must be defined by subclasses" end |