Sha256: 593e2259086d7a17f743d9179a6566b690414103d1af9f6e163aa6a4581ad337

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'erb'

module Subprocess
  module PopenFactoryMixin

    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods

      def def_command(name, command, class_meths=true)
        name = class_meths ? "self.#{name}" : name
        instance_eval "
          def #{name}(*args)
            Subprocess::Popen.new('#{command}', *args)
          end
        ", __FILE__, __LINE__
      end
      
      def def_dynamic_command(name, command, class_meths=true)
        name = class_meths ? "self.#{name}" : name
        instance_eval "
          def #{name}(data, *args)
            command = lambda do |data|
              \"#{command}\"
            end
            Subprocess::Popen.new(command.call(data), *args)
          end
        ", __FILE__, __LINE__
      end
    end # ClassMethods
  end # PopenFactoryMixin

  module PopenRemoteFactoryMixin

    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods

      def def_command(name, command, hostname_var, username_var, ssh_params_var, timeout=300)
        instance_eval do 
          define_method name.to_sym do
            eval("Subprocess::PopenRemote.new('#{command}', #{hostname_var}, #{username_var}, timeout, *#{ssh_params_var})")
          end
        end
      end

      def def_dynamic_command(name, command, hostname_var, username_var, ssh_params_var, timeout=300)
        instance_eval do 
          define_method name.to_sym do |data|
            cmd = ERB.new(command).result(binding)
            eval("Subprocess::PopenRemote.new(cmd, #{hostname_var}, #{username_var}, timeout, *#{ssh_params_var})")
          end
        end
      end
    end # ClassMethods
  end # PopenRemoteFactoryMixin

end # Subprocess

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
subprocess-0.1.6 lib/subprocess/popen_factory.rb