Class: Lazibi::Filter::OptionalDo

Inherits:
FilterBase show all
Defined in:
lib/filter/optional_do_filter.rb

Instance Method Summary collapse

Methods included from Helper::FilterHelper

#begin_keys, #clean_block, #clean_line, #comment_at_end, #end_anchor?, #end_keys, #get_indent, #get_rest, #group_match, #middle_anchor?, #middle_keys, #nasty_line?, #split_end, #start_anchor?

Instance Method Details

#down(source) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/filter/optional_do_filter.rb', line 29

def down(source)
  lines = source.split("\n")
  lines.each_index do |index|
    l = lines[index]
    l = optional_do_filter_to_rb(lines, index)
    lines[index] = l
  end
  lines.join "\n"
end

#optional_do_filter_to_py(lines, index) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/filter/optional_do_filter.rb', line 16

def optional_do_filter_to_py(lines, index)
  l = lines[index]
  return l if l.strip == ''
  return l if l.strip[0..0] == '#'
  l_check = clean_line(l).strip
  if l_check.size > 2 && l.strip[-3..-1] == ' do' && !nasty_line?(l_check)
    l = l.rstrip[0...-3].rstrip
    return l
  end
  return l
end

#optional_do_filter_to_rb(lines, index) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/filter/optional_do_filter.rb', line 39

def optional_do_filter_to_rb(lines, index)
  l = lines[index]
  return l if l.strip == ''
  return l if l =~ /[\{\[]\s*$/
  rest = get_rest(l)
  return l if start_anchor?(rest) || middle_anchor?(rest) || end_anchor?(rest)

  l_check = clean_line l
  next_line = ''
  if index + 1 < lines.size
    lines[index+1..-1].each_index do |i|
      abs_i = index + 1 + i
      next if lines[abs_i].strip == ''
      next_line = lines[abs_i]
      break
    end
  else
    return l
  end
  if get_indent(next_line) > get_indent(l)
    return l.rstrip + ' do'
  else
    return l
  end
end

#up(source) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/filter/optional_do_filter.rb', line 6

def up(source)
  lines = source.split("\n")
  lines.each_index do |index|
    l = lines[index]
    l = optional_do_filter_to_py( lines, index )
    lines[index] = l
  end
  lines.join "\n"
end