Sha256: 86d4e388cda1fe47240700928d5a0d6ad1d06972505f060d1c6ea59da2ef53c9

Contents?: true

Size: 697 Bytes

Versions: 7

Compression:

Stored size: 697 Bytes

Contents

module JSONAPIonify::Api
  class SortFieldSet
    include Enumerable
    delegate :[], :length, :to_s, :inspect, :each, to: :@list

    def initialize
      @list = []
      freeze
    end

    def to_hash
      map(&:to_hash).reduce(:merge)
    end

    def invert
      self.class.new.tap do |set|
        each do |field|
          name =
            case field.order
            when :asc
              "-#{field.name}"
            when :desc
              "#{field.name}"
            end
          set << SortField.new(name)
        end
      end
    end

    def <<(field)
      raise TypeError unless field.is_a? SortField
      @list << field unless @list.include? field
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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