class Conker::VariableDeclaration

Public Class Methods

new(declaration_opts) click to toggle source
# File lib/conker.rb, line 183
def initialize(declaration_opts)
  declaration_opts.assert_valid_keys :required_in, :type, :default, *ENVIRONMENTS.map(&:to_sym), :delimiter
  if declaration_opts.key?(:delimiter) && declaration_opts[:type] != :array
    raise "Unknown key :delimiter for type :#{declaration_opts[:type] || :string}.  Did you mean :type => :array?"
  end
  @declaration_opts = declaration_opts.with_indifferent_access
end

Public Instance Methods

evaluate(current_environment, config, varname) click to toggle source
# File lib/conker.rb, line 191
def evaluate(current_environment, config, varname)
  @environment = current_environment
  @config = config
  check_missing_value! varname
  check_missing_default!
  from_config_variable_or_default(varname)
end

Private Instance Methods

check_missing_default!() click to toggle source
# File lib/conker.rb, line 206
def check_missing_default!
  environments_needing_default = ENVIRONMENTS.map(&:to_sym) - required_in_environments
  default_specified = @declaration_opts.key? :default
  all_environments_defaulted = environments_needing_default.all?(&@declaration_opts.method(:key?))
  unless default_specified || all_environments_defaulted
    raise MissingDefault
  end
end
check_missing_value!(varname) click to toggle source
# File lib/conker.rb, line 200
def check_missing_value!(varname)
  if required_in_environments.member?(@environment.to_sym) && !@config[varname]
    raise MustBeDefined
  end
end
default_value() click to toggle source

Only interpret the default value if it is a string (to avoid coercing nil to ”)

# File lib/conker.rb, line 229
def default_value
  default = @declaration_opts.include?(@environment) ? @declaration_opts[@environment] : @declaration_opts[:default]
  if default.is_a? String
    interpret_value(default, @declaration_opts[:type])
  else
    default
  end
end
from_config_variable_or_default(varname) click to toggle source
# File lib/conker.rb, line 215
def from_config_variable_or_default(varname)
  if @config[varname] && @environment != 'test'
    interpret_value(@config[varname], @declaration_opts[:type])
  else
    default_value
  end
end
interpret_value(value, type) click to toggle source
# File lib/conker.rb, line 238
def interpret_value(value, type)
  type = type.to_sym if type
  case type
  when :boolean
    value.to_s.downcase == "true" || value.to_i == 1
    # defaults to false if omitted
  when :integer
    Integer(value)
    # defaults to 0 if omitted
  when :float
    value ?  Float(value) : 0.0
    # defaults to 0.0 if omitted
  when :url
    raise MustBeDefined if value.nil? # there's nothing sensible to default to
    require 'uri' unless defined? URI
    URI.parse(value.to_s)
  when :addressable
    raise MustBeDefined if value.nil? # there's nothing sensible to default to
    require 'addressable' unless defined? Addressable
    Addressable::URI.parse(value.to_s)
  when :ip_address
    raise MustBeDefined if value.nil? # there's nothing sensible to default to
    require 'ipaddr' unless defined? IPAddr
    IPAddr.new(value.to_s)
  when :ip_range
    raise MustBeDefined if value.nil? # there's nothing sensible to default to
    require 'ipaddr' unless defined? IPAddr

    # Support <from>..<to> for ranges that cannot be specified with CIDR easily.
    if value =~ /\A(.*)\.\.(.*)\z/
      IPAddr.new($1) .. IPAddr.new($2)
    else
      IPAddr.new(value.to_s).to_range
    end
  when :timestamp
    raise MustBeDefined if value.nil? # there's nothing sensible to default to.
    Time.iso8601(value.to_s).utc
  when :string, nil
    value.to_s
    # defaults to '' if omitted
  when :hash
    unless value.is_a? Hash
      raise IncompatibleType, "wanted a Hash, got #{value.class}"
    end
    value.with_indifferent_access
  when :array
    if delimiter = @declaration_opts[:delimiter]
      # e.g. ENV['WIDGET'] = 'foo:bar:baz' # yields ['foo', 'bar', 'baz']
      if value.is_a? String
        value.split(delimiter)
      else
        raise IncompatibleType, "wanted a String to parse :array with :delimiter, got #{value.class}"
      end
    else
      # e.g.
      #   WIDGET: foo # yields WIDGET = ['foo']
      #   WIDGET:
      #     - bar
      #     - baz
      #    # yields WIDGET = ['foo', 'bar']
      Array(value)
    end
  else
    raise UnknownType, type.to_s
  end
end
required_in_environments() click to toggle source
# File lib/conker.rb, line 223
def required_in_environments
  Array(@declaration_opts[:required_in]).map(&:to_sym)
end