Module: RCheck::OptionExpander

Defined in:
lib/rcheck/option_expander.rb

Class Method Summary collapse

Class Method Details

.[](name, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rcheck/option_expander.rb', line 7

def self.[](name, value)
  return [] if value == :none
  case name
  when :colors       then parse_theme value
  when :progress     then to_instance(ProgressPrinters, *value)
  when :report       then to_instance(ReportPrinters,   *value)
  when :filters      then to_instance(Filters,          *value)
  when :anti_filters then to_instance(Filters::Anti,    *value)
  when :headers      then to_instance(Headers,          *value)
  when :files, :initializers then parse_string_array value
  when :seed, :fail_code, :success_code
    value.to_s.to_i
  else value
  end
end

.constantize(name) ⇒ Object



3
4
5
# File 'lib/rcheck/option_expander.rb', line 3

def self.constantize(name)
  name.to_s.split('_').map(&:capitalize).join.to_sym
end

.parse_string_array(value) ⇒ Object



43
44
45
# File 'lib/rcheck/option_expander.rb', line 43

def self.parse_string_array(value)
  Array(value).map(&:to_s)
end

.parse_theme(theme) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rcheck/option_expander.rb', line 47

def self.parse_theme(theme)
  return theme if theme.is_a?(Hash)
  name = :"#{theme.upcase}"
  if Colors::Themes.const_defined?(name)
    Colors::Themes.const_get(name)
  else
    raise "Invalid color theme: #{theme.inspect}"
  end
end

.safer_const_get(scope, item) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rcheck/option_expander.rb', line 33

def self.safer_const_get(scope, item)
  if scope.const_defined?(constantize item)
    val = scope.const_get(constantize item)
    val.respond_to?(:new) ? val.new : val
  else
    raise Errors::ConfigParam, "#{item.inspect} not found in "\
      "#{scope.inspect}"
  end
end

.to_instance(scope, *list) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rcheck/option_expander.rb', line 23

def self.to_instance(scope, *list)
  list.map do |item|
    case item
    when Class          then item.new
    when String, Symbol then safer_const_get(scope, item.to_sym)
    else item
    end
  end
end