Class: GraphQL::Language::Nodes::AbstractNode

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/language/nodes.rb

Overview

AbstractNode creates classes who:

  • require their keyword arguments, throw ArgumentError if they don’t match

  • expose accessors for keyword arguments

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AbstractNode

Returns a new instance of AbstractNode.

Parameters:

  • options (Hash)

    Must contain all attributes defined by required_attrs, may also include ‘position_source`



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql/language/nodes.rb', line 9

def initialize(options)
  required_keys = self.class.required_attrs
  allowed_keys = required_keys + [:line, :col]
  position_source = options.delete(:position_source)
  if !position_source.nil?
    options[:line], options[:col] = position_source.line_and_column
  end

  present_keys = options.keys
  extra_keys = present_keys - allowed_keys
  if extra_keys.any?
    raise ArgumentError, "#{self.class.name} Extra arguments: #{extra_keys}"
  end

  missing_keys = required_keys - present_keys
  if missing_keys.any?
    raise ArgumentError, "#{self.class.name} Missing arguments: #{missing_keys}"
  end

  allowed_keys.each do |key|
    if options.has_key?(key)
      value = options[key]
      self.send("#{key}=", value)
    end
  end
end

Class Attribute Details

.required_attrsObject (readonly)

Returns the value of attribute required_attrs.



45
46
47
# File 'lib/graphql/language/nodes.rb', line 45

def required_attrs
  @required_attrs
end

Instance Attribute Details

#colObject

Returns the value of attribute col.



6
7
8
# File 'lib/graphql/language/nodes.rb', line 6

def col
  @col
end

#lineObject

Returns the value of attribute line.



6
7
8
# File 'lib/graphql/language/nodes.rb', line 6

def line
  @line
end

Class Method Details

.attr_required(*attr_names) ⇒ Object

Defines attributes which are required at initialization.



47
48
49
50
51
# File 'lib/graphql/language/nodes.rb', line 47

def attr_required(*attr_names)
  @required_attrs ||= []
  @required_attrs += attr_names
  attr_accessor(*attr_names)
end

.create(*attr_names, &block) ⇒ Class

Create a new AbstractNode child which requires and exposes attr_names.

Parameters:

  • attr_names (Array<Symbol>)

    Attributes this node class will have

  • block (Block)

    Block passed to ‘Class.new`

Returns:

  • (Class)

    A new node class



58
59
60
61
62
# File 'lib/graphql/language/nodes.rb', line 58

def create(*attr_names, &block)
  cls = Class.new(self, &block)
  cls.attr_required(*attr_names)
  cls
end

Instance Method Details

#childrenObject

Test all attributes, checking for any other nodes below this one



37
38
39
40
41
42
# File 'lib/graphql/language/nodes.rb', line 37

def children
  self.class.required_attrs
    .map { |attr| send(attr) }
    .flatten
    .select { |val| val.is_a?(GraphQL::Language::Nodes::AbstractNode) }
end