Class: MarkdownExec::OptionValue

Inherits:
Object
  • Object
show all
Defined in:
lib/option_value.rb

Overview

OptionValue

This class provides utilities to format option values for different contexts. The ‘for_hash` method prepares the value to be used as a default in `env_str()`. The `for_yaml` method prepares the value for output as a default in `list_default_yaml()`.

Class Method Summary collapse

Class Method Details

.for_hash(value, default = nil) ⇒ Object

Formats the value for use in a hash.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/option_value.rb', line 15

def self.for_hash(value, default = nil)
  return default if value.nil?

  case value
  when String, Integer
    value
  when TrueClass, FalseClass
    value ? true : false
  when ->(v) { v.respond_to?(:empty?) && v.empty? }
    default
  else
    value.to_s
  end
end

.for_yaml(value, default = nil) ⇒ Object

Formats the value for output in YAML.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/option_value.rb', line 31

def self.for_yaml(value, default = nil)
  return default if value.nil?

  case value
  when String
    "'#{value}'"
  when Integer
    value
  when TrueClass, FalseClass
    value ? true : false
  when ->(v) { v.respond_to?(:empty?) && v.empty? }
    default
  else
    value.to_s
  end
end