Class: Sequel::Postgres::PGMultiRange::Parser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/sequel/extensions/pg_multirange.rb

Overview

Converts strings into PGMultiRange instances.

Instance Method Summary collapse

Constructor Details

#initialize(source, converter) ⇒ Parser

Returns a new instance of Parser.


62
63
64
65
# File 'lib/sequel/extensions/pg_multirange.rb', line 62

def initialize(source, converter)
  super(source)
  @converter = converter 
end

Instance Method Details

#parseObject

Parse the multirange type input string into a PGMultiRange value.

Raises:


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sequel/extensions/pg_multirange.rb', line 68

def parse
  raise Sequel::Error, "invalid multirange, doesn't start with {" unless get_byte == '{'
  ranges = []

  unless scan(/\}/)
    while true
      raise Sequel::Error, "unfinished multirange" unless range_string = scan_until(/[\]\)]/)
      ranges << @converter.call(range_string)
      
      case sep = get_byte
      when '}'
        break
      when ','
        # nothing
      else
        raise Sequel::Error, "invalid multirange separator: #{sep.inspect}"
      end
    end
  end

  raise Sequel::Error, "invalid multirange, remaining data after }" unless eos?
  ranges
end