Class: Bake::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/arguments.rb

Overview

Structured access to arguments.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, defaults) ⇒ Arguments

Returns a new instance of Arguments.



19
20
21
22
23
24
25
26
27
28
# File 'lib/bake/arguments.rb', line 19

def initialize(recipe, defaults)
  @recipe = recipe
  
  @types = recipe.types
  @parameters = recipe.parameters
  @arity = recipe.arity
  
  @ordered = []
  @options = defaults
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/bake/arguments.rb', line 31

def options
  @options
end

#orderedObject (readonly)

Returns the value of attribute ordered.



30
31
32
# File 'lib/bake/arguments.rb', line 30

def ordered
  @ordered
end

Class Method Details

.extract(recipe, arguments, **defaults) ⇒ Object



12
13
14
15
16
17
# File 'lib/bake/arguments.rb', line 12

def self.extract(recipe, arguments, **defaults)
  # Only supply defaults that match the recipe option names:
  defaults = defaults.slice(*recipe.required_options)
  
  self.new(recipe, defaults).extract(arguments)
end

Instance Method Details

#extract(arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bake/arguments.rb', line 33

def extract(arguments)
  while argument = arguments.first
    if /^--(?<name>.*)$/ =~ argument
      # Consume the argument:
      arguments.shift
      
      if name.empty?
        break
      end
      
      name = normalize(name)
      
      # Extract the trailing arguments:
      @options[name] = extract_arguments(name, arguments)
    elsif /^(?<name>.*?)=(?<value>.*)$/ =~ argument
      # Consume the argument:
      arguments.shift
      
      name = name.to_sym
      
      # Extract the single argument:
      @options[name] = extract_argument(name, value)
    elsif @ordered.size < @arity
      _, name = @parameters.shift
      value = arguments.shift
      
      # Consume it:
      @ordered << extract_argument(name, value)
    else
      break
    end
  end
  
  return @ordered, @options
end