class Handlebars::Helpers::Comparison::Default
Default: Returns the first value that is not nil or undefined, otherwise the 'default' value is returned.
Public Instance Methods
handlebars_helper()
click to toggle source
# 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)
click to toggle source
Parse will Default: Returns the first value that is not nil or undefined, otherwise the 'default' value is returned.
@example
emotion = nil puts Default.new.parse(emotion, 'happy') happy
@example
emotion = 'sad' puts Default.new.parse(emotion, 'happy') sad
@example
david = nil lisa = nil ben = nil puts Default.new.parse(david, lisa, ben, 'happy') happy
@example
david = nil lisa = sad ben = mad puts Default.new.parse(david, lisa, ben, 'happy') sad
@param [Object] *values - one or more paramaters that may or may not contain nil @param [String] default_value - the last paramater will be the default value @return [String] value or default value
# 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