Class: Esse::CLI::Parser::BoolOrHash

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/cli/parser/bool_or_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, default: nil) ⇒ BoolOrHash

Returns a new instance of BoolOrHash.



13
14
15
16
# File 'lib/esse/cli/parser/bool_or_hash.rb', line 13

def initialize(key, default: nil)
  @key = key
  @default = default
end

Instance Method Details

#parse(input) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/esse/cli/parser/bool_or_hash.rb', line 18

def parse(input)
  return true if TRUTHY.include?(input)
  return false if FALSEY.include?(input)
  return input if input.is_a?(Hash)
  return @default if input.nil?
  return true if @key.to_s == input
  return @default unless HASH_MATCHER.match?(input)

  compact_hash = input.to_s.split(HASH_SEPARATOR).each_with_object({}) do |pair, hash|
    key, val = pair.match(HASH_MATCHER).captures
    hash[key.to_sym] = may_array(val)
  end
  return @default if compact_hash.empty?

  Esse::HashUtils.explode_keys(compact_hash)
end