Sha256: 54ff7093ab0e76c63c714e5335cdf1ca1976aefaaef9c57535108e2be1d1d8f7

Contents?: true

Size: 1.23 KB

Versions: 15

Compression:

Stored size: 1.23 KB

Contents

module Torque
  module PostgreSQL
    class Line < Struct.new(:slope, :intercept)
      alias c intercept
      alias c= intercept=

      def a=(value)
        self.slope = vertical? \
          ? Float::INFINITY \
          : Rational(value, b)
      end

      def a
        slope.numerator
      end

      def b=(value)
        self.slope = value.zero? \
          ? Float::INFINITY \
          : Rational(a, value)
      end

      def b
        vertical? ? 0 : slope.denominator
      end

      def horizontal?
        slope.zero?
      end

      def vertical?
        !slope.try(:infinite?).eql?(nil)
      end
    end

    config.geometry.line_class ||= ::ActiveRecord.const_set('Line', Class.new(Line))

    module Adapter
      module OID
        class Line < Torque::PostgreSQL::GeometryBuilder

          PIECES = %i[a b c].freeze
          FORMATION = '{%s,%s,%s}'.freeze

          protected

            def build_klass(*args)
              return nil if args.empty?
              check_invalid_format!(args)

              a, b, c = args.try(:first, pieces.size)&.map(&:to_f)
              slope = b.zero? ? Float::INFINITY : Rational(a, b)
              config_class.new(slope, c)
            end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
torque-postgresql-1.1.8 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.7 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.0.3 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.0.2 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.6 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.5 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.0.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.0.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.4 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.3 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.2 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.1.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.0.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-1.0.0 lib/torque/postgresql/adapter/oid/line.rb