Module: Runnable::ClassMethods

Defined in:
lib/runnable.rb

Accessors for the module class variables collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *opts) ⇒ Object

Method missing processing for the command processors

Raises:

  • (NoMethodError)


92
93
94
95
96
97
98
99
100
# File 'lib/runnable.rb', line 92

def method_missing( name, *opts )
  raise NoMethodError.new( name.to_s ) unless name.to_s =~ /([a-z]*)_([a-z]*)/
 
  # command_processors
  if $2 == "processors"
    commands[$1.to_sym][:outputs] = opts.first[:outputs]
    commands[$1.to_sym][:exceptions] = opts.first[:exceptions]
  end
end

Instance Method Details

#command_style(style) ⇒ nil

Define the parameter style to be used.

Returns:

  • (nil)


52
53
54
# File 'lib/runnable.rb', line 52

def command_style( style )
  define_method( :command_style ) { style }
end

#commandsHash

Returns the user defined commands

Returns:

  • (Hash)

    commands User defined commands



106
107
108
# File 'lib/runnable.rb', line 106

def commands
  @commands ||= Hash.new
end

#define_command(name, opts = {}, &block) ⇒ nil

Create a user definde command

Parameters:

  • name (Symbol)

    The user defined command name

  • options (Hash)

    Options.

Returns:

  • (nil)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/runnable.rb', line 62

def define_command( name, opts = {}, &block )
  blocking = opts[:blocking] || false
  log_path = opts[:log_path] || false

  commands[name] = { :blocking => blocking }

  define_method( name ) do |*args|
    if block
      run name, block.call(*args), log_path
    else
      run name, nil, log_path
    end
    join if blocking
  end
end

#executes(cmd) ⇒ nil

Define the command to be executed

Parameters:

  • command (Symbol)

    Command to be executed

Returns:

  • (nil)


46
47
48
# File 'lib/runnable.rb', line 46

def executes( cmd )
  define_method( :command ) { cmd }
end

#processesHash

Returns the list of runnable instances by pid

Returns:

  • (Hash)

    list of runnable instances by pid



112
113
114
# File 'lib/runnable.rb', line 112

def processes
  @processes ||= Hash.new
end

#processes=(value) ⇒ Object

Processes writer



117
118
119
# File 'lib/runnable.rb', line 117

def processes=( value )
  @processes = value
end

#processors(opts = nil) ⇒ Object

Generic command processor. It allows to define generic processors used in all the user defined commands

Parameters:

  • opts (Hash) (defaults to: nil)

    Processing options

Options Hash (opts):

  • :outputs (Object) — default: nil

    Output processing Hash (regexp => output)

  • :exceptions (Object) — default: nil

    Exceptions processing Hash (regexp => exception)



83
84
85
86
87
88
89
# File 'lib/runnable.rb', line 83

def processors( opts = nil )
  if opts.is_a? Hash
    @processors = opts
  else
    @processors ||= Hash.new
  end
end