Class: Graphiti::Sideload::PolymorphicBelongsTo::Grouper
- Defined in:
- lib/graphiti/sideload/polymorphic_belongs_to.rb
Instance Attribute Summary collapse
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
Instance Method Summary collapse
- #apply(sideload, resource_class) ⇒ Object
- #excluded?(group_sym) ⇒ Boolean
- #expected?(group_sym) ⇒ Boolean
- #ignore?(group_name) ⇒ Boolean
-
#initialize(field_name, opts = {}) ⇒ Grouper
constructor
A new instance of Grouper.
- #on(name, &blk) ⇒ Object
Constructor Details
#initialize(field_name, opts = {}) ⇒ Grouper
Returns a new instance of Grouper.
23 24 25 26 27 28 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 23 def initialize(field_name, opts = {}) @field_name = field_name @groups = [] @except = Array(opts[:except]).map(&:to_sym) @only = Array(opts[:only]).map(&:to_sym) end |
Instance Attribute Details
#field_name ⇒ Object (readonly)
Returns the value of attribute field_name.
21 22 23 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 21 def field_name @field_name end |
Instance Method Details
#apply(sideload, resource_class) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 49 def apply(sideload, resource_class) @groups.each do |group| if group.calls.empty? group.belongs_to(group.name.to_s.underscore.to_sym) end group.calls.each do |call| args = call[1] opts = args. opts.merge! as: sideload.name, parent: sideload, group_name: group.name, polymorphic_child: true unless sideload.resource.class.abstract_class? opts[:foreign_key] ||= sideload.foreign_key opts[:primary_key] ||= sideload.primary_key end args << opts resource_class.send(call[0], *args, &call[2]) end end end |
#excluded?(group_sym) ⇒ Boolean
34 35 36 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 34 def excluded?(group_sym) @except.include?(group_sym) end |
#expected?(group_sym) ⇒ Boolean
30 31 32 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 30 def expected?(group_sym) @only.empty? || @only.include?(group_sym) end |
#ignore?(group_name) ⇒ Boolean
38 39 40 41 |
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 38 def ignore?(group_name) group_sym = group_name.to_sym !expected?(group_sym) || excluded?(group_sym) end |