Sha256: 24a85d24710b5c203be48d9489dd1bc3b02c63d207eb58e905f3cdafe40491c9

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

#
# Dynamic creation of command classes.
#
# Contributed by James Britt 2004.
#

class Command
  Command::Target_map = {}

  def execute( system )
    cls = self.class.to_s
    cmd  = Command::Target_map[ cls ]
    system.send( cmd, *@_vars )
  end
end

# See http://www.rubytalk.com/cgi-bin/scat.rb/ruby/ruby-talk/56334
# But simplified/hacked so that it creates command/query classes
# This method defines a new class derived from Command.  The initialize
# method is defined such that all arguments are pushed onto an
# instance variable array named @_vars.  When 'execute' is called,
# this array will provide all the parameters to be sent to the
# method invoked on 'system'.
def createCommandClass( name, m_name )
  Object.const_set( name, Class.new( Command ) ).instance_eval do
    define_method( :initialize ) do  |*args|
      inst_vars = []
      args.each do |k|
        inst_vars.push( k )
      end
      instance_variable_set( "@_vars",  inst_vars )
    end
  end
  begin
    Command::Target_map[ name ] = m_name
  rescue Exception
    STDERR.puts( "Error setting value in  Command::Target_map:  #{$!}" )
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
madeleine-0.9.0 contrib/create_command.rb
madeleine-0.9.0.pre contrib/create_command.rb
madeleine-0.8.0 contrib/create_command.rb
madeleine-0.8.0.pre contrib/create_command.rb
madeleine-0.7.3 contrib/create_command.rb
madeleine-0.7.2 contrib/create_command.rb