Class: Graphiti::Sideload::PolymorphicBelongsTo::Grouper

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/sideload/polymorphic_belongs_to.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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.extract_options!
      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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#on(name, &blk) ⇒ Object



43
44
45
46
47
# File 'lib/graphiti/sideload/polymorphic_belongs_to.rb', line 43

def on(name, &blk)
  group = Group.new(name.to_sym)
  @groups << group
  group
end