Sha256: fd10c8636c9e02e9d83e3cd9ce2f3e218363d45b8c8940128aa98e23c498a7d5

Contents?: true

Size: 877 Bytes

Versions: 1

Compression:

Stored size: 877 Bytes

Contents

module Priora
  class Priority
    DIRECTION_DESC = :desc
    DIRECTION_ASC = :asc

    DIRECTIONAL_METHODS = {
        DIRECTION_DESC => :+,
        DIRECTION_ASC => :-
    }.freeze

    attr_reader :attribute, :direction

    def initialize(attribute:, direction: DIRECTION_DESC)
      @attribute = attribute
      @direction = direction
    end

    def comparable_value_from(object)
      raw_value = object.send(@attribute)
      numeric_value = numeric_value_from(raw_value)
      directional_value_from(numeric_value)
    end

    private

    def numeric_value_from(raw_value)
      conversion_lambda = Priora.configuration.conversion_lambda_for(raw_value.class)
      conversion_lambda ? conversion_lambda.call(raw_value) : raw_value
    end

    def directional_value_from(numeric_value)
      0.send(DIRECTIONAL_METHODS[@direction], numeric_value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
priora-0.1.1 lib/priora/priority.rb