2

I have a Volt Framework Task that checks and stores information on a directory, e.g.

class DirectoryHelperTask < Volt::Task
    def list_contents()
        contents = []
        Dir.glob("/path/to/files").each do |f|
            contents << f
        end
        return contents
    end
end

I would like to call this from a different task, e.g.

class DirectoryRearrangerTask < Volt::Task
    dir_contents = DirectoryHelperTask.list_contents()
end

The code above (DirectoryRearranger) throws an error, as does a promise call

DirectoryHelperTask.list_contents().then do |r|
    dir_conents = r
end.fail do |e|
    puts "Error: #{e}"
end

Could not find a way to call a task from another task in the Volt Framework documentation.

Thanks a lot!

jason a
  • 21
  • 3

2 Answers2

0

From what I gather, tasks are meant to be run on the server side and then called on the client side, hence the use of the promise object. The promise object comes from OpalRb, so trying to call it from MRI won't work. If you have a "task" that will only be used on the server side, then it doesn't really fit with Volt's concept of a task.

Your first approach to the problem actually does work, except that DirectoryRearrangerTask can't inherit from Volt::Task.

directory_helper_task.rb

require_relative "directory_rearranger_task"

class DirectoryHelperTask < Volt::Task
  def list_contents
    contents = []
    Dir.glob("*").each do |file|
      contents << file
    end

    DirectoryRearrangerTask.rearrange(contents)

    contents
  end
end

directory_rearranger_task.rb

class DirectoryRearrangerTask
  def self.rearrange(contents)
    contents.reverse!
  end
end

Here is a GitHub repo with my solution to this problem.

GDP2
  • 1,948
  • 2
  • 22
  • 38
0

You can call tasks from the client or server, but keep in mind that you call instance methods on the class. (So they get treated like singletons) And all methods return a Promise. I think your issue here is that your doing dir_contents = DirectoryHelperTask.list_contents() inside of the class. While you could do this in ruby, I'm not sure its what you want.

Also, where you do dir_contents = r, unless dir_contents was defined before the block, its going to get defined just in the block.

Ryan
  • 956
  • 7
  • 8
  • I think you're exactly right--I should change the code to better conform with Volt's architecture. – jason a Aug 25 '15 at 17:44