Class: Envo::CmdListAdd
- Inherits:
-
Object
- Object
- Envo::CmdListAdd
- Defined in:
- lib/envo/cmd_list_add.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
- .parse_cli_all(args) ⇒ Object
- .parse_cli_args(name, args, opts) ⇒ Object
- .parse_script(name, tokens, opts) ⇒ Object
- .register_cli_parser(parser) ⇒ Object
- .register_help(help) ⇒ Object
- .register_script_parser(parser) ⇒ Object
Instance Method Summary collapse
- #execute(ctx) ⇒ Object
-
#initialize(name, values, pos) ⇒ CmdListAdd
constructor
A new instance of CmdListAdd.
Constructor Details
#initialize(name, values, pos) ⇒ CmdListAdd
Returns a new instance of CmdListAdd.
57 58 59 60 61 62 |
# File 'lib/envo/cmd_list_add.rb', line 57 def initialize(name, values, pos) raise Envo::Error.new 'list-add: no values to add provided' if values.empty? @name = name @values = values @pos = pos end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
64 65 66 |
# File 'lib/envo/cmd_list_add.rb', line 64 def name @name end |
#pos ⇒ Object
Returns the value of attribute pos.
64 65 66 |
# File 'lib/envo/cmd_list_add.rb', line 64 def pos @pos end |
#values ⇒ Object
Returns the value of attribute values.
64 65 66 |
# File 'lib/envo/cmd_list_add.rb', line 64 def values @values end |
Class Method Details
.parse_cli_all(args) ⇒ Object
14 15 16 17 18 |
# File 'lib/envo/cmd_list_add.rb', line 14 def self.parse_cli_all(args) opts = CliParser.filter_opts_front(args) raise Envo::Error.new "list-add: missing name. Use 'la <name> <val>'" if args.empty? parse_cli_args(args[0], args[1..], opts) end |
.parse_cli_args(name, args, opts) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/envo/cmd_list_add.rb', line 38 def self.parse_cli_args(name, args, opts) opts += CliParser.filter_opts(args) pos = nil opts.filter! do |opt| case opt when '--front', '--top' pos = :front false when '--back', '--bottom' pos = :back false else true end end ParsedCmd.new(CmdListAdd.new(name, args, pos), opts) end |
.parse_script(name, tokens, opts) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/envo/cmd_list_add.rb', line 20 def self.parse_script(name, tokens, opts) pos = nil opts.filter! do |opt| case opt when 'front', 'top' pos = :front false when 'back', 'bottom' pos = :back false else true end end ParsedCmd.new(CmdListAdd.new(name, tokens, pos), opts) end |
.register_cli_parser(parser) ⇒ Object
7 8 9 |
# File 'lib/envo/cmd_list_add.rb', line 7 def self.register_cli_parser(parser) parser.add_cmd('la', ->(cmd, args) { parse_cli_all(args) }) end |
.register_help(help) ⇒ Object
3 4 5 |
# File 'lib/envo/cmd_list_add.rb', line 3 def self.register_help(help) help.add_cmd 'la <name> <val>', "shorthand for 'list <name> add <val>'" end |
.register_script_parser(parser) ⇒ Object
11 12 |
# File 'lib/envo/cmd_list_add.rb', line 11 def self.register_script_parser(parser) end |
Instance Method Details
#execute(ctx) ⇒ 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 95 96 97 98 99 100 |
# File 'lib/envo/cmd_list_add.rb', line 66 def execute(ctx) ename = ctx.(@name) list = ctx.smart_get(ename) ok = list.list? if !ok if list.type == :empty ok ||= ctx.ask("#{ename} doesn't exist. Create?") else ok ||= ctx.ask("#{ename} is not a list, but a #{list.type}. Convert?") end end raise Envo::Error.new "list-add: adding list item to a non-list" if !ok list = list.to_list ordered = @pos == :front ? values.reverse : values ordered.each do |val| val = ctx.(val) ok = list.accept_item?(val) ok ||= ctx.ask("Add #{val.type} to #{list.type}?") raise Envo::Error.new "list-add: adding #{val.type} to #{list.type}" if !ok idesc = val.invalid_description ok = !idesc ok ||= ctx.ask("Add #{idesc} to #{ename}?") raise Envo::Error.new "list-add: adding #{idesc} to #{ename}" if !ok list.insert(val.to_s, @pos) end ctx.smart_set(ename, list) end |