Class: PgQuery::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_query/node.rb

Overview

Patch the auto-generated generic node type with additional convenience functions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(node_field_val) ⇒ Object

Make it easier to initialize nodes from a given node child object



23
24
25
# File 'lib/pg_query/node.rb', line 23

def self.from(node_field_val)
  PgQuery::Node.new(inner_class_to_name(node_field_val.class) => node_field_val)
end

.from_integer(ival) ⇒ Object



32
33
34
# File 'lib/pg_query/node.rb', line 32

def self.from_integer(ival)
  PgQuery::Node.new(integer: PgQuery::Integer.new(ival: ival))
end

.from_string(sval) ⇒ Object

Make it easier to initialize value nodes



28
29
30
# File 'lib/pg_query/node.rb', line 28

def self.from_string(sval)
  PgQuery::Node.new(string: PgQuery::String.new(sval: sval))
end

.inner_class_to_name(klass) ⇒ Object



4
5
6
7
# File 'lib/pg_query/node.rb', line 4

def self.inner_class_to_name(klass)
  @inner_class_to_name ||= descriptor.lookup_oneof('node').to_h { |f| [f.subtype.msgclass, f.name.to_sym] }
  @inner_class_to_name[klass]
end

Instance Method Details

#innerObject



9
10
11
# File 'lib/pg_query/node.rb', line 9

def inner
  self[node.to_s]
end

#inner=(submsg) ⇒ Object



13
14
15
16
# File 'lib/pg_query/node.rb', line 13

def inner=(submsg)
  name = self.class.inner_class_to_name(submsg.class)
  public_send("#{name}=", submsg)
end

#inspectObject



18
19
20
# File 'lib/pg_query/node.rb', line 18

def inspect
  node ? format('<PgQuery::Node: %s: %s>', node, inner.inspect) : '<PgQuery::Node>'
end