Module: GQL::HasFields::ClassMethods

Defined in:
lib/gql/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



59
60
61
62
63
64
65
66
67
68
# File 'lib/gql/has_fields.rb', line 59

def method_missing(method, *args, &block)
  if type = GQL.field_types[method]
    define_field_method method, type
    send method, *args, &block
  else
    super
  end
rescue NoMethodError => exc
  raise Errors::NoMethodError.new(self, method, exc)
end

Instance Method Details

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gql/has_fields.rb', line 20

def add_field(id, *args, &block)
  options = args.extract_options!
  type = options.delete(:type) || Node
  proc = args.shift || block || proc_for_field(id)

  Node.validate_is_subclass! type, 'type'

  type.build_class(id, proc, options).tap do |field_class|
    const_name = const_name_for_field(id)

    const_set const_name, field_class unless const_defined?(const_name)
    self.fields = fields.merge(id.to_sym => field_class)
  end
end

#cursor(id_or_proc) ⇒ Object



48
49
50
51
52
53
# File 'lib/gql/has_fields.rb', line 48

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: Raw
end

#has_field?(id) ⇒ Boolean

Returns:



44
45
46
# File 'lib/gql/has_fields.rb', line 44

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

#remove_field(id) ⇒ Object



37
38
39
40
41
42
# File 'lib/gql/has_fields.rb', line 37

def remove_field(id)
  const_name = const_name_for_field(id)

  send :remove_const, const_name if const_defined?(const_name)
  fields.delete id
end

#respond_to?(method, *args) ⇒ Boolean

Returns:



55
56
57
# File 'lib/gql/has_fields.rb', line 55

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