Class: JsonApiModel::Associations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_model/associations/base.rb

Direct Known Subclasses

BelongsTo, Has, Preloaders::Has

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class, name, opts = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
# File 'lib/json_api_model/associations/base.rb', line 8

def initialize( base_class, name, opts = {} )
  self.name       = name
  self.opts       = opts
  self.key        = idify( base_class )
  self.base_class = base_class

  validate_opts!
end

Instance Attribute Details

#base_classObject

Returns the value of attribute base_class.



5
6
7
# File 'lib/json_api_model/associations/base.rb', line 5

def base_class
  @base_class
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/json_api_model/associations/base.rb', line 5

def key
  @key
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/json_api_model/associations/base.rb', line 5

def name
  @name
end

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/json_api_model/associations/base.rb', line 5

def opts
  @opts
end

Instance Method Details

#association_classObject



25
26
27
28
29
# File 'lib/json_api_model/associations/base.rb', line 25

def association_class
  @associated_class ||= opts[:class] ||
                        opts[:class_name]&.constantize ||
                        derived_class
end

#fetch(instance) ⇒ Object



17
18
19
# File 'lib/json_api_model/associations/base.rb', line 17

def fetch( instance )
  process association_class.send( action, query( instance ) )
end

#json_relationship?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/json_api_model/associations/base.rb', line 21

def json_relationship?
  association_class < JsonApiModel::Model
end

#process(results) ⇒ Object



31
32
33
# File 'lib/json_api_model/associations/base.rb', line 31

def process( results )
  results
end