Module: Sprout::Executable::CollectionParam

Included in:
Files, Strings
Defined in:
sprout/lib/sprout/executable/collection_param.rb

Overview

Included by any parameters that represent a collection of values, rather than a single value.

Should only be included by classes that extend Sprout::Executable::Param.

See Also:

Instance Method Summary (collapse)

Instance Method Details

- (CollectionParam) initialize

A new instance of CollectionParam



20
21
22
23
24
25
# File 'sprout/lib/sprout/executable/collection_param.rb', line 20

def initialize
  super
  @value = []
  @delimiter = "+="
  @option_parser_type_name = 'a,b,c'
end

- (Object) to_shell

Returns a shell formatted string of the collection



68
69
70
71
72
73
74
75
76
77
# File 'sprout/lib/sprout/executable/collection_param.rb', line 68

def to_shell
  prepare if !prepared?
  validate
  return '' if !visible?
  return @to_shell_proc.call(self) unless @to_shell_proc.nil?
  return value.join(' ') if hidden_name?
  return to_shell_value.collect { |val|
    "#{shell_name}#{delimiter}#{val}"
  }.join(' ')
end

- (Object) to_shell_value



79
80
81
# File 'sprout/lib/sprout/executable/collection_param.rb', line 79

def to_shell_value
  value
end

- (Object) value=(val)

Assign the value and raise if a collection wasn't provided.

This customization was added so that Rake tasks could help users avoid accidentally clobbering a collection with equals assignments.

The following example is incorrect and will raise an exception:

foo :name do |t|
  t.collection = 'A'
end

The following example is correct and should not raise an exception:

foo :name do |t|
  t.collection << 'A'
end

The following example is also correct and should not raise an exception:

foo :name do |t|
  t.collection = ['A']
end


51
52
53
54
55
56
57
# File 'sprout/lib/sprout/executable/collection_param.rb', line 51

def value=(val)
  if(val.is_a?(String) || !val.is_a?(Enumerable))
    message = "The #{name} property is an Enumerable. It looks like you may have used the assignment operator (=) with (#{value.inspect}) where the append operator (<<) was expected."
    raise Sprout::Errors::ExecutableError.new(message)
  end
  @value = val
end

- (Boolean) visible?

Hide the collection param if no items have been added to it.

Returns:



62
63
64
# File 'sprout/lib/sprout/executable/collection_param.rb', line 62

def visible?
  (!value.nil? && value.size > 0)
end