Class: Informix::IntervalBase

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/informix/interval.rb

Overview

The IntervalBase class is used for sending and retrieving INTERVAL values to/from Informix.

It can be used in some expressions with Numeric, Date, DateTime and Time.

Direct Known Subclasses

IntervalDTS, IntervalYTM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ IntervalBase

IntervalBase.new(val) => interval

Creates an Interval object with val as value.

Raises:

  • (TypeError)


44
45
46
47
# File 'lib/informix/interval.rb', line 44

def initialize(val)
  return @val = val if Numeric === val
  raise TypeError, "Expected Numeric" 
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



39
40
41
# File 'lib/informix/interval.rb', line 39

def val
  @val
end

Instance Method Details

#*(n) ⇒ Object

Multiplies an Interval object by a Numeric

interval*numeric  => interval

Raises:

  • (TypeError)


70
71
72
73
# File 'lib/informix/interval.rb', line 70

def *(n)
  return self.class.new(@val*n) if Numeric === n
  raise TypeError, "Expected Numeric"
end

#+(obj) ⇒ Object

Adds an Interval object to a Numeric or another compatible Interval

interval + numeric  => interval
interval + interval => interval


56
57
58
59
60
61
62
63
64
65
# File 'lib/informix/interval.rb', line 56

def +(obj)
  case obj
  when Numeric
    self.class.new(@val + obj)
  when self.class
    self.class.new(@val + obj.val)
  else
    raise TypeError, "#{self.class} cannot be added to #{obj.class}"
  end
end

#+@Object



49
# File 'lib/informix/interval.rb', line 49

def +@; self end

#-@Object



50
# File 'lib/informix/interval.rb', line 50

def -@; self.class.new(-@val) end

#/(n) ⇒ Object

Divides an Interval object by a Numeric

interval/numeric  => interval

Raises:

  • (TypeError)


78
79
80
81
# File 'lib/informix/interval.rb', line 78

def /(n)
  return self.class.new(@val/n) if Numeric === n
  raise TypeError, "Expected Numeric"
end

#<=>(ivl) ⇒ Object

Compares two compatible Interval objects.

interval1 <=> interval2  => true or false

Raises:

  • (ArgumentError)


86
87
88
89
# File 'lib/informix/interval.rb', line 86

def <=>(ivl)
  return @val <=> ivl.val if self.class === ivl
  raise ArgumentError, "Incompatible qualifiers"
end

#to_aObject

Returns the fields of an Interval object as an Array

invl.to_a   => array


94
# File 'lib/informix/interval.rb', line 94

def to_a; @fields end