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



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pg_query/node.rb', line 9

def self.from(node_field_val)
  # This needs to match libpg_query naming for the Node message field names
  # (see "underscore" method in libpg_query's scripts/generate_protobuf_and_funcs.rb)
  node_field_name = node_field_val.class.name.split('::').last
  node_field_name.gsub!(/^([A-Z\d])([A-Z][a-z])/, '\1__\2')
  node_field_name.gsub!(/([A-Z\d]+[a-z]+)([A-Z][a-z])/, '\1_\2')
  node_field_name.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  node_field_name.tr!('-', '_')
  node_field_name.downcase!

  PgQuery::Node.new(node_field_name => node_field_val)
end

.from_integer(ival) ⇒ Object



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

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

.from_string(str) ⇒ Object

Make it easier to initialize value nodes



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

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

Instance Method Details

#inspectObject



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

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