Class: SqlPostgres::PgInterval

Inherits:
PgType
  • Object
show all
Defined in:
lib/sqlpostgres/PgInterval.rb

Overview

This class holds the value of an “interval” column. Instances are immutable.

The fields of a PgInterval are:

  • millennia (integer, default 0)

  • centuries (integer, default 0)

  • decades (integer, default 0)

  • years (integer, default 0)

  • months (integer, default 0)

  • weeks (integer, default 0)

  • days (integer, default 0)

  • hours (integer, default 0)

  • minutes (integer, default 0)

  • seconds (integer or float, default 0)

  • ago (boolean, default false)

These fields are set by passing has args to the constructor and can be retrieved using simple accessors:

** Example: interval

interval = PgInterval.new('hours'=>1, 'minutes'=>30)
p interval.hours                                      # 1
p interval.minutes                                    # 30
p interval.seconds                                    # 0

**

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash, #to_sql

Constructor Details

#initialize(args = {}) ⇒ PgInterval

Constructor.

args

The values of the days, minutes, and so on, as a hash. Each value is an Integer except for ago, which is a boolean.

The keys are:

  • millennia

  • centuries

  • decades

  • years

  • months

  • weeks

  • days

  • hours

  • minutes

  • seconds

Any integer not specified defaults to 0; any boolean not specified defaults to false.

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
# File 'lib/sqlpostgres/PgInterval.rb', line 118

def initialize(args = {})
  args = args.dup
  @values = Hash[*FIELDS.collect do |field|
      [field, args.delete(field) || default(field)]
    end.flatten]
  raise ArgumentError, args.inspect unless args.empty?
end

Class Method Details

.from_sql(s) ⇒ Object

Convert a Postgres string (ie “2 days 12:00:00”) to a PgInterval instance.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sqlpostgres/PgInterval.rb', line 59

def from_sql(s)
  begin
    args = {}
    words = s.split
    raise ArgumentError if words.empty?
    while !words.empty?
      word = words.shift
      case word
      when /(-)?(\d{2}):(\d{2})(?::(\d{2}(\.\d+)?))?/
        sign = if $1 then -1 else +1 end
        args['hours'] = sign * $2.to_i
        args['minutes'] = sign * $3.to_i
        args['seconds'] = sign * $4.to_f
      when /\d+/
        n = word.to_i
        units = words.shift
        case units
        when 'day', 'days'
          args['days'] = n
        when 'mon', 'mons'
          args['months'] = n
        when 'year', 'years'
          args['years'] = n
        else
          raise ArgumentError
        end
      else
        raise ArgumentError
      end
    end
  rescue ArgumentError
    raise ArgumentError, "Syntax error in interval: #{s.inspect}"
  end
  PgInterval.new(args)
end

Instance Method Details

#to_sObject

Convert to Postgres format (ie “1 day 12:00:00”)



128
129
130
131
132
133
134
135
# File 'lib/sqlpostgres/PgInterval.rb', line 128

def to_s
  s = (to_s_pieces(DATE_FIELDS) + [to_s_time_piece]).compact.join(' ')
  if s == ""
    "0 days"
  else
    s
  end + (ago ? " ago" : "")
end