class Restwoods::LineParser
Constants
- COMMANDS
- PICKS
Public Class Methods
new(str, clazz)
click to toggle source
# File lib/restwoods/line_parser.rb, line 7 def initialize(str, clazz) @clazz = clazz pick = PICKS[clazz] @str = pick.nil? ? str : str.gsub(pick, '') end
Public Instance Methods
indentation()
click to toggle source
# File lib/restwoods/line_parser.rb, line 23 def indentation @indentation ||= @str.match(/\A\s*/)[0].length end
parse()
click to toggle source
# File lib/restwoods/line_parser.rb, line 13 def parse parts = @str.strip.split(/\s+/) command = parts[0].to_s.match(COMMANDS) if command.nil? || command[1].nil? { type: :joint, text: @str } else send(command[1], parts[1..-1], command) end end
Protected Instance Methods
analyze_arguments(args)
click to toggle source
# File lib/restwoods/line_parser.rb, line 66 def analyze_arguments(args) { group: args[0].to_s.match(/\((\w+)\)/) }.tap do |r| r[:type] = (r[:group].nil? ? args[0] : args[1]).to_s.match(/\{(.+)\}/) r[:names] = args[[r[:group], r[:type]].compact.length].to_s r[:name] = r[:names].match(/\A\[(.+)\]\Z/) r[:default] = (r[:name].nil? ? r[:names] : r[:name][1]).split("=") r[:parent] = r[:default][0].split(".") end end
cmd(args, cmd)
click to toggle source
# File lib/restwoods/line_parser.rb, line 29 def cmd(args, cmd) { type: :cmd, part: cmd[1].split("_")[1].to_sym, data: { name: args[0] } } end
doc(args, _cmd)
click to toggle source
# File lib/restwoods/line_parser.rb, line 33 def doc(args, _cmd) { type: :doc, part: :main, data: {} }.tap do |result| m = args[0].match(/\((\w+)\)/) result[:data][:summary] = args[[m].compact.length..-1].join(" ") result[:data][:name] = m[1] unless m.nil? end end
res(args, _cmd)
click to toggle source
# File lib/restwoods/line_parser.rb, line 41 def res(args, _cmd) { type: :res, part: :main, data: { method: args[0], route: args[1], summary: args[2..-1].join(" ") } } end
res_bind(args, _cmd)
click to toggle source
# File lib/restwoods/line_parser.rb, line 49 def res_bind(args, _cmd) { type: :res, part: :bind, data: {} }.tap do |result| m = args[0].match(/\((param|header|return|error)\)/) result[:data][:scope] = m[1] unless m.nil? result[:data][:command] = args[[m].compact.length] result[:data][:vars] = args[([m].compact.length + 1)..-1] end end
res_io(args, cmd)
click to toggle source
# File lib/restwoods/line_parser.rb, line 76 def res_io(args, cmd) { type: :res, part: cmd[4].to_sym, data: {} }.tap do |result| r = analyze_arguments(args) unless r[:type].nil? options = r[:type][1].split("=") array = options[0].match(/(\w+)(\[\])?\Z/) result[:data][:options] = options[1].split(",") if options.length == 2 unless array.nil? result[:data][:array] = !array[2].nil? result[:data][:type] = array[1] end end result[:data][:group] = r[:group][1] unless r[:group].nil? result[:data][:required] = r[:name].nil? result[:data][:default] = r[:default][1] if r[:default].length == 2 result[:data][:parent] = r[:parent][0..-2] if r[:parent].length > 1 result[:data][:name] = r[:parent].length == 1 ? r[:default][0] : r[:parent][-1] result[:data][:summary] = args[([r[:group], r[:type]].compact.length + 1)..-1].join(' ') end end