Class: Document

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Document

Returns a new instance of Document.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mark_set_go/document.rb', line 4

def initialize(lines)
  @sections = []
  @lines = lines

  @current_section = nil
  @lines.each do |line|
    if @current_section.nil?
      if line.strip == "" # skip initial whitespace
        next
      elsif comment_check(line)
        @current_section = Comment.new(line)
      else
        @current_section = Code.new(line)
      end

    elsif @current_section.class == Code
      if comment_check(line)
        push_current_section
        @current_section = Comment.new(line)
      else
        @current_section.lines << line
      end

    elsif @current_section.class == Comment
      if @current_section.type == "block"
        if check_block_end(line)
          push_current_section
        else
          @current_section.lines << line
        end
      elsif @current_section.type == "line"
        if check_block_start(line)
          push_current_section
          @current_section = Comment.new(line)
        else
          if comment_check(line)
            @current_section.lines << line
          elsif line.strip == ""
            @current_section.lines << line
          else
            push_current_section
            @current_section = Code.new(line)
          end
        end
      end
    end
  end
  if !@current_section.nil?
    push_current_section
  end
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



2
3
4
# File 'lib/mark_set_go/document.rb', line 2

def lines
  @lines
end

#sectionsObject

Returns the value of attribute sections.



2
3
4
# File 'lib/mark_set_go/document.rb', line 2

def sections
  @sections
end

Instance Method Details

#check_block_end(line) ⇒ Object



64
65
66
# File 'lib/mark_set_go/document.rb', line 64

def check_block_end(line)
  line.strip.end_with? "*/"
end

#check_block_start(line) ⇒ Object



60
61
62
# File 'lib/mark_set_go/document.rb', line 60

def check_block_start(line)
  line.strip.start_with? "/*"
end

#comment_check(line) ⇒ Object



56
57
58
# File 'lib/mark_set_go/document.rb', line 56

def comment_check(line)
  ["/*", "//"].map { |str| line.strip.start_with? str }.include? true
end

#push_current_sectionObject



68
69
70
71
# File 'lib/mark_set_go/document.rb', line 68

def push_current_section
  @sections << @current_section.dup
  @current_section = nil
end

#to_sObject



73
74
75
# File 'lib/mark_set_go/document.rb', line 73

def to_s
  @sections.map(&:to_s).join("\n").strip
end