Class: XPath::XPathString

Inherits:
Object
  • Object
show all
Includes:
XPathObject
Defined in:
lib/xml/xpath.rb

Instance Method Summary collapse

Methods included from XPathObject

#**, #<, #<=, #==, #>, #>=, #at, #funcall, #predicate, #to_boolean, #to_number, #to_predicate

Constructor Details

#initialize(str) ⇒ XPathString

Returns a new instance of XPathString.

Raises:

  • (::TypeError)


800
801
802
803
# File 'lib/xml/xpath.rb', line 800

def initialize(str)
  raise ::TypeError, "must be a String" unless str.is_a? String
  @value = str
end

Instance Method Details

#concat(s) ⇒ Object



830
831
832
833
# File 'lib/xml/xpath.rb', line 830

def concat(s)
  @value = @value + s
  self
end

#contain?(s) ⇒ Boolean

Returns:

  • (Boolean)


839
840
841
# File 'lib/xml/xpath.rb', line 839

def contain?(s)
  /#{Regexp.quote(s)}/ =~ @value
end

#normalize_spaceObject



898
899
900
901
902
# File 'lib/xml/xpath.rb', line 898

def normalize_space
  @value = @value.strip
  @value.gsub!(/\s+/, ' ')
  self
end

#replace(str) ⇒ Object



914
915
916
917
# File 'lib/xml/xpath.rb', line 914

def replace(str)
  @value = str
  self
end

#sizeObject



894
895
896
# File 'lib/xml/xpath.rb', line 894

def size
  @value.gsub(/[^\Wa-zA-Z_\d]/, ' ').size
end

#start_with?(s) ⇒ Boolean

Returns:

  • (Boolean)


835
836
837
# File 'lib/xml/xpath.rb', line 835

def start_with?(s)
  /\A#{Regexp.quote(s)}/ =~ @value
end

#substring(start, len) ⇒ Object



861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/xml/xpath.rb', line 861

def substring(start, len)
  start = start.round.to_f
  if start.infinite? or start.nan? then
    @value = ''
  elsif len then
    len = len.round.to_f
    maxlen = start + len
    len = maxlen - 1.0 if len >= maxlen
    if start <= 1.0 then
      start = 0
    else
      start = start.to_i - 1
    end
    if len.nan? or len < 1.0 then
      @value = ''
    elsif len.infinite? then
      # @value = @value[start..-1]
      /\A[\W\w]{0,#{start}}/ =~ @value
      @value = $'
    else
      # @value = @value[start, len.to_i]
      /\A[\W\w]{0,#{start}}([\W\w]{0,#{len.to_i}})/ =~ @value
      @value = $1
    end
  elsif start > 1.0 then
    # @value = @value[(start-1)..-1]
    /\A[\W\w]{0,#{start.to_i-1}}/ =~ @value
    @value = $'
  end
  raise "BUG" unless @value
  self
end

#substring_after(s) ⇒ Object



852
853
854
855
856
857
858
859
# File 'lib/xml/xpath.rb', line 852

def substring_after(s)
  if /#{Regexp.quote(s)}/ =~ @value then
    @value = $'
  else
    @value = ''
  end
  self
end

#substring_before(s) ⇒ Object



843
844
845
846
847
848
849
850
# File 'lib/xml/xpath.rb', line 843

def substring_before(s)
  if /#{Regexp.quote(s)}/ =~ @value then
    @value = $`
  else
    @value = ''
  end
  self
end

#to_fObject



809
810
811
812
813
814
815
# File 'lib/xml/xpath.rb', line 809

def to_f
  if /\A\s*(-?\d+\.?\d*)(?:\s|\z)/ =~ @value then
    $1.to_f
  else
    0.0 / 0.0  # NaN
  end
end

#to_rubyObject



821
822
823
# File 'lib/xml/xpath.rb', line 821

def to_ruby
  to_str
end

#to_strObject



805
806
807
# File 'lib/xml/xpath.rb', line 805

def to_str
  @value
end

#to_string(context) ⇒ Object



825
826
827
# File 'lib/xml/xpath.rb', line 825

def to_string(context)
  self
end

#translate(from, to) ⇒ Object



904
905
906
907
908
909
910
911
912
# File 'lib/xml/xpath.rb', line 904

def translate(from, to)
  to = to.split(//)
  h = {}
  from.split(//).each_with_index { |i,n|
    h[i] = to[n] unless h.key? i
  }
  @value = @value.gsub(/[#{Regexp.quote(h.keys.join)}]/) { |s| h[s] }
  self
end

#true?Boolean

Returns:

  • (Boolean)


817
818
819
# File 'lib/xml/xpath.rb', line 817

def true?
  not @value.empty?
end