Class: ConfigParser::List

Inherits:
Option show all
Defined in:
lib/config_parser/list.rb

Overview

List represents a special type of Option where multiple values may be assigned to the same key.

Constant Summary

Constants inherited from Option

Option::DEFAULT_ARGNAME, Option::OPTIONAL

Constants included from Utils

Utils::DELIMITER, Utils::LONG_FLAG, Utils::NEST, Utils::OPTION, Utils::OPTION_BREAK, Utils::SHORT_FLAG, Utils::SWITCH

Instance Attribute Summary collapse

Attributes inherited from Option

#arg_name, #optional

Attributes inherited from Flag

#assigned, #callback, #default, #desc, #hint, #key, #long, #nest_keys, #short

Instance Method Summary collapse

Methods inherited from Option

#parse

Methods inherited from Flag

#assign_default, #flags, #inspect, #nest, #parse, #reset, #to_s

Methods included from Utils

guess_hint, guess_option_type, guess_option_type_by_arg_name, guess_option_type_by_value, longify, next_arg, option?, parse_attrs, prefix_long, shortify, wrap

Constructor Details

#initialize(attrs = {}) ⇒ List

Returns a new instance of List.



13
14
15
16
17
18
# File 'lib/config_parser/list.rb', line 13

def initialize(attrs={})
  super

  @delimiter = attrs.has_key?(:delimiter) ? attrs[:delimiter] : DELIMITER
  @default = split(@default)
end

Instance Attribute Details

#delimiterObject (readonly)

The delimiter on which to split single values into multiple values; use nil to prevent splitting.



11
12
13
# File 'lib/config_parser/list.rb', line 11

def delimiter
  @delimiter
end

Instance Method Details

#assign(config, values) ⇒ Object

Assigns the values to config. Multiple calls to assign will concatenate (ie when assigned is true) new values onto the existing values. As usual, no values are assigned if key is not set. Returns config.



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

def assign(config, values)
  if key
    nest_config = nest(config)

    unless assigned
      nest_config[key] = []
    end

    array = (nest_config[key] ||= [])
    array.concat(values)
  end

  @assigned = true
  config
end

#process(value) ⇒ Object

Splits the value into multiple values, and then process as usual.



21
22
23
# File 'lib/config_parser/list.rb', line 21

def process(value)
  split(value).collect {|val| super(val) }
end

#split(obj) ⇒ Object

Splits string values along the delimiter, if specified. Returns array values directly, and an empty array for nil. All other values are arrayified like [obj].



47
48
49
50
51
52
53
54
# File 'lib/config_parser/list.rb', line 47

def split(obj)
  case obj
  when Array  then obj
  when String then delimiter ? obj.split(delimiter) : [obj]
  when nil    then []
  else [obj]
  end
end