Class: JsonApi::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_ruby/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Association

Returns a new instance of Association.



24
25
26
27
28
29
30
31
# File 'lib/json_api_ruby/association.rb', line 24

def initialize(name, options)
  @name = name
  @resources = []
  @parent = options.fetch(:parent_resource)
  @parent_model = parent._model
  @included = options.fetch(:included, JsonApi::Includes.new)
  @explicit_resource_class = options.fetch(:explicit_resource_class)
end

Instance Attribute Details

#explicit_resource_classObject (readonly)

Returns the value of attribute explicit_resource_class.



18
19
20
# File 'lib/json_api_ruby/association.rb', line 18

def explicit_resource_class
  @explicit_resource_class
end

#includedObject (readonly)

Determines whether the ‘data` attribute should be filled out and included



16
17
18
# File 'lib/json_api_ruby/association.rb', line 16

def included
  @included
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/json_api_ruby/association.rb', line 17

def name
  @name
end

#parentObject (readonly)

The resource object that “owns” this relationship

Example:

class ArticleResource < JsonApi::Resource
  has_one :author
end

‘ArticleResource` is the parent of the author object



11
12
13
# File 'lib/json_api_ruby/association.rb', line 11

def parent
  @parent
end

#parent_modelObject (readonly)

Returns the value of attribute parent_model.



12
13
14
# File 'lib/json_api_ruby/association.rb', line 12

def parent_model
  @parent_model
end

#resourcesObject (readonly)

The resource object that represents this relationship



22
23
24
# File 'lib/json_api_ruby/association.rb', line 22

def resources
  @resources
end

Instance Method Details

#included?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/json_api_ruby/association.rb', line 33

def included?
  included.has_name?(@name)
end


44
45
46
47
48
49
50
51
# File 'lib/json_api_ruby/association.rb', line 44

def relationship_links
  {
    'links' => {
      'self' => JsonApi.configuration.base_url + parent.self_link_path + "/relationships/#{name}",
      'related' => JsonApi.configuration.base_url + parent.self_link_path + "/#{name}"
    }
  }
end

#to_hashObject



37
38
39
40
41
42
# File 'lib/json_api_ruby/association.rb', line 37

def to_hash
  return_hash = {}
  return_hash.merge!(relationship_links) if JsonApi.configuration.use_links
  return_hash.merge!(data) if included?
  return_hash
end