Class: GraphQL::Language::Nodes::AbstractNode
- Inherits:
-
Object
- Object
- GraphQL::Language::Nodes::AbstractNode
- 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
-
.required_attrs ⇒ Object
readonly
Returns the value of attribute required_attrs.
Instance Attribute Summary collapse
-
#col ⇒ Object
Returns the value of attribute col.
-
#line ⇒ Object
Returns the value of attribute line.
Class Method Summary collapse
-
.attr_required(*attr_names) ⇒ Object
Defines attributes which are required at initialization.
-
.create(*attr_names, &block) ⇒ Class
Create a new AbstractNode child which requires and exposes attr_names.
Instance Method Summary collapse
-
#children ⇒ Object
Test all attributes, checking for any other nodes below this one.
-
#initialize(options) ⇒ AbstractNode
constructor
A new instance of AbstractNode.
Constructor Details
#initialize(options) ⇒ AbstractNode
Returns a new instance of AbstractNode.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/graphql/language/nodes.rb', line 11 def initialize() required_keys = self.class.required_attrs allowed_keys = required_keys + [:line, :col] position_source = .delete(:position_source) if !position_source.nil? [:line], [:col] = position_source.line_and_column end present_keys = .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 .has_key?(key) value = [key] self.send("#{key}=", value) end end end |
Class Attribute Details
.required_attrs ⇒ Object (readonly)
Returns the value of attribute required_attrs.
47 48 49 |
# File 'lib/graphql/language/nodes.rb', line 47 def required_attrs @required_attrs end |
Instance Attribute Details
#col ⇒ Object
Returns the value of attribute col.
8 9 10 |
# File 'lib/graphql/language/nodes.rb', line 8 def col @col end |
#line ⇒ Object
Returns the value of attribute line.
8 9 10 |
# File 'lib/graphql/language/nodes.rb', line 8 def line @line end |
Class Method Details
.attr_required(*attr_names) ⇒ Object
Defines attributes which are required at initialization.
49 50 51 52 53 |
# File 'lib/graphql/language/nodes.rb', line 49 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.
60 61 62 63 64 |
# File 'lib/graphql/language/nodes.rb', line 60 def create(*attr_names, &block) cls = Class.new(self, &block) cls.attr_required(*attr_names) cls end |
Instance Method Details
#children ⇒ Object
Test all attributes, checking for any other nodes below this one
39 40 41 42 43 44 |
# File 'lib/graphql/language/nodes.rb', line 39 def children self.class.required_attrs .map { |attr| send(attr) } .flatten .select { |val| val.is_a?(GraphQL::Language::Nodes::AbstractNode) } end |