Sha256: bc403f12fbacbe84baf2264f3562efcf7a5f610058fa4b275aac9e397d210e52

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'easy/jsonapi/request/query_param_collection'
require 'easy/jsonapi/name_value_pair'
require 'easy/jsonapi/exceptions/query_params_exceptions'
require 'easy/jsonapi/utility'


module JSONAPI
  class Request
    class QueryParamCollection < JSONAPI::NameValuePairCollection
      # A generic name=value query parameter
      class QueryParam < JSONAPI::NameValuePair

        # @param name [String] The name of the parameter
        # @param value [String | Array<String>] The value of the parameter
        def initialize(name, value)
          if instance_of?(QueryParam)
            JSONAPI::Exceptions::QueryParamsExceptions.check_param_name(name)
          end
          value = value.split(',') if value.is_a? String
          super(name, value)
        end
  
        # Update the query_param value, turning value into an array if it was given as a string
        # @param new_value [String, Array<String>] The new value of the Parameter
        def value=(new_value)
          new_value = new_value.split(',') if new_value.is_a? String
          super(new_value)
        end
        
        # Represents a parameter as a string
        def to_s
          "#{name}=#{JSONAPI::Utility.to_string_collection(value, delimiter: ',')}"
        end

        # @raise RuntimeError Cannot change the name of a QueryParam object
        def name=(_)
          raise 'Cannot change the name of QueryParam Objects'
        end
      end
    end
  end
end

# include=author,comments-likers,comments.likes
# author comments-likers comments.likes

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
easy-jsonapi-1.0.11 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.10 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.9 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.8 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.7 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.6 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.5 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.4 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.3 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.2 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.1 lib/easy/jsonapi/request/query_param_collection/query_param.rb
easy-jsonapi-1.0.0 lib/easy/jsonapi/request/query_param_collection/query_param.rb