Class: JsonApi::Resources::Relationship

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

Direct Known Subclasses

ToManyRelationship, ToOneRelationship

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Relationship

Returns a new instance of Relationship.



51
52
53
54
55
56
57
# File 'lib/json_api_ruby/resources/relationships.rb', line 51

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

Instance Attribute Details

#includedObject (readonly)

Determines whether the data attribute should be filled out and included



45
46
47
# File 'lib/json_api_ruby/resources/relationships.rb', line 45

def included
  @included
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/json_api_ruby/resources/relationships.rb', line 46

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



40
41
42
# File 'lib/json_api_ruby/resources/relationships.rb', line 40

def parent
  @parent
end

#parent_modelObject (readonly)

Returns the value of attribute parent_model.



41
42
43
# File 'lib/json_api_ruby/resources/relationships.rb', line 41

def parent_model
  @parent_model
end

#resourcesObject (readonly)

The resource object that represents this relationship



49
50
51
# File 'lib/json_api_ruby/resources/relationships.rb', line 49

def resources
  @resources
end

Instance Method Details

#included?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/json_api_ruby/resources/relationships.rb', line 59

def included?
  included == true
end


69
70
71
72
73
74
75
76
# File 'lib/json_api_ruby/resources/relationships.rb', line 69

def 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



63
64
65
66
67
# File 'lib/json_api_ruby/resources/relationships.rb', line 63

def to_hash
  return_hash = links
  return_hash.merge!(data) if included?
  return_hash
end