Class: Informix::IntervalYTM

Inherits:
IntervalBase show all
Defined in:
lib/informix/interval.rb

Overview

The IntervalYTM class is an Interval class dedicated only to represent intervals in the scope YEAR TO MONTH.

Instance Attribute Summary collapse

Attributes inherited from IntervalBase

#val

Instance Method Summary collapse

Methods inherited from IntervalBase

#*, #+@, #-@, #/, #<=>, #to_a

Constructor Details

#initialize(val) ⇒ IntervalYTM

Creates an IntervalYTM object with val as value.

IntervalYTM.new(val)  =>  interval


106
107
108
109
110
111
112
113
114
115
# File 'lib/informix/interval.rb', line 106

def initialize(val)
  super
  @val = val.to_i
  @years, @months = @val.abs.divmod 12
  if @val < 0
    @years = -@years
    @months = -@months
  end
  @fields = [ @years, @months ]
end

Instance Attribute Details

#monthsObject (readonly)

Returns the value of attribute months.



101
102
103
# File 'lib/informix/interval.rb', line 101

def months
  @months
end

#yearsObject (readonly)

Returns the value of attribute years.



101
102
103
# File 'lib/informix/interval.rb', line 101

def years
  @years
end

Instance Method Details

#+(obj) ⇒ Object

interval + date => date

interval + datetime => datetime


119
120
121
122
123
124
125
126
# File 'lib/informix/interval.rb', line 119

def +(obj)
  case obj
  when Date, DateTime
    obj >> @val
  else
    super
  end
end

#to_monthsObject

Converts invl to months

invl.to_months  => numeric


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

def to_months; @val end

#to_sObject

Returns an ANSI SQL standards compliant string representation

invl.to_s   => string


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

def to_s; "%d-%02d" % [@years, @months.abs] end

#to_yearsObject

Converts a invl to years

invl.to_years  => numeric


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

def to_years; Rational === @val ? @val/12 : @val/12.0 end