Class: Eymiha::Envelope

Inherits:
BaseEnvelope show all
Defined in:
lib/eymiha/util/envelope.rb

Overview

An Envelope is the minimum envelope that will completely contain a set of values. Values may be added to an instance, and it can return the number of values considered so far and its high and low boundaries.

Envelopes can be used to generate ranges, however the result may be of limited utility if the types of values being enveloped don’t play nicely with ranges.

Instance Attribute Summary

Attributes inherited from BaseEnvelope

#count

Instance Method Summary collapse

Methods inherited from BaseEnvelope

#raise_no_compare, #raise_no_envelope

Constructor Details

#initialize(value = nil) ⇒ Envelope

Creates and returns an instance. If an argument is given, it is passed to the set method to initialize the new instance.



54
55
56
57
# File 'lib/eymiha/util/envelope.rb', line 54

def initialize(value=nil)
  super()
  add(value) unless value == nil
end

Instance Method Details

#add(value) ⇒ Object

Adds a value to the instance. When

  • x is an Envelope, it is coalesced into the instance.

  • otherwise, the envelope is extened to contain the value.

  • if the value cannot be compared to the boundaries, an EnvelopeException is raised.

The modified instance is returned.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/eymiha/util/envelope.rb', line 70

def add(value)
  if value.kind_of? Envelope
    count = value.count
    if (count > 0)
      add value.high
      add value.low
      @count += (count-2)
    end
    self
  else
    begin
      @high = value if (@count == 0 || value > @high)
      @low = value if (@count == 0 || value < @low)
      @count += 1
      self
    rescue
      raise_no_compare value
    end
  end
end

#contains?(value) ⇒ Boolean Also known as: ===

Returns true if the instance completely contains the argument:

  • value is an Envelope, its high and low are contained.

  • otherwise, the value is contained.

  • if the value cannot be compared to the boundaries, an EnvelopeException is raised.

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/eymiha/util/envelope.rb', line 109

def contains?(value)
  if value.kind_of? Envelope
    (contains? value.high) && (contains? value.low)
  else
    begin
      (value >= low) && (value <= high)
    rescue
      raise_no_compare value
    end
  end
end

#highObject

Returns the high boundary of the instance.

  • if there are no boundaries, an EnvelopeException is raised.



93
94
95
96
# File 'lib/eymiha/util/envelope.rb', line 93

def high
  raise_no_envelope if @count == 0
  @high
end

#lowObject

Returns the low boundary of the instance.

  • if there are no boundaries, an EnvelopeException is raised.



100
101
102
103
# File 'lib/eymiha/util/envelope.rb', line 100

def low 
  raise_no_envelope if @count == 0
  @low
end

#to_rangeObject

Returns an inclusive range from the low to high boundaries



124
125
126
# File 'lib/eymiha/util/envelope.rb', line 124

def to_range
  low..high
end

#to_sObject

Returns a string representation of the instance.



60
61
62
63
# File 'lib/eymiha/util/envelope.rb', line 60

def to_s
  values = (count > 0)? "\n  high  #{high}\n  low   #{low}" : ""
  "Envelope: count #{count}#{values}"
end