Class: Handlebars::Helpers::Comparison::Default

Inherits:
BaseSafeStringHelper show all
Defined in:
lib/handlebars/helpers/comparison/default.rb

Overview

Default: Returns the first value that is not nil or undefined, otherwise the ‘default’ value is returned.

Instance Method Summary collapse

Methods inherited from BaseSafeStringHelper

#wrapper

Methods inherited from BaseHelper

#parse_json, #struct_to_hash, #tokenizer, #wrapper

Instance Method Details

#handlebars_helperObject



59
60
61
62
# File 'lib/handlebars/helpers/comparison/default.rb', line 59

def handlebars_helper
  # Exclude last paramater which is the context V8::Object
  proc { |_context, *values| wrapper(parse(values[0..-2])) }
end

#parse(values) ⇒ String

Parse will Default: Returns the first value that is not nil or undefined, otherwise the ‘default’ value is returned.

Examples:


emotion = nil
puts Default.new.parse(emotion, 'happy')

happy

emotion = 'sad'
puts Default.new.parse(emotion, 'happy')

sad

david = nil
lisa = nil
ben = nil
puts Default.new.parse(david, lisa, ben, 'happy')

happy

david = nil
lisa = sad
ben = mad
puts Default.new.parse(david, lisa, ben, 'happy')

sad

Parameters:

  • *values (Object)
    • one or more paramaters that may or may not contain nil

  • default_value (String)
    • the last paramater will be the default value

Returns:

  • (String)

    value or default value



51
52
53
54
55
56
57
# File 'lib/handlebars/helpers/comparison/default.rb', line 51

def parse(values)
  default_value = values[-1]

  find_value = values[0..-2].find { |value| !value.nil? }

  find_value || default_value
end