class Fig::TokenizedString::PlainSegment

Attributes

raw_value[R]

Public Class Methods

new(raw_value) click to toggle source
# File lib/fig/tokenized_string/plain_segment.rb, line 9
def initialize(raw_value)
  @raw_value     = raw_value

  return
end

Public Instance Methods

to_double_quotable_string(metacharacters) click to toggle source

Should not be invoked if original string was not single quoted.

# File lib/fig/tokenized_string/plain_segment.rb, line 37
def to_double_quotable_string(metacharacters)
  quoted_value = @raw_value.gsub %r< ( ["#{metacharacters}] ) >xm, '\\\\\1'

  quoted_value.gsub!(
    %r<
      (
        (?: ^ | [^\\] ) # New line or non-backslash
        (\\{2})*        # Even number of backslashes
      )

      # All single quotes must have been escaped.  The important bit is to
      # not lose escaped backslashes.
      \\'
    >xm,
    %q<\1'>
  )

  return quoted_value
end
to_escaped_string() click to toggle source
# File lib/fig/tokenized_string/plain_segment.rb, line 23
def to_escaped_string()
  return @raw_value
end
to_expanded_string(&block) click to toggle source
# File lib/fig/tokenized_string/plain_segment.rb, line 19
def to_expanded_string(&block)
  return @raw_value.gsub(%r< \\ (.) >xm, '\1')
end
to_single_quoted_string() click to toggle source

Should not be invoked if original string was single quoted.

# File lib/fig/tokenized_string/plain_segment.rb, line 28
def to_single_quoted_string()
  # Raw value will have come from a non-single quoted string, so we unescape
  # everything (including backslashes) and then escape backslashes and single
  # quotes (which cannot be escaped outside of single quotes).
  return \
    @raw_value.gsub(%r< \\ (.) >xm, '\1').gsub(%r< ([\\']) >xm, '\\\\\1')
end
type() click to toggle source
# File lib/fig/tokenized_string/plain_segment.rb, line 15
def type
  return nil
end