# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require_relative'types'moduleBakePARAMETER=/@param\s+(?<name>.*?)\s+\[(?<type>.*?)\]\s+(?<details>.*?)\Z/classRecipedefinitialize(instance,name,method=nil)@instance=instance@name=name@command=nil@description=nil@types=nil@method=method@arity=nilendattr:instanceattr:namedef<=>otherself.source_location<=>other.source_locationenddefmethod@method||=@instance.method(@name)enddefsource_locationself.method.source_locationenddefparametersparameters=method.parametersunlessparameters.empty?returnparametersendenddefoptions?ifparameters=self.parameterstype,name=parameters.lastreturntype==:keyrest||type==:keyreq||type==:keyendenddefcommand@command||=compute_commandenddefto_sself.commandenddefarityif@arity.nil?@arity=method.parameters.count{|type,name|type==:req}endreturn@arityenddefprepare(arguments)offset=0ordered=[]options={}parameters=method.parameters.duptypes=self.typeswhileargument=arguments.firstname,value=argument.split('=',2)ifnameandvalue# Consume it:arguments.shiftiftype=types[name.to_sym]value=type.parse(value)endoptions[name.to_sym]=valueelsifordered.size<self.arity_,name=parameters.shiftvalue=arguments.shiftiftype=types[name]value=type.parse(value)end# Consume it:ordered<<valueelsebreakendendreturnordered,optionsenddefcall(*arguments,**options)ifoptions?@instance.send(@name,*arguments,**options)else# Ignore options...@instance.send(@name,*arguments)endenddefexplain(context,*arguments,**options)ifoptions?puts"#{self}(#{arguments.join(", ")}, #{options.inspect})"elseputs"#{self}(#{arguments.join(", ")})"endenddefdescription@description||=read_descriptionenddeftypes@types||=read_typesendprivatedefcompute_commandpath=@instance.pathifpath.empty?@name.to_selsifpath.last.to_sym==@namepath.join(':')else(path+[@name]).join(':')endenddefread_descriptionfile,line_number=self.method.source_locationlines=File.readlines(file)line_index=line_number-1# Legacy "recipe" syntax:ifmatch=lines[line_index].match(/description: "(.*?)"/)return[match[1]]enddescription=[]line_index-=1# Extract comment preceeding method:whileline=lines[line_index]ifmatch=line.match(/^\s*\#\s?(.*?)$/)description.unshift(match[1])elsebreakendline_index-=1endreturndescriptionenddefread_typestypes={}description.eachdo|description|iffields=PARAMETER.match(description)types[fields[:name].to_sym]=Types.parse(fields[:type])endendreturntypesendendend