Class: SqlPostgres::PgPoint

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

Overview

This class holds the value of a “point” column.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash, #to_sql

Constructor Details

#initialize(x = 0, y = 0) ⇒ PgPoint

Constructor taking the x and y coordinate



34
35
36
37
# File 'lib/sqlpostgres/PgPoint.rb', line 34

def initialize(x = 0, y = 0)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

Return the x coordinate



11
12
13
# File 'lib/sqlpostgres/PgPoint.rb', line 11

def x
  @x
end

#yObject (readonly)

Return the y coordinate



15
16
17
# File 'lib/sqlpostgres/PgPoint.rb', line 15

def y
  @y
end

Class Method Details

.from_sql(s) ⇒ Object

Create a PgPoint from a string in Postgres format (ie “(1,2)”).



22
23
24
25
26
27
28
# File 'lib/sqlpostgres/PgPoint.rb', line 22

def from_sql(s)
  if s =~ /^\((.*?),(.*\))$/
    PgPoint.new($1.to_f, $2.to_f)
  else
    raise ArgumentError, "Invalid point: #{s.inspect}"
  end
end

Instance Method Details

#to_sObject

Return a string representation (ie “(1, 2)”).



41
42
43
# File 'lib/sqlpostgres/PgPoint.rb', line 41

def to_s
  "(%g, %g)" % parts
end