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.



58
59
60
61
62
63
64
65
# File 'lib/json_api_ruby/resources/relationships.rb', line 58

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

Instance Attribute Details

#explicit_resource_classObject (readonly)

Returns the value of attribute explicit_resource_class.



52
53
54
# File 'lib/json_api_ruby/resources/relationships.rb', line 52

def explicit_resource_class
  @explicit_resource_class
end

#includedObject (readonly)

Determines whether the data attribute should be filled out and included



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

def included
  @included
end

#nameObject (readonly)

Returns the value of attribute name.



51
52
53
# File 'lib/json_api_ruby/resources/relationships.rb', line 51

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



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

def parent
  @parent
end

#parent_modelObject (readonly)

Returns the value of attribute parent_model.



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

def parent_model
  @parent_model
end

#resourcesObject (readonly)

The resource object that represents this relationship



56
57
58
# File 'lib/json_api_ruby/resources/relationships.rb', line 56

def resources
  @resources
end

Instance Method Details

#included?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/json_api_ruby/resources/relationships.rb', line 67

def included?
  included == true
end


77
78
79
80
81
82
83
84
# File 'lib/json_api_ruby/resources/relationships.rb', line 77

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



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

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