Module: GQL::Mixins::HasFields::ClassMethods

Defined in:
lib/gql/mixins/has_fields.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/gql/mixins/has_fields.rb', line 52

def method_missing(method, *args, &block)
  if type = GQL.field_types[method]
    define_field_method method, type
    send method, *args, &block
  else
    options = args.extract_options!.merge(type: method.to_sym)
    args = args.push(options)
    add_field(*args, &block)
  end
end

Instance Method Details

#add_field(id, *args, &block) ⇒ Object Also known as: field



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gql/mixins/has_fields.rb', line 18

def add_field(id, *args, &block)
  remove_field id

  id      = id.to_sym
  options = args.extract_options!
  type    = options.delete(:type) || Field
  proc    = args.shift || block || proc_for_field(id)

  build_field_class(type, id, proc, options).tap do |field_class|
    propagate :field, id, field_class
  end
end

#cursor(id_or_proc) ⇒ Object



41
42
43
44
45
46
# File 'lib/gql/mixins/has_fields.rb', line 41

def cursor(id_or_proc)
  id = id_or_proc.is_a?(Proc) ? nil : id_or_proc
  proc = id ? -> { target.public_send(id) } : id_or_proc

  add_field :cursor, proc, type: Scalar
end

#has_field?(id) ⇒ Boolean

Returns:



37
38
39
# File 'lib/gql/mixins/has_fields.rb', line 37

def has_field?(id)
  fields.has_key? id.to_sym
end

#remove_field(id) ⇒ Object



33
34
35
# File 'lib/gql/mixins/has_fields.rb', line 33

def remove_field(id)
  shutdown :field, id.to_sym
end

#respond_to?(method, *args) ⇒ Boolean

Returns:



48
49
50
# File 'lib/gql/mixins/has_fields.rb', line 48

def respond_to?(method, *args)
  super || GQL.field_types.has_key?(method)
end