Class: Boundy::Bound::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/boundy/bound/comparator.rb

Constant Summary collapse

Bound =
Boundy::Bound
COMPARISONS =
{
  Bound::Infinite::Above => {
    Bound::Infinite::Above => :equal,
    Bound::Infinite::Below => :left,
    Bound => :left
  },
  Bound::Infinite::Below => {
    Bound::Infinite::Above => :right,
    Bound::Infinite::Below => :equal,
    Bound => :right
  },
  Bound => {
    Bound::Infinite::Above => :right,
    Bound::Infinite::Below => :left 
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Comparator

Returns a new instance of Comparator.



7
8
9
10
# File 'lib/boundy/bound/comparator.rb', line 7

def initialize(a, b)
  @a = a
  @b = b
end

Instance Method Details

#after?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/boundy/bound/comparator.rb', line 35

def after?
  compare == :right
end

#before?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/boundy/bound/comparator.rb', line 31

def before?
  compare == :left 
end

#compareObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/boundy/bound/comparator.rb', line 43

def compare
  result = nil
  if COMPARISONS.has_key?(@a.class)
    part_comp = COMPARISONS[@a.class]
    if part_comp.has_key?(@b.class)
      result = part_comp[@b.class]
    end
  end

  map = {
    -1 => :right,
    0 => :equal,
    1 => :left
  }

  if result.nil?
    map[@a.datum <=> @b.datum]
  else
    result
  end

end

#within?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/boundy/bound/comparator.rb', line 39

def within?
  compare == :equal
end