Class: JSONAPI::Relationship
- Inherits:
-
Object
- Object
- JSONAPI::Relationship
- Defined in:
- lib/jsonapi/relationship.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#acts_as_set ⇒ Object
readonly
Returns the value of attribute acts_as_set.
-
#always_include_linkage_data ⇒ Object
readonly
Returns the value of attribute always_include_linkage_data.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#polymorphic ⇒ Object
(also: #polymorphic?)
readonly
Returns the value of attribute polymorphic.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Relationship
constructor
A new instance of Relationship.
- #primary_key ⇒ Object
- #relation_name(options) ⇒ Object
- #resource_klass ⇒ Object
- #type_for_source(source) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Relationship
Returns a new instance of Relationship.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/jsonapi/relationship.rb', line 6 def initialize(name, = {}) @name = name.to_s @options = @acts_as_set = .fetch(:acts_as_set, false) == true @foreign_key = [:foreign_key] ? [:foreign_key].to_sym : nil @module_path = [:module_path] || '' @relation_name = .fetch(:relation_name, @name) @polymorphic = .fetch(:polymorphic, false) == true @always_include_linkage_data = .fetch(:always_include_linkage_data, false) == true end |
Instance Attribute Details
#acts_as_set ⇒ Object (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_data ⇒ Object (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_name ⇒ Object (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_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def foreign_key @foreign_key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def @options end |
#polymorphic ⇒ Object (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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def type @type end |
Instance Method Details
#primary_key ⇒ Object
19 20 21 |
# File 'lib/jsonapi/relationship.rb', line 19 def primary_key @primary_key ||= resource_klass._primary_key end |
#relation_name(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jsonapi/relationship.rb', line 27 def relation_name() case @relation_name when Symbol # :nocov: @relation_name # :nocov: when String @relation_name.to_sym when Proc @relation_name.call() end end |
#resource_klass ⇒ Object
23 24 25 |
# File 'lib/jsonapi/relationship.rb', line 23 def resource_klass @resource_klass ||= Resource.resource_for(@module_path + @class_name) end |
#type_for_source(source) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/jsonapi/relationship.rb', line 40 def type_for_source(source) if polymorphic? resource = source.public_send(name) resource.class._type if resource else type end end |