lib/chusaku/parser.rb in chusaku-0.1.4 vs lib/chusaku/parser.rb in chusaku-0.2.0
- old
+ new
@@ -17,18 +17,19 @@
# action: 'action_name' }
# { type: :code,
# body: 'end # vanilla is the best flavor\n',
# action: nil } ]
#
- # @param {String} path
- # @param {Array<String>} actions
- # @return {Array<Hash>}
+ # @param {String} path - File path to parse
+ # @param {Array<String>} actions - List of valid actions for this route
+ # @return {Hash} Parsed groups of the file and original content
def self.call(path:, actions:)
groups = []
group = {}
+ content = IO.read(path)
- File.open(path, 'r').each_line do |line|
+ content.each_line do |line|
parsed_line = parse_line(line: line, actions: actions)
if group[:type] != parsed_line[:type]
# Now looking at a new group. Push the current group onto the array
# and start a new one.
@@ -40,11 +41,14 @@
end
end
# Push the last group onto the array and return.
groups.push(group)
- groups
+ {
+ content: content,
+ groups: groups
+ }
end
# Given a line and actions, returns the line's type:
#
# 1. comment - A line that is entirely commented. Lines that have trailing
@@ -54,12 +58,12 @@
#
# And give back a Hash in the form:
#
# { type: :action, body: 'def foo', action: 'foo' }
#
- # @param {String} line
- # @param {Array<String>} actions
- # @return {Hash}
+ # @param {String} line - A line of a file
+ # @param {Array<String>} actions - List of valid actions for this route
+ # @return {Hash} Parsed line
def self.parse_line(line:, actions:)
comment_match = /^\s*#.*$/.match(line)
def_match = /^\s*def\s+(\w*)\s*\w*.*$/.match(line)
if !comment_match.nil?