Module: Clue::Options

Included in:
Clue
Defined in:
lib/clue/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/clue/options.rb', line 4

def options
  @options
end

#singular_optionsObject (readonly)

Returns the value of attribute singular_options.



4
5
6
# File 'lib/clue/options.rb', line 4

def singular_options
  @singular_options
end

Instance Method Details

#add_option(key, value) ⇒ Object

Add a single options



42
43
44
# File 'lib/clue/options.rb', line 42

def add_option(key, value)
  @options[key.to_sym] = value.to_s
end

#add_options(opts = {}) ⇒ Object

Add many options



47
48
49
# File 'lib/clue/options.rb', line 47

def add_options(opts = {})
  opts.each { |key, value| add_option key, value }
end

#add_singular_option(option) ⇒ Object

Add a singular option



27
28
29
30
# File 'lib/clue/options.rb', line 27

def add_singular_option(option)
  @singular_options << option.to_s
  @singular_options.uniq!
end

#add_singular_options(options) ⇒ Object

Add many singular options



33
34
35
36
37
38
39
# File 'lib/clue/options.rb', line 33

def add_singular_options(options)
  return unless options
  options = options.split(' ') if options.is_a?(String)

  @singular_options += options.map(&:to_s)
  @singular_options.uniq!
end

#cli_optionsObject

Returns the options string



52
53
54
# File 'lib/clue/options.rb', line 52

def cli_options
  options_as_array.join self.config[:between_options]
end

#init_options(options = nil) ⇒ Object

Initialize options ivars



7
8
9
10
11
12
# File 'lib/clue/options.rb', line 7

def init_options(options = nil)
  @options = {}
  @singular_options = []

  parse_options(options) if options
end

#options_as_arrayObject

Returns options as an array



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/clue/options.rb', line 57

def options_as_array
  # Valued options
  ary = @options.each_with_object([]) do |(key, value), memo|
    prefix = self.config[:option_prefix]
    separator = self.config[:option_value_separator]
    delimiter = self.config[:option_delimiter]
    memo << "#{prefix}#{key.to_s}#{separator}#{delimiter}#{value.to_s}#{delimiter}"
  end

  # Singular options
  ary = @singular_options.each_with_object(ary) do |opt, memo|
    prefix = self.config[:option_prefix]
    memo << "#{prefix}#{opt.to_s}"
  end

  ary
end

#parse_options(options) ⇒ Object

Separate simple options from valued ones.



15
16
17
18
19
20
21
22
23
24
# File 'lib/clue/options.rb', line 15

def parse_options(options)
  if options.kind_of?(Hash)
    @options = options
  elsif options.kind_of?(Array)
    @singular_options = options
    @options = @singular_options.pop if @singular_options.last.kind_of?(Hash)
  elsif options.kind_of?(String) || options.kind_of?(Symbol)
    @singular_options = [options.to_sym]
  end
end