Sha256: ea728e9d5b2960270c5a56ff23aee77f1e713ae80b3aa6d3009e3b872a6fb352

Contents?: true

Size: 1.15 KB

Versions: 29

Compression:

Stored size: 1.15 KB

Contents

module JsonApiClient
  class RequestParams
    attr_reader :klass, :includes, :fields

    def initialize(klass, includes: [], fields: {})
      @klass = klass
      @includes = includes
      @fields = fields
    end

    def add_includes(includes)
      Utils.parse_includes(klass, *includes).each do |name|
        name = name.to_sym
        self.includes.push(name) unless self.includes.include?(name)
      end
    end

    def reset_includes!
      @includes = []
    end

    def set_fields(type, field_names)
      self.fields[type.to_sym] = field_names.map(&:to_sym)
    end

    def remove_fields(type)
      self.fields.delete(type.to_sym)
    end

    def field_types
      self.fields.keys
    end

    def clear
      reset_includes!
      @fields = {}
    end

    def to_params
      return nil if field_types.empty? && includes.empty?
      parsed_fields.merge(parsed_includes)
    end

    private

    def parsed_includes
      return {} if includes.empty?
      {include: includes.join(",")}
    end

    def parsed_fields
      return {} if field_types.empty?
      {fields: fields.map { |type, names| [type, names.join(",")] }.to_h}
    end

  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
json_api_client-1.23.0 lib/json_api_client/request_params.rb
json_api_client-1.22.0 lib/json_api_client/request_params.rb
json_api_client-1.21.1 lib/json_api_client/request_params.rb
json_api_client-1.21.0 lib/json_api_client/request_params.rb
json_api_client-1.20.0 lib/json_api_client/request_params.rb
json_api_client-1.19.0 lib/json_api_client/request_params.rb
carwow-json_api_client-1.19.0 lib/json_api_client/request_params.rb
json_api_client-1.18.0 lib/json_api_client/request_params.rb
json_api_client-1.17.1 lib/json_api_client/request_params.rb
json_api_client-1.17.0 lib/json_api_client/request_params.rb
json_api_client-1.16.1 lib/json_api_client/request_params.rb
json_api_client-1.16.0 lib/json_api_client/request_params.rb
json_api_client-1.15.0 lib/json_api_client/request_params.rb
json_api_client-1.14.1 lib/json_api_client/request_params.rb
json_api_client-1.14.0 lib/json_api_client/request_params.rb
json_api_client-1.13.0 lib/json_api_client/request_params.rb
json_api_client-1.12.2 lib/json_api_client/request_params.rb
json_api_client-1.12.1 lib/json_api_client/request_params.rb
json_api_client-1.12.0 lib/json_api_client/request_params.rb
json_api_client-1.11.0 lib/json_api_client/request_params.rb