Class: DRG::FileContext
- Inherits:
-
Object
show all
- Defined in:
- lib/drg/file_context.rb
Defined Under Namespace
Modules: NullDefinition
Classes: BlockDef, ClassDef, ClassMethodDef, Definition, InstanceMethodDef, MethodDefinition, ModuleDef, NullLine
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FileContext.
9
10
11
12
|
# File 'lib/drg/file_context.rb', line 9
def initialize
@lines = Set.new
@is_private = false
end
|
Instance Attribute Details
#lines ⇒ Object
25
26
27
|
# File 'lib/drg/file_context.rb', line 25
def lines
@lines.to_a
end
|
Instance Method Details
#add(raw_line, index = 0) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/drg/file_context.rb', line 14
def add(raw_line, index = 0)
line_number = index + 1
line = parse_line(raw_line, line_number)
@lines << line
line
end
|
#blocks ⇒ Object
91
92
93
|
# File 'lib/drg/file_context.rb', line 91
def blocks
lines.select { |line| line.block? }
end
|
#classes ⇒ Object
83
84
85
|
# File 'lib/drg/file_context.rb', line 83
def classes
lines.select { |line| line.class? }
end
|
#close_current_definition ⇒ Object
74
75
76
|
# File 'lib/drg/file_context.rb', line 74
def close_current_definition
current_definition.close
end
|
#current_class_or_module ⇒ Object
78
79
80
81
|
# File 'lib/drg/file_context.rb', line 78
def current_class_or_module
current_definition = lines.reverse.detect { |line| line.open? && (line.class? || line.mod?) }
current_definition && current_definition.name
end
|
#current_definition ⇒ Object
70
71
72
|
# File 'lib/drg/file_context.rb', line 70
def current_definition
lines.reverse.detect { |line| line.open? } || Definition.new
end
|
#inspect ⇒ Object
21
22
23
|
# File 'lib/drg/file_context.rb', line 21
def inspect
%Q(#<DRG::FileContext:#{object_id} @lines=#{@lines.count}>, @is_private=false, @is_protected=false>)
end
|
#last_class ⇒ Object
103
104
105
|
# File 'lib/drg/file_context.rb', line 103
def last_class
lines.reverse.detect { |line| line.class? }
end
|
#last_method ⇒ Object
99
100
101
|
# File 'lib/drg/file_context.rb', line 99
def last_method
lines.reverse.detect { |line| line.method? }
end
|
#methods ⇒ Object
95
96
97
|
# File 'lib/drg/file_context.rb', line 95
def methods
lines.select { |line| line.method? }
end
|
#modules ⇒ Object
87
88
89
|
# File 'lib/drg/file_context.rb', line 87
def modules
lines.select { |line| line.mod? }
end
|
#parse_line(line, line_number) ⇒ Object
def set_name *args def get_name def [](other) def + def - def <=> def / def eql?
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/drg/file_context.rb', line 41
def parse_line(line, line_number)
case line.to_s.chomp.strip
when /\A(=begin|=end)/
nil
when /class\s+([A-Z]+\w*)/
return ClassDef.new($1, line_number)
when /module\s+([A-Z]+\w*)/
return ModuleDef.new($1, line_number)
when /def self\.([a-z0-9\[\]_+-<=>?]+)\s*\(?/
return ClassMethodDef.new($1, line_number, current_class_or_module, @is_private)
when /def ([a-z0-9\[\]_+-<=>?]+)\s*\(?/
return InstanceMethodDef.new($1, line_number, current_class_or_module, @is_private)
when /(.+)\s*(do|\{)(.*)(\}|end)/
return BlockDef.new($1, $2, $3, line_number, $4)
when /(.+)\s*(do|\{)(.*)/
return BlockDef.new($1, $2, $3, line_number)
when /#*\s*(private|protected)\b/i
@is_private = true
when /#*\s*public[\s\n]+/i
@is_private = false
when /\A(end|\})\b/i
close_current_definition unless current_definition.method?
else
nil
end
NullLine.new(line)
end
|