Class: ParameterSubstitution::Formatters::IfTruthy

Inherits:
Base
  • Object
show all
Defined in:
lib/parameter_substitution/formatters/if_truthy.rb

Constant Summary collapse

TRUTHY_VALUES =
[true, "true", "t", 1, "1", "on", "yes"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

encoding, format, key, parse_duration

Constructor Details

#initialize(value_if_true, value_if_false) ⇒ IfTruthy

Returns a new instance of IfTruthy.



14
15
16
17
# File 'lib/parameter_substitution/formatters/if_truthy.rb', line 14

def initialize(value_if_true, value_if_false)
  @value_if_true = value_if_true
  @value_if_false = value_if_false
end

Class Method Details

.descriptionObject



6
7
8
# File 'lib/parameter_substitution/formatters/if_truthy.rb', line 6

def self.description
  "If the input is truthy (i.e. #{TRUTHY_VALUES.inspect}) then the input is replaced with the first argument. Otherwise, the input is replaced with the second argument."
end

.has_parameters?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/parameter_substitution/formatters/if_truthy.rb', line 10

def self.has_parameters?
  true
end

Instance Method Details

#downcase_if_string(value) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/parameter_substitution/formatters/if_truthy.rb', line 29

def downcase_if_string(value)
  if value.is_a?(String)
    value.downcase
  else
    value
  end
end

#format(value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/parameter_substitution/formatters/if_truthy.rb', line 19

def format(value)
  if TRUTHY_VALUES.include?(downcase_if_string(value))
    @value_if_true
  else
    @value_if_false
  end
end