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

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) (defaults to: {})

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



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphql/language/nodes.rb', line 11

def initialize(options={})
  if options.key?(:position_source)
    position_source = options.delete(:position_source)
    @line, @col = position_source.line_and_column
  elsif options.key?(:line)
    @line = options.delete(:line)
    @col = options.delete(:col)
  end

  initialize_node(options)
end

Instance Attribute Details

#colObject

Returns the value of attribute col.



8
9
10
# File 'lib/graphql/language/nodes.rb', line 8

def col
  @col
end

#lineObject

Returns the value of attribute line.



8
9
10
# File 'lib/graphql/language/nodes.rb', line 8

def line
  @line
end

Class Method Details

.child_attributes(*attr_names) ⇒ Object



36
37
38
39
# File 'lib/graphql/language/nodes.rb', line 36

def child_attributes(*attr_names)
  @child_attributes ||= []
  @child_attributes += attr_names
end

Instance Method Details

#childrenGraphQL::Language::Nodes::AbstractNode

Returns all nodes in the tree below this one.

Returns:



29
30
31
32
33
# File 'lib/graphql/language/nodes.rb', line 29

def children
  self.class.child_attributes
    .map { |attr_name| public_send(attr_name) }
    .flatten
end

#initialize_node(options = {}) ⇒ Object

This is called with node-specific options

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/graphql/language/nodes.rb', line 24

def initialize_node(options={})
  raise NotImplementedError
end

#positionObject



42
43
44
# File 'lib/graphql/language/nodes.rb', line 42

def position
  [line, col]
end

#to_query_stringObject



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

def to_query_string
  Generation.generate(self)
end