Module: Sparehand::CommentBlock

Defined in:
lib/sparehand/comment_block.rb

Constant Summary collapse

@@operation =
:toggle
@@comment =
'#'

Class Method Summary collapse

Class Method Details

.ignore(line, *extra) ⇒ Object



32
33
34
# File 'lib/sparehand/comment_block.rb', line 32

def self.ignore line, *extra
  line
end

.process(input, tag, operation = @@operation, comment = @@comment, output = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sparehand/comment_block.rb', line 8

def self.process input, tag, operation = @@operation, comment = @@comment, output = nil
   
  unless output 
    output = StringIO.new('')
    return_output = true
  end
  
  state = :ignore
  
  input.each_line do |line|
    if line.strip == "#{comment}END #{tag}"
      state = :ignore
    end
   
    output.puts send(state, line, operation, comment)
    
    if line.strip == "#{comment}BEGIN #{tag}"
      state = :process_line
    end
  end
  
  return output.string if return_output
end

.process_line(line, operation = @@operation, comment = @@comment) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sparehand/comment_block.rb', line 36

def self.process_line line, operation = @@operation, comment = @@comment
  if line =~ /^#{comment}/
    if operation == :comment
      line
    else
      line[comment.length..-1]
    end
  else
    if operation == :uncomment
      line
    else
      comment + line
    end
  end
end