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.



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

def initialize(raw_value)
  @raw_value     = raw_value

  return
end

Instance Attribute Details

#raw_valueObject (readonly)

Returns the value of attribute raw_value.



5
6
7
# File 'lib/fig/tokenized_string/plain_segment.rb', line 5

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fig/tokenized_string/plain_segment.rb', line 35

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



21
22
23
# File 'lib/fig/tokenized_string/plain_segment.rb', line 21

def to_escaped_string()
  return @raw_value
end

#to_expanded_string(&block) ⇒ Object



17
18
19
# File 'lib/fig/tokenized_string/plain_segment.rb', line 17

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.



26
27
28
29
30
31
32
# File 'lib/fig/tokenized_string/plain_segment.rb', line 26

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



13
14
15
# File 'lib/fig/tokenized_string/plain_segment.rb', line 13

def type
  return nil
end