Class: Linen::Plugin::TwoPhaseCommand
- Inherits:
-
Object
- Object
- Linen::Plugin::TwoPhaseCommand
- Includes:
- CommandInfrastructure
- Defined in:
- lib/linen/two_phase_command.rb
Overview
A two-phase command, commonly used for editing data.
Authors
-
Ben Bleything <[email protected]>
Copyright
Copyright © 2007 Laika, Inc.
This code released under the terms of the BSD license.
Version
$Id: two_phase_command.rb 407 2007-12-14 17:13:55Z bbleything $
Instance Attribute Summary collapse
-
#editable_attrs ⇒ Object
readonly
Returns the value of attribute editable_attrs.
-
#lookup_attr ⇒ Object
readonly
Returns the value of attribute lookup_attr.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #editable_attributes(*attr_list) ⇒ Object
-
#lookup_by(attr_name, &block) ⇒ Object
PLUGIN DEFINITION METHODS #.
-
#lookup_object(input) ⇒ Object
HELPER METHODS #.
- #validate_arguments(args) ⇒ Object
Methods included from CommandInfrastructure
#action, #can_inspect?, #execute, #help, #help_message, #initialize, #inspect, #require_confirmation, #requires_confirmation?
Instance Attribute Details
#editable_attrs ⇒ Object (readonly)
Returns the value of attribute editable_attrs.
23 24 25 |
# File 'lib/linen/two_phase_command.rb', line 23 def editable_attrs @editable_attrs end |
#lookup_attr ⇒ Object (readonly)
Returns the value of attribute lookup_attr.
23 24 25 |
# File 'lib/linen/two_phase_command.rb', line 23 def lookup_attr @lookup_attr end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/linen/two_phase_command.rb', line 23 def name @name end |
Instance Method Details
#editable_attributes(*attr_list) ⇒ Object
36 37 38 |
# File 'lib/linen/two_phase_command.rb', line 36 def editable_attributes( *attr_list ) @editable_attrs = attr_list end |
#lookup_by(attr_name, &block) ⇒ Object
PLUGIN DEFINITION METHODS #
30 31 32 33 |
# File 'lib/linen/two_phase_command.rb', line 30 def lookup_by( attr_name, &block ) @lookup_attr = attr_name @lookup_block = block end |
#lookup_object(input) ⇒ Object
HELPER METHODS #
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/linen/two_phase_command.rb', line 45 def lookup_object( input ) results = IndifferentHash.new argument = @plugin.arguments[ @lookup_attr ] arg_value = input # prompt if we didn't get an arg value arg_value ||= Linen::CLI.prompt( argument.prompt ) # put the command back onto the history command_line = Linen::Workspace.handler.history.pop command_line << " #{arg_value}" unless command_line =~ /#{arg_value}$/ Linen::Workspace.handler.history.push command_line.squeeze( ' ' ).strip processed_argument = argument.process( arg_value ) results[ @lookup_attr ] = @lookup_block.call( processed_argument ) return results end |
#validate_arguments(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/linen/two_phase_command.rb', line 66 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.prompt( 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 puts e. # reset arg_value to nil so we get prompted on retry arg_value = nil ; retry end end return results end |