Sha256: 9783aac530df50d5ab7d4111dcd54df97f3146e9d8479a91a68ae3322dc46e70

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require_relative 'sort_value_validator'

module Might
  # Sorting parameter definition
  #
  class SortParameterDefinition
    # @return [String]
    attr_reader :name

    # If the property name doesn't match the name in the query string, use the :as option
    # @return [String]
    attr_reader :as

    # @return [Boolean]
    attr_reader :reverse_direction

    # @param [String] name of the field
    # @param [String] as (#name) alias for the property
    # @param [Boolean] reverse_direction (false) default sorting direction
    #
    def initialize(name, as: name, reverse_direction: false)
      @name = name.to_s
      @as = as.to_s
      @reverse_direction = reverse_direction
    end

    # If two parameters have the same name, they are equal.
    delegate :hash, to: :name

    def eql?(other)
      other.is_a?(self.class) && other.name == name
    end

    def ==(other)
      other.is_a?(self.class) &&
        other.name == name &&
        other.as == as
    end

    alias reverse_direction? reverse_direction

    def validator
      SortValueValidator.build(self).new
    end

    def defined?
      true
    end

    def undefined?
      !self.defined? # rubocop:disable Style/RedundantSelf
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
might-0.6.0 lib/might/sort_parameter_definition.rb
might-0.5.2 lib/might/sort_parameter_definition.rb