Class: Caty::TaskHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/caty/task_hash.rb

Overview

A hash that preserves the order of task insertion.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TaskHash

Works like Hash#initialize.



15
16
17
18
19
# File 'lib/caty/task_hash.rb', line 15

def initialize( *args )
    @ary  = Array.new
    @hash = Hash.new(*args)
    super(@hash)
end

Instance Method Details

#[](num_or_key) ⇒ Object

Works like Hash#[].



24
25
26
27
28
29
# File 'lib/caty/task_hash.rb', line 24

def []( num_or_key )
    case num_or_key
    when Numeric, Range then @ary[num_or_key]
    else @hash[num_or_key]
    end
end

#[]=(key, value) ⇒ Object

Works like Hash#[]=.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/caty/task_hash.rb', line 34

def []=( key, value )
    index = @ary.find { |item| item == @hash[key] }

    if @hash[key].nil? or index.nil?
        @ary << value
    else
        @ary[index] = value
    end

    @hash[key] = value
end

#resolve(task_name) ⇒ Object

Follows indirections until the task behind the given task name is discovered.



50
51
52
53
54
55
56
# File 'lib/caty/task_hash.rb', line 50

def resolve( task_name )
    task = self[task_name]

    if task.nil? then nil
    else task.resolve(self)
    end
end

#to_aObject

Returns a copy of the task hash as an array. The order of the items in the array is the same as in the task hash.



63
64
65
# File 'lib/caty/task_hash.rb', line 63

def to_a
    @ary.dup
end

#to_hObject

Returns a copy of the task hash as a plain hash.



70
71
72
# File 'lib/caty/task_hash.rb', line 70

def to_h
    @hash.dup
end