Module: DSLTasks::TaskMixin

Included in:
DSLTaskContext, Task
Defined in:
lib/dsltasks/dsltasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



87
88
89
# File 'lib/dsltasks/dsltasks.rb', line 87

def method_missing(name, *args, &block)
  run_task(name, *args, block)
end

Instance Attribute Details

#__block__Object (readonly)

Returns the value of attribute __block__.



4
5
6
# File 'lib/dsltasks/dsltasks.rb', line 4

def __block__
  @__block__
end

#__lib_dirs__Object (readonly)

Returns the value of attribute lib_dirs.



8
9
10
# File 'lib/dsltasks/dsltasks.rb', line 8

def __lib_dirs__
  @__lib_dirs__
end

#__lib_stack__Object (readonly)

Returns the value of attribute lib_stack.



7
8
9
# File 'lib/dsltasks/dsltasks.rb', line 7

def __lib_stack__
  @__lib_stack__
end

#__name__Object (readonly)

Returns the value of attribute __name__.



5
6
7
# File 'lib/dsltasks/dsltasks.rb', line 5

def __name__
  @__name__
end

#__parent_task__Object (readonly)

Returns the value of attribute parent_task.



6
7
8
# File 'lib/dsltasks/dsltasks.rb', line 6

def __parent_task__
  @__parent_task__
end

Instance Method Details

#__caller__Object



29
30
31
# File 'lib/dsltasks/dsltasks.rb', line 29

def __caller__()
  (DSLTasks::task_stack(@__root__))[-2]
end

#__exec__(calling_task, args, block) ⇒ Object



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

def __exec__(calling_task, args, block)
  rvalue = nil
  task_stack = DSLTasks::task_stack(@__root__)
  task_stack.push self
  unless block.nil?
    args << block
  end
  rvalue = instance_exec(*args, &@__block__)
  task_stack.pop
  return rvalue
end

#__initialize_task_mixin__(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/dsltasks/dsltasks.rb', line 9

def __initialize_task_mixin__(opts={})
  @__parent_task__ = opts[:parent]
  @__name__ = opts[:name]
  @__block__ = opts[:block]
  @__lib_stack__ = opts[:lib_stack]
  @__lib_dirs__ = opts[:lib_dirs] || (opts[:parent] ? opts[:parent].__lib_dirs__ : [])
  @__root__ = opts[:root] || self
  @__tasks__ = Hash.new
end

#lib(lib_name) ⇒ Object



32
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/dsltasks/dsltasks.rb', line 32

def lib(lib_name)
  lib_name = lib_name.to_s
  if lib_name.start_with?('/')
    if File.exist?(lib_name)
      unless @__lib_stack__.include?(lib_name)
        @__lib_stack__.push(File.expand_path(lib_name))
        instance_eval(File.read(lib_name), File.expand_path(lib_name))
        @__lib_stack__.pop
      end
    else
      raise "Lib file not found: #{lib_name}"
    end
  else
    # search path if not absolute:
    #   directory of file of task from which lib was called
    #   directories in @__lib_dirs__, in order
    dirnames = [File.dirname(@__lib_stack__[-1]), *@__lib_dirs__]
    fnames = dirnames.map {|d| File.join(d, lib_name)}
    fnames.map! {|f| f.end_with?('.rb') ? f : f+'.rb'}
    found_lib = false
    fnames.each do |fname|
      if File.exist?(fname)
        unless @__lib_stack__.include?(fname)
          @__lib_stack__.push(File.expand_path(fname))
          instance_eval(File.read(fname), File.expand_path(fname))
          @__lib_stack__.pop
        end
        found_lib = true
        break
      end
    end
    if !found_lib
      raise "Lib file not found in possible locations: #{fnames.join(',')}"
    end
  end
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
# File 'lib/dsltasks/dsltasks.rb', line 90

def respond_to_missing?(name, *args)
  if @__tasks__.has_key?(name.to_sym)
    return true
  elsif @__parent_task__
    return @__parent_task__.respond_to_missing?(name, *args)
  else
    return false
  end
end

#run_task(name, *args, block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/dsltasks/dsltasks.rb', line 68

def run_task(name, *args, block)
  if @__tasks__.has_key?(name.to_sym)
    task = @__tasks__[name.to_sym]
    return task.__exec__(self, args, block)
  elsif @__parent_task__
    return @__parent_task__.run_task(name, *args, block)
  else
    raise "Unrecognized task: #{name}"
  end
end

#task(name, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/dsltasks/dsltasks.rb', line 78

def task(name, &block)
  if block.nil?
    if @__tasks__.has_key?(name.to_sym)
      return @__tasks__[name.to_sym]
    end
  else
    @__tasks__[name.to_sym] = Task.new(parent: self, lib_stack: @__lib_stack__.clone, name: name.to_sym, block: block, root: @__root__)
  end
end