Module: Tap::Declarations

Included in:
Context
Defined in:
lib/tap/declarations.rb,
lib/tap/declarations/context.rb,
lib/tap/declarations/description.rb

Defined Under Namespace

Classes: Context, Description

Instance Method Summary collapse

Instance Method Details

#baseclass(baseclass = Tap::Task) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/tap/declarations.rb', line 44

def baseclass(baseclass=Tap::Task)
  current = @baseclass
  begin
    @baseclass = env.constant(baseclass) unless baseclass.nil?
    yield if block_given?
  ensure
    @baseclass = current if block_given?
  end
end

#declare(baseclass, const_name, configs = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tap/declarations.rb', line 74

def declare(baseclass, const_name, configs={}, &block)
  const_name = const_name.to_s.camelize
  subclass = Class.new(env.constant(baseclass))
  @namespace.const_set(const_name, subclass)
  
  # define configs
  configs.each_pair do |key, value|
    # specifying a desc prevents lazydoc registration of these lines
    opts = {:desc => ""}
    opts[:short] = key if key.to_s.length == 1
    config_block = Configurable::Validation.guess(value)
    subclass.send(:config, key, value, opts, &config_block)
  end
  
  # define process
  if block
    # determine arity, correcting for the self arg
    arity = block.arity
    arity -= arity > 0 ? 1 : -1
    signature = Array.new(arity < 0 ? arity.abs - 1 : arity, 'arg')
    signature << '*args' if arity < 0
    
    # prevents assessment of process args by lazydoc
    subclass.const_attrs[:process] = signature.join(' ')
    subclass.send(:define_method, :process) do |*args|
      block.call(self, *args)
    end
  end
  
  # register documentation
  constant = env.set(subclass, nil)
  
  if @desc
    subclass.desc = @desc
    constant.register_as(subclass.type, @desc)
    @desc = nil
  end
  
  subclass
end

#desc(str) ⇒ Object

Sets the description for use by the next task declaration.



34
35
36
37
38
# File 'lib/tap/declarations.rb', line 34

def desc(str)
  @desc = Lazydoc.register_caller(Description)
  @desc.desc = str
  @desc
end

#envObject



10
11
12
# File 'lib/tap/declarations.rb', line 10

def env
  app.env
end

#join(inputs, outputs, config = {}, clas = Tap::Join, &block) ⇒ Object

Generates a join between the inputs and outputs. Join resolves the class using env and initializes a new instance with the configs and self.



24
25
26
27
28
29
30
31
# File 'lib/tap/declarations.rb', line 24

def join(inputs, outputs, config={}, clas=Tap::Join, &block)
  inputs  = [inputs]  unless inputs.kind_of?(Array)
  outputs = [outputs] unless outputs.kind_of?(Array)
  
  obj = app.init(clas, config, app)
  obj.join(inputs, outputs, &block)
  obj
end

#namespace(namespace) ⇒ Object

Nests tasks within the named module for the duration of the block. Namespaces may be nested.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tap/declarations.rb', line 56

def namespace(namespace)
  current = @namespace
  begin
    unless namespace.nil? || namespace.kind_of?(Module)
      const_name = namespace.to_s.camelize
      unless current.const_defined?(const_name)
        current.const_set(const_name, Module.new)
      end
      namespace = current.const_get(const_name)
    end
    
    @namespace = namespace unless namespace.nil?
    yield if block_given?
  ensure
    @namespace = current if block_given?
  end
end

#node(var = nil, &node) ⇒ Object

Returns a new node that executes block on call.



15
16
17
18
19
# File 'lib/tap/declarations.rb', line 15

def node(var=nil, &node) # :yields: *args
  def node.joins; @joins ||= []; end
  app.set(var, node) if var
  node
end

#singleton(&block) ⇒ Object



40
41
42
# File 'lib/tap/declarations.rb', line 40

def singleton(&block)
  baseclass(Tap::Tasks::Singleton, &block)
end

#task(const_name, configs = {}, baseclass = @baseclass, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tap/declarations.rb', line 115

def task(const_name, configs={}, baseclass=@baseclass, &block)
  @desc ||= Lazydoc.register_caller(Description)
  const_name, prerequisites = parse_prerequisites(const_name)

  if prerequisites.nil?
    return declare(baseclass, const_name, configs, &block)
  end

  tasc = declare(Tap::Workflow, const_name, configs) do |workflow|
    psr = Parser.new
    args = psr.parse!(prerequisites)
    warn "ignoring args: #{args.inspect}" unless args.empty?
    psr.build_to(app)
    
    obj = init("#{const_name.to_s.underscore}/task", workflow.config.to_hash)
    setup = lambda {|input| exe(obj, input) }
    
    [setup, obj]
  end

  namespace(const_name) do
    declare(baseclass, 'Task', configs, &block)
  end

  tasc
end

#work(const_name, definition, configs = {}, baseclass = Tap::Workflow, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/tap/declarations.rb', line 142

def work(const_name, definition, configs={}, baseclass=Tap::Workflow, &block)
  unless definition.kind_of?(String)
    raise "workflow definition must be a string: #{definition.inspect}"
  end
  
  @desc ||= Lazydoc.register_caller(Description)
  block ||= lambda {|config| node(0) }
  task({const_name => definition}, configs, baseclass, &block)
end