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 Attribute Summary collapse

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, #forget_change!, #set_all_attributes_dirty, #set_attribute_was

Methods included from Helpers::DynamicAttributes

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

Constructor Details

#initialize(record_class, relations) ⇒ Relations

Returns a new instance of Relations.


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

def initialize(record_class, relations)
  @record_class = record_class
  self.attributes = relations
end

Dynamic Method Handling

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

Instance Attribute Details

#record_classObject (readonly)

Returns the value of attribute record_class


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

def record_class
  @record_class
end

Instance Method Details

#as_jsonObject


26
27
28
29
30
# File 'lib/json_api_client/relationships/relations.rb', line 26

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

#as_json_apiObject


20
21
22
23
24
# File 'lib/json_api_client/relationships/relations.rb', line 20

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


32
33
34
# File 'lib/json_api_client/relationships/relations.rb', line 32

def attributes_for_serialization
  attributes.slice(*changed)
end

#present?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/json_api_client/relationships/relations.rb', line 16

def present?
  attributes.present?
end