lib/tap/support/declarations.rb in bahuvrihi-tap-0.10.2 vs lib/tap/support/declarations.rb in bahuvrihi-tap-0.10.3
- old
+ new
@@ -14,75 +14,52 @@
end
def self.extended(base)
set_declaration_base(base)
end
-
- def tasc(name, configs={}, options={}, &block)
- Tap::Task.subclass(nest(name), configs, options, &block)
+
+ def tasc(name, configs={}, &block)
+ declare(Tap::Task, name, configs, &block)
end
- def task(name, configs={}, options={}, &block)
- options[:arity] = arity(block)
- tasc(name, configs, options, &task_block(block)).new
+ def task(name, configs={}, &block)
+ mod_declare(Tap::Task, name, configs, &block)
end
- def file_tasc(name, configs={}, options={}, &block)
- Tap::FileTask.subclass(nest(name), configs, options, &block)
+ def file_tasc(name, configs={}, &block)
+ declare(Tap::FileTask, name, configs, &block)
end
- def file_task(name, configs={}, options={}, &block)
- options[:arity] = arity(block)
- file_tasc(nest(name), configs, options, &task_block(block)).new
+ def file_task(name, configs={}, &block)
+ mod_declare(Tap::FileTask, name, configs, &block)
end
-
- def worcflow(name, configs={}, options={}, &block)
- Tap::Workflow.subclass(nest(name), configs, options, &block)
- end
-
- def workflow(name, configs={}, options={}, &block)
- options[:arity] = arity(block)
- worcflow(name, configs, options, &task_block(block)).new
- end
-
+
protected
def config(key, value=nil, options={}, &block)
- caller.each do |line|
- case line
- when Lazydoc::CALLER_REGEXP
- options[:desc] = Support::Lazydoc.register($1, $3.to_i - 1)
- break
- end
- end if options[:desc] == nil
+ if options[:desc] == nil
+ caller[0] =~ Lazydoc::CALLER_REGEXP
+ options[:desc] = Support::Lazydoc.register($1, $3.to_i - 1)
+ end
[:config, key, value, options, block]
end
def config_attr(key, value=nil, options={}, &block)
- caller.each do |line|
- case line
- when Lazydoc::CALLER_REGEXP
- options[:desc] = Support::Lazydoc.register($1, $3.to_i - 1)
- break
- end
- end if options[:desc] == nil
+ if options[:desc] == nil
+ caller[0] =~ Lazydoc::CALLER_REGEXP
+ options[:desc] = Support::Lazydoc.register($1, $3.to_i - 1)
+ end
[:config_attr, key, value, options, block]
end
def c
Support::Validation
end
private
-
- def nest(name)
- # use self if self is a Module or Class,
- # or self.class if self is an instance.
- File.join((self.kind_of?(Module) ? self : self.class).instance_variable_get(:@tap_declaration_base), name.to_s)
- end
def arity(block)
arity = block.arity
case
@@ -90,22 +67,67 @@
when arity < 0 then arity += 1
end
arity
end
-
- def task_block(block)
- lambda do |*inputs|
- inputs.unshift(self)
-
- arity = block.arity
- n = inputs.length
- unless n == arity || (arity < 0 && (-1-n) <= arity)
- raise ArgumentError.new("wrong number of arguments (#{n} for #{arity})")
+
+ def declare(klass, declaration, configs={}, options={}, &block)
+ # Extract name and dependencies from declaration
+ name, dependencies = case declaration
+ when Hash then declaration.to_a[0]
+ else [declaration, []]
+ end
+
+ unless dependencies.kind_of?(Array)
+ dependencies = [dependencies]
+ end
+
+ unless dependencies.empty?
+ dependencies.collect! do |dependency|
+ case dependency
+ when Array then dependency
+ when String, Symbol then [dependency, declare(Tap::Task, dependency)]
+ else
+ if dependency.kind_of?(Class) && dependency.ancestors.include?(Tap::Task)
+ [File.basename(dependency.default_name), dependency]
+ else
+ raise ArgumentError, "malformed dependency declaration: #{dependency}"
+ end
+ end
end
+ end
+
+ # Nest the constant name
+ base = (self.kind_of?(Module) ? self : self.class).instance_variable_get(:@tap_declaration_base)
+ name = File.join(base, name.to_s)
+
+ klass.subclass(name, configs, dependencies, options, &block)
+ end
+
+ def mod_declare(klass, declaration, configs={}, &block)
+ options = {}
+ options[:arity] = arity(block) if block_given?
- block.call(*inputs)
+ subclass = declare(klass, declaration, configs, options)
+
+ if block_given?
+ mod = Module.new
+ mod.module_eval %Q{
+ ACTION = ObjectSpace._id2ref(#{block.object_id})
+ def process(*args)
+ results = super
+ case ACTION.arity
+ when 0 then ACTION.call
+ when 1 then ACTION.call(self)
+ else ACTION.call(self, *args)
+ end
+ results
+ end
+ }
+ subclass.send(:include, mod)
end
+ subclass.instance
end
+
end
end
end
\ No newline at end of file