Class: Taski::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/taski/args.rb

Overview

Runtime arguments accessible from any task. Holds user-defined options passed by the user at execution time. Args is immutable after creation - options cannot be modified during task execution.

Instance Method Summary collapse

Constructor Details

#initialize(options:) ⇒ Args

Returns a new instance of Args.

Parameters:

  • options (Hash)

    User-defined options (immutable after creation)



11
12
13
# File 'lib/taski/args.rb', line 11

def initialize(options:)
  @options = options.dup.freeze
end

Instance Method Details

#[](key) ⇒ Object?

Get a user-defined option value

Parameters:

  • key (Symbol, String)

    The option key

Returns:

  • (Object, nil)

    The option value or nil if not set



18
19
20
# File 'lib/taski/args.rb', line 18

def [](key)
  @options[key]
end

#fetch(key, default = nil) { ... } ⇒ Object

Get a user-defined option value with a default

Parameters:

  • key (Symbol, String)

    The option key

  • default (Object) (defaults to: nil)

    Default value if key is not present

Yields:

  • Block to compute default value if key is not present

Returns:

  • (Object)

    The option value or default



27
28
29
30
31
32
33
34
35
# File 'lib/taski/args.rb', line 27

def fetch(key, default = nil, &block)
  if @options.key?(key)
    @options[key]
  elsif block
    block.call
  else
    default
  end
end

#key?(key) ⇒ Boolean

Check if a user-defined option key exists

Parameters:

  • key (Symbol, String)

    The option key

Returns:

  • (Boolean)

    true if the key exists



40
41
42
# File 'lib/taski/args.rb', line 40

def key?(key)
  @options.key?(key)
end