Class: Pcut::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pcut/query.rb

Constant Summary collapse

FORMAT =
%r{\A\s*\[\s*/\\?(.)/\s*,\s*([0-9\-]+)\s*\]\z}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delimiter, index) ⇒ Query

Returns a new instance of Query.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pcut/query.rb', line 12

def initialize(delimiter, index)
  if !delimiter.is_a?(String) || delimiter.size != 1
    raise ArgumentError, "invalid delimiter: #{delimiter}"
  end
  if !index.is_a?(Pcut::RangeIndex)
    raise ArgumentError, "index must be a RangeIndex: #{index}"
  end

  @delimiter = delimiter
  @index     = index
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



10
11
12
# File 'lib/pcut/query.rb', line 10

def delimiter
  @delimiter
end

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/pcut/query.rb', line 10

def index
  @index
end

Class Method Details

.parse(str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/pcut/query.rb', line 24

def self.parse(str)
  unless str.is_a?(String)
    raise ArgumentError, "#{str.to_s} is not a string"
  end
  unless str =~ FORMAT
    raise ArgumentError, "invalid format: #{str}"
  end
  index = Pcut::RangeIndex.parse($2)
  self.new($1, index)
end

Instance Method Details

#to_sObject



35
36
37
# File 'lib/pcut/query.rb', line 35

def to_s
  "[/%s/,%s]" % [@delimiter, @index.to_s]
end