# 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.moduleBakeclassRecipedefinitialize(scope,name,method=nil)@scope=scope@name=name@command=nil@description=nil@method=methodendattr:scopeattr:namedefmethod@method||=@scope.method(@name)enddefparametersparameters=method.parametersunlessparameters.empty?returnparametersendenddefoptions?ifparameters=self.parameterstype,name=self.parameters.lastreturntype==:keyrest||type==:keyreq||type==:keyendenddefcommand@command||=compute_commandenddefto_sself.commandenddefaritymethod.arityenddefprepare(arguments)offset=0ordered=[]options={}whileargument=arguments.firstname,value=argument.split('=',2)ifnameandvalue# Consume it:arguments.shiftoptions[name.to_sym]=valueelsifordered.size<self.arity# Consume it:ordered<<arguments.shiftelsebreakendendreturnordered,optionsenddefcall(*arguments,**options)ifoptions?@scope.send(@name,*arguments,**options)else# Ignore options...@scope.send(@name,*arguments)endenddefexplain(context,*arguments,**options)ifoptions?puts"#{self}(#{arguments.join(", ")}, #{options.inspect})"elseputs"#{self}(#{arguments.join(", ")})"endenddefdescription@description||=read_descriptionendprivatedefcompute_commandpath=@scope.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-=1endreturndescriptionendendend