Sha256: e41409b8470a5c26f69daac25d30bfbcab09c6ba4469988dec41a64d48044446

Contents?: true

Size: 1.26 KB

Versions: 31

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

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

31 entries across 31 versions & 1 rubygems

Version Path
torque-postgresql-3.4.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.5 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.4.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.3.3 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.4 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.3.2 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.3 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.3.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.3.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.2.2 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.2 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.2.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.4.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.2.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.1.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.3.0 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.0.1 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-2.2.4 lib/torque/postgresql/adapter/oid/line.rb
torque-postgresql-3.0.0 lib/torque/postgresql/adapter/oid/line.rb