Class: JsonapiCompliable::Sideload
- Inherits:
-
Object
- Object
- JsonapiCompliable::Sideload
- Defined in:
- lib/jsonapi_compliable/sideload.rb
Instance Attribute Summary collapse
-
#assign_proc ⇒ Object
readonly
Returns the value of attribute assign_proc.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#grouper ⇒ Object
readonly
Returns the value of attribute grouper.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#polymorphic ⇒ Object
readonly
Returns the value of attribute polymorphic.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
-
#scope_proc ⇒ Object
readonly
Returns the value of attribute scope_proc.
-
#sideloads ⇒ Object
readonly
Returns the value of attribute sideloads.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #allow_sideload(name, opts = {}, &blk) ⇒ Object
- #assign(&blk) ⇒ Object
- #associate(parent, child) ⇒ Object
- #group_by(&grouper) ⇒ Object
-
#initialize(name, type: nil, resource: nil, polymorphic: false, primary_key: :id, foreign_key: nil) ⇒ Sideload
constructor
A new instance of Sideload.
- #polymorphic? ⇒ Boolean
- #resolve(parents, query, namespace = nil) ⇒ Object
- #resource ⇒ Object
- #scope(&blk) ⇒ Object
- #sideload(name) ⇒ Object
-
#to_hash(levels_deep = 0) ⇒ Object
Grab from nested sideloads, AND resource, recursively To prevent circular relationships (author resource sideloads books and books resource sideloads authors), this allows sideloading up to 10 levels deep into the nesting of relationships We may want to make this configurable (possible at runtime), or maybe there is a better pattern here.
Constructor Details
#initialize(name, type: nil, resource: nil, polymorphic: false, primary_key: :id, foreign_key: nil) ⇒ Sideload
Returns a new instance of Sideload.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi_compliable/sideload.rb', line 14 def initialize(name, type: nil, resource: nil, polymorphic: false, primary_key: :id, foreign_key: nil) @name = name @resource_class = (resource || Class.new(Resource)) @sideloads = {} @polymorphic = !!polymorphic @polymorphic_groups = {} if polymorphic? @primary_key = primary_key @foreign_key = foreign_key @type = type extend @resource_class.config[:adapter].sideloading_module end |
Instance Attribute Details
#assign_proc ⇒ Object (readonly)
Returns the value of attribute assign_proc.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def assign_proc @assign_proc end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def foreign_key @foreign_key end |
#grouper ⇒ Object (readonly)
Returns the value of attribute grouper.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def grouper @grouper end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def name @name end |
#polymorphic ⇒ Object (readonly)
Returns the value of attribute polymorphic.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def polymorphic @polymorphic end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def primary_key @primary_key end |
#resource_class ⇒ Object (readonly)
Returns the value of attribute resource_class.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def resource_class @resource_class end |
#scope_proc ⇒ Object (readonly)
Returns the value of attribute scope_proc.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def scope_proc @scope_proc end |
#sideloads ⇒ Object (readonly)
Returns the value of attribute sideloads.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def sideloads @sideloads end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def type @type end |
Instance Method Details
#allow_sideload(name, opts = {}, &blk) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapi_compliable/sideload.rb', line 61 def allow_sideload(name, opts = {}, &blk) sideload = Sideload.new(name, opts) sideload.instance_eval(&blk) if blk if polymorphic? @polymorphic_groups[name] = sideload else @sideloads[name] = sideload end end |
#assign(&blk) ⇒ Object
39 40 41 |
# File 'lib/jsonapi_compliable/sideload.rb', line 39 def assign(&blk) @assign_proc = blk end |
#associate(parent, child) ⇒ Object
43 44 45 |
# File 'lib/jsonapi_compliable/sideload.rb', line 43 def associate(parent, child) resource_class.config[:adapter].associate(parent, child, name, type) end |
#group_by(&grouper) ⇒ Object
47 48 49 |
# File 'lib/jsonapi_compliable/sideload.rb', line 47 def group_by(&grouper) @grouper = grouper end |
#polymorphic? ⇒ Boolean
31 32 33 |
# File 'lib/jsonapi_compliable/sideload.rb', line 31 def polymorphic? @polymorphic == true end |
#resolve(parents, query, namespace = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/jsonapi_compliable/sideload.rb', line 51 def resolve(parents, query, namespace = nil) namespace ||= name if polymorphic? resolve_polymorphic(parents, query) else resolve_basic(parents, query, namespace) end end |
#resource ⇒ Object
27 28 29 |
# File 'lib/jsonapi_compliable/sideload.rb', line 27 def resource @resource ||= resource_class.new end |
#scope(&blk) ⇒ Object
35 36 37 |
# File 'lib/jsonapi_compliable/sideload.rb', line 35 def scope(&blk) @scope_proc = blk end |
#sideload(name) ⇒ Object
72 73 74 |
# File 'lib/jsonapi_compliable/sideload.rb', line 72 def sideload(name) @sideloads[name] end |
#to_hash(levels_deep = 0) ⇒ Object
Grab from nested sideloads, AND resource, recursively To prevent circular relationships (author resource sideloads books and books resource sideloads authors), this allows sideloading up to 10 levels deep into the nesting of relationships We may want to make this configurable (possible at runtime), or maybe there is a better pattern here
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/jsonapi_compliable/sideload.rb', line 82 def to_hash(levels_deep = 0) levels_deep += 1 return {} if levels_deep == 10 { name => {} }.tap do |hash| @sideloads.each_pair do |key, sideload| hash[name][key] = sideload.to_hash(levels_deep)[key] || {} if sideloading = sideload.resource_class.sideloading hash[name][key].merge!(sideloading.to_hash(levels_deep)[:base] || {}) end end end end |