Class: Taski::Context

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

Overview

Runtime context accessible from any task. Holds user-defined options and execution metadata. Context is immutable after creation - options cannot be modified during task execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, root_task:) ⇒ Context

Returns a new instance of Context.

Parameters:

  • options (Hash)

    User-defined options (immutable after creation)

  • root_task (Class)

    The root task class that initiated execution



14
15
16
17
18
19
# File 'lib/taski/context.rb', line 14

def initialize(options:, root_task:)
  @options = options.dup.freeze
  @root_task = root_task
  @started_at = Time.now
  @working_directory = Dir.pwd
end

Instance Attribute Details

#root_taskObject (readonly)

Returns the value of attribute root_task.



10
11
12
# File 'lib/taski/context.rb', line 10

def root_task
  @root_task
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/taski/context.rb', line 10

def started_at
  @started_at
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



10
11
12
# File 'lib/taski/context.rb', line 10

def working_directory
  @working_directory
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



24
25
26
# File 'lib/taski/context.rb', line 24

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



33
34
35
36
37
38
39
40
41
# File 'lib/taski/context.rb', line 33

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



46
47
48
# File 'lib/taski/context.rb', line 46

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