Class: Torque::PostgreSQL::Attributes::LQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/attributes/lquery.rb

Overview

A pattern that matches label paths, as described by the lquery data type. It is only ever built from Ruby and never parsed back from its text form, so each entry of the given list becomes one item

Constant Summary collapse

MARKERS =
/[*|!{}@%]/
QUANTIFIER =
/\{(?:\d+|\d*,\d*)\}/
LABEL =
/[[:alnum:]_-]+/
ALTERNATIVE =
/#{LABEL}[@*%]{0,3}/
ITEM =
/\A!?#{ALTERNATIVE}(?:\|#{ALTERNATIVE})*#{QUANTIFIER}?\z/
STAR_ITEM =
/\A\*#{QUANTIFIER}?\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, normalize: true) ⇒ LQuery

Returns a new instance of LQuery.



49
50
51
52
# File 'lib/torque/postgresql/attributes/lquery.rb', line 49

def initialize(items, normalize: true)
  items = normalize ? expand(items) : Array.wrap(items)
  @items = normalize ? items.map { |item| compile(item) } : items.map(&:to_s)
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



47
48
49
# File 'lib/torque/postgresql/attributes/lquery.rb', line 47

def items
  @items
end

Class Method Details

.[](*items) ⇒ Object



18
19
20
# File 'lib/torque/postgresql/attributes/lquery.rb', line 18

def [](*items)
  new(items)
end

.load(value) ⇒ Object

Values coming from the database are valid by construction



23
24
25
# File 'lib/torque/postgresql/attributes/lquery.rb', line 23

def load(value)
  new(value, normalize: false)
end

.marker?(value) ⇒ Boolean

Whether the given value asks for a pattern instead of a plain path, which is what makes a condition use ~ instead of =

Returns:

  • (Boolean)


29
30
31
# File 'lib/torque/postgresql/attributes/lquery.rb', line 29

def marker?(value)
  Array.wrap(value).any? { |item| item_marker?(item) }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



58
59
60
# File 'lib/torque/postgresql/attributes/lquery.rb', line 58

def ==(other)
  other.is_a?(LQuery) && to_s == other.to_s
end

#hashObject



63
64
65
# File 'lib/torque/postgresql/attributes/lquery.rb', line 63

def hash
  to_s.hash
end

#to_sObject



54
55
56
# File 'lib/torque/postgresql/attributes/lquery.rb', line 54

def to_s
  items.join('.')
end