Class: Linen::Plugin::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/linen/argument.rb

Overview

Contains Linen::Argument, which encapsulates most of the argument parsing/processing logic.

Authors

Copyright © 2007 Laika, Inc.

This code released under the terms of the BSD license.

Version

$Id: argument.rb 391 2007-11-19 22:21:55Z bbleything $

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name, opts) ⇒ Argument

Returns a new instance of Argument.



24
25
26
27
28
29
30
# File 'lib/linen/argument.rb', line 24

def initialize( plugin, name, opts )
	@plugin  = plugin
	@name    = name
	@prompt  = opts[ :prompt  ] || "Please enter the value for #{@name}: "
	@regex   = opts[ :regex   ] || //
	@process = opts[ :process ] || nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/linen/argument.rb', line 22

def name
  @name
end

#promptObject (readonly)

Returns the value of attribute prompt.



22
23
24
# File 'lib/linen/argument.rb', line 22

def prompt
  @prompt
end

Instance Method Details

#process(value, opts_hash = IndifferentHash.new) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/linen/argument.rb', line 33

def process( value, opts_hash = IndifferentHash.new )
	opts_hash[ :mode ] ||= :simple
	
	# if we're in edit mode, we need to take care of a couple of
	# special cases before falling into the regular handling loop
	if opts_hash[ :mode ] == :edit or opts_hash[ :mode ] == 'edit'
		if value.empty? # means the user just hit enter.  Return nil to indicate the value should be left alone
			return nil
		elsif value =~ /^\s+$/ # user entered only spaces.  Empty string means "clear out previous value"
			return ""
		end
	end
	
	raise Linen::Plugin::ArgumentError, "The value entered ('#{value}') is not a valid #{@name}." unless value =~ @regex
	return value unless @process

	begin
		return @process.call( value )
	rescue => e
		raise Linen::Plugin::ArgumentError, e.message
	end
end