Last active
February 2, 2016 07:28
-
-
Save dsci/81e6b1a365b642543ec4 to your computer and use it in GitHub Desktop.
Revisions
-
dsci revised this gist
Feb 2, 2016 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,16 @@ # Use instance methods of classes as Rake tasks: # # In a folder tasks, create your classes. # # class FooTask # extend RakeDecorator # # task :numbers, desc: 'A funny task that prints out the first 10 numbers' # def print_numbers_task # puts (1..10).to_a.join(',') # end # end # require 'rake' unless defined?(Rake) module RakeTasks -
dsci revised this gist
Jan 25, 2016 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ module RakeTasks module_function def add_task(task) task_class = task[:class].name if tasks.key?(task_class) tasks[task_class][:tasks] << task else @@ -29,19 +29,23 @@ def task(name, *args) @_args = args end def desc(description) @_desc = {}; @_desc[:description] = description end def method_added(meth_name) RakeTasks.add_task(build_task_config(meth_name)) %i(_task _args _desc).each { |ivar| instance_variable_set("@#{ivar}", nil) } end private def build_task_config(meth_name) task = { class: self, caller: self.instance_method(meth_name), task_name: @_task, arguments: @_args } task[:description] = @_desc[:desc] || @desc if @_desc task end end -
dsci revised this gist
Jan 25, 2016 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,16 @@ require 'rake' unless defined?(Rake) module RakeTasks @@tasks = {} module_function def add_task(task) task_class = task[:instance].name if tasks.key?(task_class) tasks[task_class][:tasks] << task else tasks[task_class] = { tasks: [task] } end -
dsci revised this gist
Jan 25, 2016 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,15 +29,19 @@ def task(name, *args) end def method_added(meth_name) RakeTasks.add_task(build_task_config(meth_name)) %i(_task _args _desc).each { |ivar| instance_variable_set("@#{ivar}", nil) } end private def build_task_config(meth_name) task = { instance: self, caller: self.instance_method(meth_name), task_name: @_task, arguments: @_args } task[:description] = @_desc[:desc] if @_desc task end end @@ -59,4 +63,4 @@ def LoadTasks(dir_path) end dir_path = File.join(File.dirname(__FILE__), 'tasks', '**') LoadTasks(dir_path) -
dsci revised this gist
Jan 25, 2016 . 1 changed file with 19 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,20 +6,20 @@ module RakeTasks module_function def add_task(task) if tasks.key?(task[:instance].name) tasks[task[:instance].name][:tasks] << task else tasks[task[:instance].name] = { tasks: [task] } end end def tasks @@tasks end end module RakeDecorator def task(name, *args) @@ -35,39 +35,28 @@ def method_added(meth_name) task_name: @_task, arguments: @_args } task[:description] = @_desc[:desc] if @_desc RakeTasks.add_task(task) %i(_task _args _desc).each { |ivar| instance_variable_set("@#{ivar}", nil) } end end def LoadTasks(dir_path) Dir[dir_path].each do |file| require file unless File.directory?(file) end RakeTasks.tasks.each_pair do |task_class,value| tasks, instance = value[:tasks], Module.const_get(task_class).new tasks.each do |task| Rake.application.last_description = task[:description] if task[:description] caller = task[:caller] Rake::Task.define_task(task[:task_name], task[:arguments]) do |arg| caller.bind(instance).call end end end end dir_path = File.join(File.dirname(__FILE__), 'tasks', '**') LoadTasks(dir_path) -
dsci revised this gist
Jan 25, 2016 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,7 @@ def task(name, *args) def method_added(meth_name) task = { instance: self, caller: self.instance_method(meth_name), task_name: @_task, arguments: @_args } @@ -60,15 +60,14 @@ def bar r = RakeGen.new RakeTasks.tasks.each_pair do |key,value| tasks = value[:tasks] instance = Module.const_get(key).new tasks.each do |task| Rake.application.last_description = task[:description] if task[:description] caller = task[:caller] Rake::Task.define_task(task[:task_name], task[:arguments]) do |arg| caller.bind(instance).call end end end -
dsci created this gist
Jan 25, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ require 'rake' module RakeTasks @@tasks = {} module_function def add_task(task) if @@tasks.key?(task[:instance].name) @@tasks[task[:instance].name][:tasks] << task else @@tasks[task[:instance].name] = { tasks: [task] } end #@@tasks << task end def tasks @@tasks end end module RakeDecorator def task(name, *args) @_task = name @_desc = args.pop if args.last.is_a?(Hash) @_args = args end def method_added(meth_name) task = { instance: self, caller: meth_name, task_name: @_task, arguments: @_args } if @_desc task[:description] = @_desc[:desc] end RakeTasks.add_task(task) %i(_task _args _desc).each { |ivar| instance_variable_set("@#{ivar}", nil) } end end class RakeGen extend RakeDecorator task "hello", :foo, desc: "Whatever it is." def foo p "Juri" end task "nie" def bar p "what!" end end r = RakeGen.new #p RakeTasks.tasks RakeTasks.tasks.each_pair do |key,value| tasks = value[:tasks] instance = Module.const_get(key).new tasks.each do |task| Rake.application.last_description = task[:description] if task[:description] Rake::Task.define_task(task[:task_name], task[:arguments]) do |arg| instance.public_send(task[:caller]) end end end