Module: Martin::ClassMethods

Defined in:
lib/martin/base.rb

Instance Method Summary collapse

Instance Method Details

#command(regexp, &block) ⇒ Object

Adds a command

Parameters:

  • cmd (Regexp)

    the regexp you want to match to run the command



44
45
46
# File 'lib/martin/base.rb', line 44

def command(regexp, &block)
    Commands << DSL::Command.new(regexp, block)
end

#configure(&block) ⇒ Object

Runs once, at the beginning of the application If there are multiple configure blocks, then each one will run in the order the were created.

Parameters:

  • cmd (Regexp)

    the regexp you want to match to run the command



37
38
39
# File 'lib/martin/base.rb', line 37

def configure(&block)
    Configures << DSL::Configure.new(block)
end

#error(&block) ⇒ Object

When the user input matches non of the user’s commands then this method will run If there are multiple error blocks, then each one will run in the order the were created.

Parameters:

  • cmd (Regexp)

    the regexp you want to match to run the command



54
55
56
# File 'lib/martin/base.rb', line 54

def error(&block)
    Errors << DSL::Error.new(block)
end

#run(args = ARGV.join(' ')) ⇒ Object

Runs the application on the given arguments

Parameters:

  • args (String) (defaults to: ARGV.join(' '))

    The arguments to run the application on. Default is ARGV.join(‘ ’)



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

def run(args = ARGV.join(' '))
    Configures.each do |configure|
        configure.block.call
    end
    Commands.each do |command|
        match = command.regexp.match(args)
        unless match.nil?
            command.block.call(*match.captures)
        else
            Errors.each do |error|
                error.block.call(args)
            end
        end
    end
    self
end