Sha256: e66cb24b028a8f43461da2a4b7d353e0cb90ad2bb71d468e706cbe46afbc30d4

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module JSONAPIonify::Api
  class SortField
    delegate :to_s, :inspect, to: :to_hash
    attr_reader :name, :order

    def initialize(name)
      @str = name.to_s
      if name.to_s.start_with? '-'
        @name  = name.to_s[1..-1].to_sym
        @order = :desc
      else
        @name  = name.to_sym
        @order = :asc
      end
      freeze
    end

    def ==(other)
      other.class == self.class &&
        other.name == self.name
    end

    def ===(other)
      other.class == self.class &&
        other.name == self.name
    end

    def id?
      name == :id
    end

    def contains_operator
      case @order
      when :asc
        :>=
      when :desc
        :<=
      end
    end

    def contains_arel
      case @order
      when :asc
        :gteq
      when :desc
        :lteq
      end
    end

    def outside_operator
      case @order
      when :asc
        :>
      when :desc
        :<
      end
    end

    def outside_arel
      case @order
      when :asc
        :gt
      when :desc
        :lt
      end
    end

    def to_s
      @str
    end

    def to_hash
      { name => order }
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jsonapionify-0.11.0 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.10.2 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.10.1 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.10.0 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.9.3 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.9.2 lib/jsonapionify/api/sort_field.rb
jsonapionify-0.9.1 lib/jsonapionify/api/sort_field.rb