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.
-
#polymorphic_groups ⇒ Object
readonly
Returns the value of attribute polymorphic_groups.
-
#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(processed = []) ⇒ Object
Grab from nested sideloads, AND resource, recursively.
Constructor Details
#initialize(name, type: nil, resource: nil, polymorphic: false, primary_key: :id, foreign_key: nil) ⇒ Sideload
Returns a new instance of Sideload.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jsonapi_compliable/sideload.rb', line 15 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 |
#polymorphic_groups ⇒ Object (readonly)
Returns the value of attribute polymorphic_groups.
3 4 5 |
# File 'lib/jsonapi_compliable/sideload.rb', line 3 def polymorphic_groups @polymorphic_groups 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
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jsonapi_compliable/sideload.rb', line 62 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
40 41 42 |
# File 'lib/jsonapi_compliable/sideload.rb', line 40 def assign(&blk) @assign_proc = blk end |
#associate(parent, child) ⇒ Object
44 45 46 |
# File 'lib/jsonapi_compliable/sideload.rb', line 44 def associate(parent, child) resource_class.config[:adapter].associate(parent, child, name, type) end |
#group_by(&grouper) ⇒ Object
48 49 50 |
# File 'lib/jsonapi_compliable/sideload.rb', line 48 def group_by(&grouper) @grouper = grouper end |
#polymorphic? ⇒ Boolean
32 33 34 |
# File 'lib/jsonapi_compliable/sideload.rb', line 32 def polymorphic? @polymorphic == true end |
#resolve(parents, query, namespace = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/jsonapi_compliable/sideload.rb', line 52 def resolve(parents, query, namespace = nil) namespace ||= name if polymorphic? resolve_polymorphic(parents, query) else resolve_basic(parents, query, namespace) end end |
#resource ⇒ Object
28 29 30 |
# File 'lib/jsonapi_compliable/sideload.rb', line 28 def resource @resource ||= resource_class.new end |
#scope(&blk) ⇒ Object
36 37 38 |
# File 'lib/jsonapi_compliable/sideload.rb', line 36 def scope(&blk) @scope_proc = blk end |
#sideload(name) ⇒ Object
73 74 75 |
# File 'lib/jsonapi_compliable/sideload.rb', line 73 def sideload(name) @sideloads[name] end |
#to_hash(processed = []) ⇒ Object
Grab from nested sideloads, AND resource, recursively
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jsonapi_compliable/sideload.rb', line 78 def to_hash(processed = []) return { name => {} } if processed.include?(self) processed << self result = { name => {} }.tap do |hash| @sideloads.each_pair do |key, sideload| hash[name][key] = sideload.to_hash(processed)[key] || {} if sideload.polymorphic? sideload.polymorphic_groups.each_pair do |type, sl| hash[name][key].merge!(nested_sideload_hash(sl, processed)) end else hash[name][key].merge!(nested_sideload_hash(sideload, processed)) end end end result end |