Class: Society::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/society/parser.rb

Overview

The Parser class is responsible for producing an ObjectGraph from one or more ruby sources.

Defined Under Namespace

Classes: AREdge, NSNode

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Parser

Public: Create a Parser, staging ruby source files to be analyzed.

source - An Enumerable containing ruby source strings.



34
35
36
# File 'lib/society/parser.rb', line 34

def initialize(source)
  @source = source.map { |file| graph_from(file) }
end

Class Method Details

.for_files(*file_paths) ⇒ Object

Public: Generate a list of files from a collection of paths, creating a new Parser with them. Note: Since the files are not read, a new Parser MAY be returned such that initiating processing will cause a crash later.

file_paths - Any number of Strings representing paths to files.

Returns a Parser.



15
16
17
18
19
20
# File 'lib/society/parser.rb', line 15

def self.for_files(*file_paths)
  files = file_paths.flatten.flat_map do |path|
    File.directory?(path) ? Dir.glob(File.join(path, '**', '*.rb')) : path
  end
  new(files.lazy.map { |f| File.read(f) })
end

.for_source(*source) ⇒ Object

Public: Create a Parser with a collection of ruby sources to be analyzed.

source - Any number of Strings containing ruby source.

Returns a Parser.



27
28
29
# File 'lib/society/parser.rb', line 27

def self.for_source(*source)
  new(source.lazy)
end

Instance Method Details

#classesObject

Public: Return a list of known classes from the object graph.

Returns an Array of Strings.



63
64
65
# File 'lib/society/parser.rb', line 63

def classes
  graph.map(&:name)
end

#graphObject

Public: Return the ObjectGraph representing the analyzed source. Calling this method will trigger the analysis of the source if the object was created with lazy enumerables.

Returns an ObjectGraph.



56
57
58
# File 'lib/society/parser.rb', line 56

def graph
  @graph ||= resolve_known_edges(source.reduce(ObjectGraph.new, &:+))
end

#report(format, output_path = nil) ⇒ Object

Public: Generate a report from the object graph.

format - A symbol representing any known output format. output_path - Path to which output should be written. (default: nil)

Returns nothing.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/society/parser.rb', line 44

def report(format, output_path=nil)
  raise ArgumentError, "Unknown format #{format}" unless known_formats.include?(format)
  options = { json_data: graph.to_json }
  options[:output_path] = output_path unless output_path.nil?
  FORMATTERS[format].new(options).write
end