Class: JSONAPI::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/relationship.rb

Direct Known Subclasses

ToMany, ToOne

Defined Under Namespace

Classes: ToMany, ToOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Relationship

Returns a new instance of Relationship


7
8
9
10
11
12
13
14
15
16
# File 'lib/jsonapi/relationship.rb', line 7

def initialize(name, options = {})
  @name = name.to_s
  @options = options
  @acts_as_set = options.fetch(:acts_as_set, false) == true
  @foreign_key = options[:foreign_key] ? options[:foreign_key].to_sym : nil
  @parent_resource = options[:parent_resource]
  @relation_name = options.fetch(:relation_name, @name)
  @polymorphic = options.fetch(:polymorphic, false) == true
  @always_include_linkage_data = options.fetch(:always_include_linkage_data, false) == true
end

Instance Attribute Details

#acts_as_setObject (readonly)

Returns the value of attribute acts_as_set


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def acts_as_set
  @acts_as_set
end

#always_include_linkage_dataObject (readonly)

Returns the value of attribute always_include_linkage_data


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def always_include_linkage_data
  @always_include_linkage_data
end

#class_nameObject (readonly)

Returns the value of attribute class_name


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def class_name
  @class_name
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def foreign_key
  @foreign_key
end

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def options
  @options
end

#parent_resourceObject (readonly)

Returns the value of attribute parent_resource


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def parent_resource
  @parent_resource
end

#polymorphicObject (readonly) Also known as: polymorphic?

Returns the value of attribute polymorphic


3
4
5
# File 'lib/jsonapi/relationship.rb', line 3

def polymorphic
  @polymorphic
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/jsonapi/relationship.rb', line 58

def belongs_to?
  false
end

#primary_keyObject


20
21
22
# File 'lib/jsonapi/relationship.rb', line 20

def primary_key
  @primary_key ||= resource_klass._primary_key
end

#relation_name(options) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jsonapi/relationship.rb', line 36

def relation_name(options)
  case @relation_name
    when Symbol
      # :nocov:
      @relation_name
      # :nocov:
    when String
      @relation_name.to_sym
    when Proc
      @relation_name.call(options)
  end
end

#resource_klassObject


24
25
26
# File 'lib/jsonapi/relationship.rb', line 24

def resource_klass
  @resource_klass ||= @parent_resource.resource_for(@class_name)
end

#table_nameObject


28
29
30
# File 'lib/jsonapi/relationship.rb', line 28

def table_name
  @table_name ||= resource_klass._table_name
end

#typeObject


32
33
34
# File 'lib/jsonapi/relationship.rb', line 32

def type
  @type ||= resource_klass._type.to_sym
end

#type_for_source(source) ⇒ Object


49
50
51
52
53
54
55
56
# File 'lib/jsonapi/relationship.rb', line 49

def type_for_source(source)
  if polymorphic?
    resource = source.public_send(name)
    resource.class._type if resource
  else
    type
  end
end