Class: Linen::Plugin::Argument

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

Overview

Copyright 2007, LAIKA, Inc. #

#

Authors: #

* Ben Bleything <[email protected]>                    #

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name, opts) ⇒ Argument

Returns a new instance of Argument.



13
14
15
16
17
18
19
# File 'lib/linen/argument.rb', line 13

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

#promptObject (readonly)

Returns the value of attribute prompt.



11
12
13
# File 'lib/linen/argument.rb', line 11

def prompt
  @prompt
end

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/linen/argument.rb', line 22

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 unless value =~ @regex
	return value unless @process

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