Class: MARC::Spec::Queries::Position

Inherits:
Object
  • Object
show all
Includes:
PositionOrRange
Defined in:
lib/marc/spec/queries/position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Part

#eql?, #hash, #inspect

Constructor Details

#initialize(position) ⇒ Position

Returns a new instance of Position.



14
15
16
# File 'lib/marc/spec/queries/position.rb', line 14

def initialize(position)
  @position = int_or_nil(position)
end

Instance Attribute Details

#positionObject (readonly)


Attributes



12
13
14
# File 'lib/marc/spec/queries/position.rb', line 12

def position
  @position
end

Instance Method Details

#select_from(seq) ⇒ Array #select_from(seq) ⇒ String?

Overloads:

  • #select_from(seq) ⇒ Array

    Selects the value at the specified position and returns it as a single-element array, for compatibility with AlNumRange

    Parameters:

    • the input array

    Returns:

    • a single-element array containing the result, or an empty array if the index is out of bounds

  • #select_from(seq) ⇒ String?

    Selects the character at the specified position

    Parameters:

    • the input array

    Returns:

    • a single-element array, or nil if the index is out of bounds



32
33
34
35
36
# File 'lib/marc/spec/queries/position.rb', line 32

def select_from(seq)
  # we use raw_result[-1] instead of raw_result.last b/c seq might be a string
  raw_result = seq[position.nil? ? -1 : position]
  seq.is_a?(String) ? wrap_string_result(raw_result) : wrap_array_result(raw_result)
end

#to_sObject


Object overrides



41
42
43
# File 'lib/marc/spec/queries/position.rb', line 41

def to_s
  (position || '#').to_s
end