Class: Reek::Source
- Defined in:
- lib/reek/source.rb,
lib/reek/spec.rb,
lib/reek/object_source.rb
Overview
A Source
object represents a chunk of Ruby source code.
The various class methods are factories that will create Source
instances from various types of input.
Direct Known Subclasses
Class Method Summary collapse
-
.from_f(file) ⇒ Object
Factory method: creates a
Source
object by reading Ruby code from Filefile
. -
.from_io(ios, desc) ⇒ Object
Factory method: creates a
Source
object by reading Ruby code from theIO
stream. -
.from_object(obj) ⇒ Object
Factory method: creates a
Source
from obj. -
.from_path(filename) ⇒ Object
Factory method: creates a
Source
object by reading Ruby code from the named file. -
.from_pathlist(paths) ⇒ Object
Factory method: creates a
Source
object from an array of file paths. -
.from_s(code) ⇒ Object
Factory method: creates a
Source
object by reading Ruby code from thecode
string.
Instance Method Summary collapse
-
#full_report ⇒ Object
Creates a formatted report of all the
Smells::SmellWarning
objects recorded in this report, with a heading. - #generate_syntax_tree ⇒ Object
-
#has_smell?(smell_class, patterns) ⇒ Boolean
Checks this source for instances of
smell_class
, and returnstrue
only if one of them has a report string matching all of thepatterns
. -
#initialize(code, desc, dir = nil) ⇒ Source
constructor
:nodoc:.
-
#report ⇒ Object
Returns a
Report
listing the smells found in this source. - #smelly? ⇒ Boolean
- #to_s ⇒ Object
- #to_source ⇒ Object
Constructor Details
#initialize(code, desc, dir = nil) ⇒ Source
:nodoc:
61 62 63 64 65 66 |
# File 'lib/reek/source.rb', line 61 def initialize(code, desc, dir = nil) # :nodoc: @source = code @desc = desc @cf = SmellConfig.new @cf = @cf.load_local(dir) if dir end |
Class Method Details
.from_f(file) ⇒ Object
Factory method: creates a Source
object by reading Ruby code from File file
. The source code is not parsed until report
is called.
39 40 41 |
# File 'lib/reek/source.rb', line 39 def self.from_f(file) from_path(file.path) end |
.from_io(ios, desc) ⇒ Object
Factory method: creates a Source
object by reading Ruby code from the IO
stream. The stream is consumed upto end-of-file, but the source code is not parsed until report
is called. desc
provides a string description to be used in the header of formatted reports.
22 23 24 25 |
# File 'lib/reek/source.rb', line 22 def self.from_io(ios, desc) code = ios.readlines.join return new(code, desc) end |
.from_object(obj) ⇒ Object
Factory method: creates a Source
from obj. The code is not parsed until report
is called. (This feature is only enabled if you have the ParseTree gem installed.)
8 9 10 |
# File 'lib/reek/object_source.rb', line 8 def self.from_object(obj) return ObjectSource.new(obj, obj.to_s) end |
.from_path(filename) ⇒ Object
Factory method: creates a Source
object by reading Ruby code from the named file. The source code is not parsed until report
is called.
47 48 49 50 |
# File 'lib/reek/source.rb', line 47 def self.from_path(filename) code = IO.readlines(filename).join return new(code, filename, File.dirname(filename)) end |
.from_pathlist(paths) ⇒ Object
Factory method: creates a Source
object from an array of file paths. No source code is actually parsed until the report is accessed.
56 57 58 59 |
# File 'lib/reek/source.rb', line 56 def self.from_pathlist(paths) sources = paths.map {|path| Source.from_path(path) } SourceList.new(sources) end |
.from_s(code) ⇒ Object
Factory method: creates a Source
object by reading Ruby code from the code
string. The code is not parsed until report
is called.
31 32 33 |
# File 'lib/reek/source.rb', line 31 def self.from_s(code) return new(code, 'string') end |
Instance Method Details
#full_report ⇒ Object
Creates a formatted report of all the Smells::SmellWarning
objects recorded in this report, with a heading.
100 101 102 |
# File 'lib/reek/source.rb', line 100 def full_report report.full_report(@desc) end |
#generate_syntax_tree ⇒ Object
68 69 70 |
# File 'lib/reek/source.rb', line 68 def generate_syntax_tree RubyParser.new.parse(@source, @desc) || s() end |
#has_smell?(smell_class, patterns) ⇒ Boolean
Checks this source for instances of smell_class
, and returns true
only if one of them has a report string matching all of the patterns
.
94 95 96 |
# File 'lib/reek/source.rb', line 94 def has_smell?(smell_class, patterns) report.any? { |smell| smell.matches?(smell_class, patterns) } end |
#report ⇒ Object
Returns a Report
listing the smells found in this source. The first call to report
parses the source code and constructs a list of SmellWarnings found; subsequent calls simply return this same list.
77 78 79 80 81 82 83 84 |
# File 'lib/reek/source.rb', line 77 def report unless @report @report = Report.new parser = CodeParser.new(@report, @cf.smell_listeners) parser.process(generate_syntax_tree) end @report end |
#smelly? ⇒ Boolean
86 87 88 |
# File 'lib/reek/source.rb', line 86 def smelly? report.length > 0 end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/reek/source.rb', line 104 def to_s @desc end |
#to_source ⇒ Object
136 137 138 |
# File 'lib/reek/spec.rb', line 136 def to_source self end |