Sha256: c830e62a80c7892bf00832c99f8ccf0604a7e3876d49fe495e6340fb7849402b

Contents?: true

Size: 958 Bytes

Versions: 3

Compression:

Stored size: 958 Bytes

Contents

require 'sqlpostgres/PgType'

module SqlPostgres

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

  class PgPoint < PgType

    # Return the x coordinate

    attr_reader :x

    # Return the y coordinate

    attr_reader :y

    class << self

      # Create a PgPoint from a string in Postgres format (ie
      # "(1,2)").

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

    end

    # Constructor taking the x and y coordinate

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

    # Return a string representation (ie "(1, 2)").

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

    protected

    def parts
      [x, y]
    end

    private

    def column_type
      'point'
    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/PgPoint.rb
sqlpostgres-1.2.5 lib/sqlpostgres/PgPoint.rb
sqlpostgres-1.2.4 lib/sqlpostgres/PgPoint.rb