class YARD::CLI::Config

CLI command to view or edit configuration options @since 0.6.2

Attributes

append[RW]

@return [Boolean] whether to append values to existing key

as_list[RW]

@return [Boolean] whether the value being set should be inside a list

key[RW]

@return [Symbol, nil] the key to view/edit, if any

reset[RW]

@return [Boolean] whether to reset the {#key}

values[RW]

@return [Array, nil] the list of values to set (or single value), if modifying

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/yard/cli/config.rb, line 21
def initialize
  super
  self.key = nil
  self.values = []
  self.reset = false
  self.append = false
  self.as_list = false
end

Public Instance Methods

description() click to toggle source
# File lib/yard/cli/config.rb, line 30
def description
  'Views or edits current global configuration'
end
run(*args) click to toggle source
# File lib/yard/cli/config.rb, line 34
def run(*args)
  optparse(*args)
  if key
    if reset || values.size > 0
      modify_item
    else
      view_item
    end
  else
    list_configuration
  end
end

Private Instance Methods

encode_value(value) click to toggle source
# File lib/yard/cli/config.rb, line 81
def encode_value(value)
  case value
  when /^-?\d+/; value.to_i
  when "true"; true
  when "false"; false
  else value
  end
end
encode_values() click to toggle source
# File lib/yard/cli/config.rb, line 73
def encode_values
  if values.size == 1 && !as_list
    encode_value(values.first)
  else
    values.map {|v| encode_value(v) }
  end
end
list_configuration() click to toggle source
# File lib/yard/cli/config.rb, line 67
def list_configuration
  log.debug "Listing configuration"
  require 'yaml'
  log.puts YAML.dump(YARD::Config.options).sub(/\A--.*\n/, '').gsub(/\n\n/, "\n")
end
modify_item() click to toggle source
# File lib/yard/cli/config.rb, line 49
def modify_item
  if reset
    log.debug "Resetting #{key}"
    YARD::Config.options[key] = YARD::Config::DEFAULT_CONFIG_OPTIONS[key]
  else
    log.debug "Setting #{key} to #{values.inspect}"
    items, current_items = encode_values, YARD::Config.options[key]
    items = [current_items].flatten + [items].flatten if append
    YARD::Config.options[key] = items
  end
  YARD::Config.save
end
optparse(*args) click to toggle source
# File lib/yard/cli/config.rb, line 90
def optparse(*args)
  list = false
  self.as_list = false
  self.append = false
  opts = OptionParser.new
  opts.banner = "Usage: yard config [options] [item [value ...]]"
  opts.separator ""
  opts.separator "Example: yard config load_plugins true"
  opts.separator ""
  opts.separator "Views and sets configuration items. If an item is provided"
  opts.separator "With no value, the item is viewed. If a value is provided,"
  opts.separator "the item is modified. Specifying no item is equivalent to --list."
  opts.separator "If you specify multiple space delimited values, these are"
  opts.separator "parsed as an array of values."
  opts.separator ""
  opts.separator "Note that `true` and `false` are reserved words."
  opts.separator ""
  opts.separator "General options:"

  opts.on('-l', '--list', 'List current configuration') do
    list = true
  end
  opts.on('-r', '--reset', 'Resets the specific item to default') do
    self.reset = true
  end

  opts.separator ""
  opts.separator "Modifying keys:"

  opts.on('-a', '--append', 'Appends items to existing key values') do
    self.append = true
  end
  opts.on('--as-list', 'Forces the value(s) to be wrapped in an array') do
    self.as_list = true
  end

  common_options(opts)
  parse_options(opts, args)
  args = [] if list
  self.key = args.shift.to_sym if args.size >= 1
  self.values = args if args.size >= 1
  args
end
view_item() click to toggle source
# File lib/yard/cli/config.rb, line 62
def view_item
  log.debug "Viewing #{key}"
  log.puts YARD::Config.options[key].inspect
end