Class: JsonApiClient::RequestParams

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/request_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, includes: [], fields: {}) ⇒ RequestParams

Returns a new instance of RequestParams.


5
6
7
8
9
# File 'lib/json_api_client/request_params.rb', line 5

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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields


3
4
5
# File 'lib/json_api_client/request_params.rb', line 3

def fields
  @fields
end

#includesObject (readonly)

Returns the value of attribute includes


3
4
5
# File 'lib/json_api_client/request_params.rb', line 3

def includes
  @includes
end

#klassObject (readonly)

Returns the value of attribute klass


3
4
5
# File 'lib/json_api_client/request_params.rb', line 3

def klass
  @klass
end

Instance Method Details

#add_includes(includes) ⇒ Object


11
12
13
14
15
16
# File 'lib/json_api_client/request_params.rb', line 11

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

#clearObject


34
35
36
37
# File 'lib/json_api_client/request_params.rb', line 34

def clear
  reset_includes!
  @fields = {}
end

#field_typesObject


30
31
32
# File 'lib/json_api_client/request_params.rb', line 30

def field_types
  self.fields.keys
end

#remove_fields(type) ⇒ Object


26
27
28
# File 'lib/json_api_client/request_params.rb', line 26

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

#reset_includes!Object


18
19
20
# File 'lib/json_api_client/request_params.rb', line 18

def reset_includes!
  @includes = []
end

#set_fields(type, field_names) ⇒ Object


22
23
24
# File 'lib/json_api_client/request_params.rb', line 22

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

#to_paramsObject


39
40
41
42
# File 'lib/json_api_client/request_params.rb', line 39

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