Class: Bake::Recipe
- Inherits:
-
Object
- Object
- Bake::Recipe
- Defined in:
- lib/bake/recipe.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #arity ⇒ Object
- #call(*arguments, **options) ⇒ Object
- #command ⇒ Object
- #description ⇒ Object
- #explain(context, *arguments, **options) ⇒ Object
-
#initialize(instance, name, method = nil) ⇒ Recipe
constructor
A new instance of Recipe.
- #method ⇒ Object
- #options? ⇒ Boolean
- #parameters ⇒ Object
- #prepare(arguments) ⇒ Object
- #source_location ⇒ Object
- #to_s ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize(instance, name, method = nil) ⇒ Recipe
Returns a new instance of Recipe.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bake/recipe.rb', line 27 def initialize(instance, name, method = nil) @instance = instance @name = name @command = nil @description = nil @types = nil @method = method @arity = nil end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
38 39 40 |
# File 'lib/bake/recipe.rb', line 38 def instance @instance end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/bake/recipe.rb', line 39 def name @name end |
Instance Method Details
#<=>(other) ⇒ Object
41 42 43 |
# File 'lib/bake/recipe.rb', line 41 def <=> other self.source_location <=> other.source_location end |
#arity ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/bake/recipe.rb', line 77 def arity if @arity.nil? @arity = method.parameters.count{|type, name| type == :req} end return @arity end |
#call(*arguments, **options) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/bake/recipe.rb', line 122 def call(*arguments, **) if @instance.send(@name, *arguments, **) else # Ignore options... @instance.send(@name, *arguments) end end |
#command ⇒ Object
69 70 71 |
# File 'lib/bake/recipe.rb', line 69 def command @command ||= compute_command end |
#description ⇒ Object
139 140 141 |
# File 'lib/bake/recipe.rb', line 139 def description @description ||= read_description end |
#explain(context, *arguments, **options) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/bake/recipe.rb', line 131 def explain(context, *arguments, **) if puts "#{self}(#{arguments.join(", ")}, #{.inspect})" else puts "#{self}(#{arguments.join(", ")})" end end |
#method ⇒ Object
45 46 47 |
# File 'lib/bake/recipe.rb', line 45 def method @method ||= @instance.method(@name) end |
#options? ⇒ Boolean
61 62 63 64 65 66 67 |
# File 'lib/bake/recipe.rb', line 61 def if parameters = self.parameters type, name = parameters.last return type == :keyrest || type == :keyreq || type == :key end end |
#parameters ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/bake/recipe.rb', line 53 def parameters parameters = method.parameters unless parameters.empty? return parameters end end |
#prepare(arguments) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/bake/recipe.rb', line 85 def prepare(arguments) offset = 0 ordered = [] = {} parameters = method.parameters.dup types = self.types while argument = arguments.first name, value = argument.split('=', 2) if name and value # Consume it: arguments.shift if type = types[name.to_sym] value = type.parse(value) end [name.to_sym] = value elsif ordered.size < self.arity _, name = parameters.shift value = arguments.shift if type = types[name] value = type.parse(value) end # Consume it: ordered << value else break end end return ordered, end |
#source_location ⇒ Object
49 50 51 |
# File 'lib/bake/recipe.rb', line 49 def source_location self.method.source_location end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/bake/recipe.rb', line 73 def to_s self.command end |
#types ⇒ Object
143 144 145 |
# File 'lib/bake/recipe.rb', line 143 def types @types ||= read_types end |