Sha256: 18a1a408e35192273209bfd8a5af9f53fea81bd7d12c2487ba78dae8d97cecf7

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'sqlpostgres/PgType'

module SqlPostgres

  # This class holds the value of a "path" column.

  class PgPath < PgType

    attr_reader :points
    attr_reader :closed

    class << self

      # Create a PgPath from a string in Postgres format

      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

    end

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

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

    protected

    def parts
      [closed, points]
    end

    private

    def column_type
      'path'
    end

  end

end

# Local Variables:
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sqlpostgres-1.2.6 lib/sqlpostgres/PgPath.rb
sqlpostgres-1.2.5 lib/sqlpostgres/PgPath.rb
sqlpostgres-1.2.4 lib/sqlpostgres/PgPath.rb