Class: Fig::TokenizedString::PlainSegment

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/tokenized_string/plain_segment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_value) ⇒ PlainSegment

Returns a new instance of PlainSegment.


9
10
11
12
13
# File 'lib/fig/tokenized_string/plain_segment.rb', line 9

def initialize(raw_value)
  @raw_value     = raw_value

  return
end

Instance Attribute Details

#raw_valueObject (readonly)

Returns the value of attribute raw_value


7
8
9
# File 'lib/fig/tokenized_string/plain_segment.rb', line 7

def raw_value
  @raw_value
end

Instance Method Details

#to_double_quotable_string(metacharacters) ⇒ Object

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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_stringObject


23
24
25
# File 'lib/fig/tokenized_string/plain_segment.rb', line 23

def to_escaped_string()
  return @raw_value
end

#to_expanded_string(&block) ⇒ Object


19
20
21
# 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_stringObject

Should not be invoked if original string was single quoted.


28
29
30
31
32
33
34
# 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

#typeObject


15
16
17
# File 'lib/fig/tokenized_string/plain_segment.rb', line 15

def type
  return nil
end