Class: JsonApiClient::Relationships::Relations

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, Helpers::Dirty, Helpers::DynamicAttributes
Defined in:
lib/json_api_client/relationships/relations.rb

Instance Method Summary collapse

Methods included from Helpers::Dirty

#attribute_change, #attribute_changed?, #attribute_was, #attribute_will_change!, #changed, #changed?, #changed_attributes, #clear_changes_information, #set_all_attributes_dirty, #set_attribute_was

Methods included from Helpers::DynamicAttributes

#[], #[]=, #attributes, #attributes=, #has_attribute?, #respond_to_missing?

Constructor Details

#initialize(relations) ⇒ Relations

Returns a new instance of Relations.



8
9
10
11
# File 'lib/json_api_client/relationships/relations.rb', line 8

def initialize(relations)
  self.attributes = relations
  clear_changes_information
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class JsonApiClient::Helpers::Dirty

Instance Method Details

#as_jsonObject



23
24
25
26
27
# File 'lib/json_api_client/relationships/relations.rb', line 23

def as_json
  Hash[attributes.map do |k, v|
         [k, v.slice("data")]  if v.has_key?("data")
       end.compact]
end

#as_json_apiObject



17
18
19
20
21
# File 'lib/json_api_client/relationships/relations.rb', line 17

def as_json_api
  Hash[attributes_for_serialization.map do |k, v|
         [k, v.slice("data")]  if v.has_key?("data")
       end.compact]
end

#attributes_for_serializationObject



29
30
31
# File 'lib/json_api_client/relationships/relations.rb', line 29

def attributes_for_serialization
  attributes.slice(*changed)
end

#present?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/json_api_client/relationships/relations.rb', line 13

def present?
  attributes.present?
end