lib/capitate/cap_ext/variables.rb in capitate-0.2.7 vs lib/capitate/cap_ext/variables.rb in capitate-0.2.8
- old
+ new
@@ -40,19 +40,42 @@
# ==== Examples
# fetch_or_default(:memcached_port, 11211) => 11211
# # Any calls to fetch(memcached_port) in the future will return this value 11211 (unless overriden)
#
def fetch_or_default(variable, default, *args)
- begin
- return fetch(variable, *args)
- rescue IndexError
+ if exists?(variable)
+ fetch(variable, *args)
+ else
set variable, default
+ default
+ end
+ end
+
+ # Fetch or set and fetch any default variable listed.
+ #
+ # ==== Options
+ # +variable+:: Variable to fetch
+ # +variables+:: List if variables to try in order
+ #
+ # ==== Examples
+ # fetch_or_set(:sphinx_db_host, :db_host)
+ #
+ def fetch_or_set(variable, *default_variables)
+ return fetch(variable) if exists?(variable)
+
+ default_variables.each do |default_variable|
+ if exists?(default_variable)
+ value = fetch(default_variable)
+ set variable, value
+ return value
+ end
end
- default
+ nil
end
# Fetch roles with name and options
+ # I don't actually use this.
#
# ==== Options
# +name+:: Role name to look for
# +options+:: Options to match on, e.g. :primary => true
#
@@ -84,10 +107,10 @@
end
end
matched_roles.to_a
end
- # Fetch first role with name and options
+ # Fetch first role with name and options.
#
# ==== Options
# +name+:: Role name to look for
# +options+:: Options to match on, e.g. :primary => true
#
\ No newline at end of file