Class: Linen::Plugin::TwoPhaseCommand

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

Overview

Copyright 2007, LAIKA, Inc. #

#

Authors: #

* Ben Bleything <[email protected]>                    #

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name, &block) ⇒ TwoPhaseCommand

Returns a new instance of TwoPhaseCommand.



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

def initialize( plugin, name, &block )
	@plugin    = plugin
	@name      = name
	@arguments = []
	@help_text = "No help for #{plugin.short_name} #{name}"

	self.instance_eval &block
end

Instance Attribute Details

#editable_attrsObject (readonly)

Returns the value of attribute editable_attrs.



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

def editable_attrs
  @editable_attrs
end

#lookup_attrObject (readonly)

Returns the value of attribute lookup_attr.



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

def lookup_attr
  @lookup_attr
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#action(&block) ⇒ Object



95
96
97
# File 'lib/linen/two_phase_command.rb', line 95

def action( &block )
	@action_proc = block
end

#editable_attributes(*attr_list) ⇒ Object



85
86
87
# File 'lib/linen/two_phase_command.rb', line 85

def editable_attributes( *attr_list )
	@editable_attrs = attr_list
end

#execute(workspace = Linen::Workspace.new) ⇒ Object



23
24
25
# File 'lib/linen/two_phase_command.rb', line 23

def execute( workspace = Linen::Workspace.new )
	return workspace.instance_eval( &@action_proc )
end

#helpObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/linen/two_phase_command.rb', line 28

def help
	output = []

	output << @help_text.wrap
	output << nil # blank line

	# this map turns our list of args into a list like this:
	# <arg1> <arg2> <arg3> <arg4>...
	arg_list = @arguments.map {|a| "<#{a.to_s}>"}.join( ' ' )

	output << "Usage: #{@plugin.short_name} #{name} #{arg_list}"

	return output.join( "\n" )
end

#help_message(message) ⇒ Object



90
91
92
# File 'lib/linen/two_phase_command.rb', line 90

def help_message( message )
	@help_text = message
end

#inspect(workspace = Linen::Workspace.new, &block) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/linen/two_phase_command.rb', line 105

def inspect( workspace = Linen::Workspace.new, &block )
	if block_given?
		@inspect_proc = block
	else
		return workspace.instance_eval( &@inspect_proc )
	end
end

#lookup_by(attr_name, &block) ⇒ Object

def one_of( *args ) raise Linen::Plugin::ArgumentError, “You may not specify both required and one_of arguments” if @argument_type == :required

@argument_type = :one_of

args.each do |arg| raise Linen::Plugin::ArgumentError, “Argument ‘#arg’ has not been defined” unless @plugin.arguments.include? arg

@arguments << arg end end

def required_arguments( *args ) raise Linen::Plugin::ArgumentError, “You may not specify both required and one_of arguments” if @argument_type == :one_of

@argument_type = :required

args.each do |arg| raise Linen::Plugin::ArgumentError, “Argument ‘#arg’ has not been defined” unless @plugin.arguments.include? arg

@arguments << arg end end alias required_argument required_arguments



79
80
81
82
# File 'lib/linen/two_phase_command.rb', line 79

def lookup_by( attr_name, &block )
	@lookup_attr  = attr_name
	@lookup_block = block
end

#lookup_object(input) ⇒ Object

HELPER METHODS #



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/linen/two_phase_command.rb', line 118

def lookup_object( input )
	results = IndifferentHash.new
	
	argument  = @plugin.arguments[ @lookup_attr ]
	arg_value = input
	
	begin
		arg_value = Linen::CLI.reprompt( argument.prompt ) if arg_value.nil?

		processed_argument = argument.process( arg_value )

		results[ @lookup_attr ] = @lookup_block.call( processed_argument )
	rescue Linen::Plugin::ArgumentError => e
		# reset arg_value to nil so we get prompted on retry
		arg_value = nil
		
		retry
	end

	return results
end

#require_confirmationObject



100
101
102
# File 'lib/linen/two_phase_command.rb', line 100

def require_confirmation
	@require_confirmation = true
end

#requires_confirmation?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/linen/two_phase_command.rb', line 174

def requires_confirmation?
	@require_confirmation
end

#validate_arguments(args) ⇒ Object

FIXME:bbleything

The reprompting stuff below is going to come back to bite us when we try to add batch mode later… that said, in the interest of getting things out the door, we’re going to leave it be for now.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/linen/two_phase_command.rb', line 145

def validate_arguments( args )
	results = IndifferentHash.new
	
	@editable_attrs.each do |arg_name|
		argument  = @plugin.arguments[ arg_name ]
		arg_value = args.shift
		
		begin
			arg_value = Linen::CLI.reprompt( argument.prompt ) if arg_value.nil?

			result = argument.process( arg_value, :mode => :edit )
			
			if result.nil?
				# user just hit enter; leave value as-is.  noop.
			elsif result == ""
				results[ arg_name ] = ''
			else
				results[ arg_name ] = result
			end
		rescue Linen::Plugin::ArgumentError => e
			# reset arg_value to nil so we get prompted on retry
			arg_value = nil ; retry
		end
		
		return results
	end
end