Class: SqlPostgres::PgPath

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash, #to_sql

Constructor Details

#initialize(closed = true, *points) ⇒ PgPath

Returns a new instance of PgPath.



30
31
32
33
# File 'lib/sqlpostgres/PgPath.rb', line 30

def initialize(closed = true, *points)
  @points = points
  @closed = closed
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



10
11
12
# File 'lib/sqlpostgres/PgPath.rb', line 10

def closed
  @closed
end

#pointsObject (readonly)

Returns the value of attribute points.



9
10
11
# File 'lib/sqlpostgres/PgPath.rb', line 9

def points
  @points
end

Class Method Details

.from_sql(s) ⇒ Object

Create a PgPath from a string in Postgres format



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sqlpostgres/PgPath.rb', line 16

def from_sql(s)
  if s =~ /^(\[)\(.*\)(,\(.*\))?\]$/ || s =~ /^(\()\(.*\)(,\(.*\))?\)$/
    closed = $1 == "("
    points = s.scan(/\([^(]*?\)/).collect do |t|
      PgPoint.from_sql(t)
    end
    PgPath.new(closed, *points)
  else
    raise ArgumentError, "Invalid path: #{s.inspect}"
  end
end

Instance Method Details

#to_sObject



35
36
37
38
39
40
41
42
# File 'lib/sqlpostgres/PgPath.rb', line 35

def to_s
  s = points.join(", ")
  if closed
    "(#{s})"
  else
    "[#{s}]"
  end
end