Sha256: e7f84360c5aa359f8b77c105ca6c51376b28fd928b7b6acdb8be918f75618681

Contents?: true

Size: 1.07 KB

Versions: 19

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Coercers

    # Provides the mechanism to convert a value into a
    # {Vedeu::Geometries::Position}.
    #
    # @api private
    #
    class Position < Vedeu::Coercers::Coercer

      # @macro raise_fatal
      # @return [NilClass|Vedeu::Geometries::Position]
      def coerce
        if value.nil? || coerced?
          value

        elsif tuple?
          klass.new(*value)

        elsif hash?(value)
          klass.new(value.fetch(:y, 1), value.fetch(:x, 1))

        elsif numeric?(value)
          klass.new(value, 1)

        else
          incoercible!

        end
      end

      private

      # @return [Class]
      def klass
        Vedeu::Geometries::Position
      end

      # @macro raise_fatal
      # @return [Boolean]
      def tuple?
        return false unless array?(value)
        return true  if value.size == 2

        raise Vedeu::Error::Fatal,
              "A '#{klass}' is made up of two elements. (Provided " \
              "#{value.size}.)"
      end

    end # Position

  end # Coercers

end # Vedeu

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
vedeu-0.8.32 lib/vedeu/coercers/position.rb
vedeu-0.8.31 lib/vedeu/coercers/position.rb
vedeu-0.8.30 lib/vedeu/coercers/position.rb
vedeu-0.8.29 lib/vedeu/coercers/position.rb
vedeu-0.8.28 lib/vedeu/coercers/position.rb
vedeu-0.8.27 lib/vedeu/coercers/position.rb
vedeu-0.8.26 lib/vedeu/coercers/position.rb
vedeu-0.8.25 lib/vedeu/coercers/position.rb
vedeu-0.8.24 lib/vedeu/coercers/position.rb
vedeu-0.8.23 lib/vedeu/coercers/position.rb
vedeu-0.8.22 lib/vedeu/coercers/position.rb
vedeu-0.8.21 lib/vedeu/coercers/position.rb
vedeu-0.8.20 lib/vedeu/coercers/position.rb
vedeu-0.8.19 lib/vedeu/coercers/position.rb
vedeu-0.8.18 lib/vedeu/coercers/position.rb
vedeu-0.8.17 lib/vedeu/coercers/position.rb
vedeu-0.8.16 lib/vedeu/coercers/position.rb
vedeu-0.8.15 lib/vedeu/coercers/position.rb
vedeu-0.8.14 lib/vedeu/coercers/position.rb