Class: JunkletUpgradeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/junklet_upgrade_parser.rb

Overview

As with all parsers, this is easy if we allow peeking.

Constant Summary collapse

INACTIVE =
'inactive'
MAYBE_ACTIVE =
'maybe_active'
ACTIVE =
'active'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJunkletUpgradeParser

Returns a new instance of JunkletUpgradeParser.



37
38
# File 'lib/junklet_upgrade_parser.rb', line 37

def initialize
end

Instance Attribute Details

#junkletsObject (readonly)

Returns the value of attribute junklets.



31
32
33
# File 'lib/junklet_upgrade_parser.rb', line 31

def junklets
  @junklets
end

#letsObject (readonly)

Returns the value of attribute lets.



31
32
33
# File 'lib/junklet_upgrade_parser.rb', line 31

def lets
  @lets
end

#linesObject (readonly)

Returns the value of attribute lines.



31
32
33
# File 'lib/junklet_upgrade_parser.rb', line 31

def lines
  @lines
end

#modeObject (readonly)

Returns the value of attribute mode.



31
32
33
# File 'lib/junklet_upgrade_parser.rb', line 31

def mode
  @mode
end

Instance Method Details

#activeObject



42
# File 'lib/junklet_upgrade_parser.rb', line 42

def active; ACTIVE; end

#active?Boolean

Returns:

  • (Boolean)


46
# File 'lib/junklet_upgrade_parser.rb', line 46

def active?; mode == active; end

#imblart_line(line) ⇒ Object



119
120
121
# File 'lib/junklet_upgrade_parser.rb', line 119

def imblart_line(line)
  Line.new line
end

#inactiveObject



40
# File 'lib/junklet_upgrade_parser.rb', line 40

def inactive; INACTIVE; end

#inactive?Boolean

Returns:

  • (Boolean)


44
# File 'lib/junklet_upgrade_parser.rb', line 44

def inactive?; mode == inactive; end

#junk_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/junklet_upgrade_parser.rb', line 144

def junk_line?(line)
  line =~ /\bSecureRandom\./ || line =~ /\bjunklet\b/
end

#junky?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/junklet_upgrade_parser.rb', line 140

def junky?
  lines.any? { |line| junk_line? line }
end

#maybe_activeObject



41
# File 'lib/junklet_upgrade_parser.rb', line 41

def maybe_active; MAYBE_ACTIVE; end

#maybe_active?Boolean

Returns:

  • (Boolean)


45
# File 'lib/junklet_upgrade_parser.rb', line 45

def maybe_active?; mode == maybe_active; end

#parse_line(line) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/junklet_upgrade_parser.rb', line 96

def parse_line(line)
  numeric_range_match = /(\s*)SecureRandom.(hex|uuid)\[(\d+)\.\.(\d+)\](\s*)/
  range_match = /(\s*)SecureRandom.(hex|uuid)\[(\d+)\.\.(.+?)\](\s*)/
  simple_match = /(\s*)SecureRandom.(hex|uuid)(\s*)/

  # match these in order--simple match will match all complicated matches.
  line \
    .gsub(numeric_range_match) {|s| "#{$1}junk(#{$4.to_i-$3.to_i})#{$5}" } \
    .gsub(range_match) {|s| "#{$1}junk(#{$4})#{$5}" } \
    .gsub(simple_match) {|s| "#{$1}junk#{$3}" }
end

#reordered_blockObject



108
109
110
111
# File 'lib/junklet_upgrade_parser.rb', line 108

def reordered_block
  lets << "\n" unless lets.empty? || lets.last == "\n" || junklets.empty?
  lets + sort_junklets
end

#resetObject



136
137
138
# File 'lib/junklet_upgrade_parser.rb', line 136

def reset
  @lets, @junklets = [], []
end

#sort_junkletsObject



113
114
115
116
117
# File 'lib/junklet_upgrade_parser.rb', line 113

def sort_junklets
  return if junklets.empty?
  indent = junklets.first.indent
  ["#{indent}junklet #{(junklets.map(&:names).sort * ', ')}\n"]
end

#upgraded_lines(lines) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/junklet_upgrade_parser.rb', line 48

def upgraded_lines(lines)
  @lines = lines
  return unless junky?
  we_are_inactive!
  emitted_lines = []
  lines.each do |line|
    self.current_line = imblart_line(line)
    case mode
    when inactive
      if current_line.let?
        lets << parse_line(current_line)
        we_are_maybe_active!
      elsif current_line.junklet?
        junklets << current_line
        we_are_active!
      elsif current_line.code?
        emitted_lines << parse_line(current_line)
      end
    when maybe_active
      if current_line.let?
        lets << parse_line(current_line)
      elsif current_line.junklet?
        junklets << current_line
        we_are_active!
      elsif current_line.code?
        emitted_lines += lets
        emitted_lines << parse_line(current_line)
        we_are_inactive!
      end
    when active
      if current_line.let?
        lets << parse_line(current_line)
      elsif current_line.junklet?
        junklets << current_line
      elsif current_line.code?
        emitted_lines += reordered_block
        emitted_lines << parse_line(current_line)
        reset
        we_are_inactive!
      end
    end
  end

  # if lets || junklets we've hit EOF while active
  emitted_lines += reordered_block if active?
  emitted_lines
end

#we_are_active!Object



132
133
134
# File 'lib/junklet_upgrade_parser.rb', line 132

def we_are_active!
  @mode = ACTIVE
end

#we_are_inactive!Object



123
124
125
126
# File 'lib/junklet_upgrade_parser.rb', line 123

def we_are_inactive!
  reset
  @mode = INACTIVE
end

#we_are_maybe_active!Object



128
129
130
# File 'lib/junklet_upgrade_parser.rb', line 128

def we_are_maybe_active!
  @mode = MAYBE_ACTIVE
end