Class: Simp::Cli::Config::ListItem

Inherits:
Item
  • Object
show all
Defined in:
lib/simp/cli/config/item.rb

Overview

A Item that asks for lists instead of Strings

note that @value  is now an Array

Instance Attribute Summary collapse

Attributes inherited from Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from Item

#apply, #default_value, #next_items, #os_value, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_status, #recommended_value, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s

Constructor Details

#initializeListItem

Returns a new instance of ListItem.



350
351
352
353
# File 'lib/simp/cli/config/item.rb', line 350

def initialize
  super
  @allow_empty_list = false
end

Instance Attribute Details

#allow_empty_listObject

Returns the value of attribute allow_empty_list.



348
349
350
# File 'lib/simp/cli/config/item.rb', line 348

def allow_empty_list
  @allow_empty_list
end

Instance Method Details

#highline_question_typeObject



373
374
375
376
377
378
379
# File 'lib/simp/cli/config/item.rb', line 373

def highline_question_type
  # Convert the String (delimited by comma and/or whitespace) answer into an array
  lambda { |str|
    str = str.split(/,\s*|,?\s+/) if str.is_a? String
    str
  }
end

#not_valid_messageObject



355
356
357
358
359
# File 'lib/simp/cli/config/item.rb', line 355

def not_valid_message
  extra = 'hit enter to skip'
  extra = "hit enter to accept default value" if default_value
  "enter a comma or space-delimited list (#{extra})"
end

#query_extras(q) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/simp/cli/config/item.rb', line 361

def query_extras( q )
  # NOTE: this is a hack to massage Array input to/from a highline query.
  # It would probably be better (but more complex) to provide native Array
  # support for highline.
  # TODO: Override #query_ask using Highline's #gather?
  q.default  = q.default.join( " " ) if q.default.is_a? Array
  reminder = ::HighLine.color( not_valid_message,
                        ::HighLine.const_get('YELLOW') )
  q.question = "#{reminder}\n#{q.question}"
  q
end

#validate(list) ⇒ Object

validate the list and each item in the list



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/simp/cli/config/item.rb', line 382

def validate( list )
  # reuse the highline lambda to santize input
  return true  if (@allow_empty_list && list.nil?)
  list = highline_question_type.call( list ) if !list.is_a? Array
  return false if !list.is_a?(Array)
  return false if (!@allow_empty_list && list.empty? )
  list.each{ |item|
    return false if !validate_item( item )
  }
  true
end

#validate_item(x) ⇒ Object

validate a single list item



395
396
397
# File 'lib/simp/cli/config/item.rb', line 395

def validate_item( x )
  fail 'not implemented!'
end