Class: Opto::Resolvers::Condition

Inherits:
Opto::Resolver show all
Defined in:
lib/opto/resolvers/condition.rb

Overview

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.

Defined Under Namespace

Classes: HashCond

Instance Attribute Summary

Attributes inherited from Opto::Resolver

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Resolver

for, inherited, #initialize, origin, #origin, resolvers

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#resolveObject

Raises:

  • (TypeError)

68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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