Class: GraphQL::Nodes::AbstractNode
- Inherits:
-
Object
- Object
- GraphQL::Nodes::AbstractNode
- Defined in:
- lib/graph_ql/parser/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
-
.create(*attr_names, &block) ⇒ Object
Create a new AbstractNode child which requires and exposes attr_names.
Instance Method Summary collapse
- #children ⇒ Object
-
#initialize(options) ⇒ AbstractNode
constructor
A new instance of AbstractNode.
Constructor Details
#initialize(options) ⇒ AbstractNode
Returns a new instance of AbstractNode.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/graph_ql/parser/nodes.rb', line 7 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.
42 43 44 |
# File 'lib/graph_ql/parser/nodes.rb', line 42 def required_attrs @required_attrs end |
Instance Attribute Details
#col ⇒ Object
Returns the value of attribute col.
6 7 8 |
# File 'lib/graph_ql/parser/nodes.rb', line 6 def col @col end |
#line ⇒ Object
Returns the value of attribute line.
6 7 8 |
# File 'lib/graph_ql/parser/nodes.rb', line 6 def line @line end |
Class Method Details
.attr_required(*attr_names) ⇒ Object
43 44 45 46 47 |
# File 'lib/graph_ql/parser/nodes.rb', line 43 def attr_required(*attr_names) @required_attrs ||= [] @required_attrs += attr_names attr_accessor(*attr_names) end |
.create(*attr_names, &block) ⇒ Object
Create a new AbstractNode child which requires and exposes attr_names.
51 52 53 54 55 |
# File 'lib/graph_ql/parser/nodes.rb', line 51 def create(*attr_names, &block) cls = Class.new(self, &block) cls.attr_required(*attr_names) cls end |
Instance Method Details
#children ⇒ Object
34 35 36 37 38 39 |
# File 'lib/graph_ql/parser/nodes.rb', line 34 def children self.class.required_attrs .map { |attr| send(attr) } .flatten # eg #fields is a list of children .select { |val| val.is_a?(GraphQL::Nodes::AbstractNode) } end |