class Opto::Resolvers::Condition

Allows setting the value conditionally based on other variables

Example: from:

condition:
  - if:
      db_instances: 1
    then: single
  - elsif:
      db_instances:
        gt: 1
    then: multi
  - else: none

Which is the same as: if $db_instances == 1

return "single"

elsif $db_instances > 1

return "multi"

else

return "none"

end

If you don't define an else, a null will be returned when no conditions match.

Public Instance Methods

resolve() click to toggle source
# File lib/opto/resolvers/condition.rb, line 68
def resolve
  raise TypeError, "An Array of Hashes expected" unless hint.kind_of?(Array)
  raise TypeError, "An Array of Hashes expected" unless hint.all? {|h| h.kind_of?(Hash) }
  raise TypeError, "Can't use condition resolver when option isn't a member of an option group" if option.group.nil?

  conditions = hint.map {|h| HashCond.new(option.group, h) }

  unless conditions.last.else?
    conditions << HashCond.new(:else => nil)
  end

  matching_condition = conditions.find {|c| c.true? }
  matching_condition.result
end