class Configurator::Cast::Director

Public Class Methods

[](type)
Alias for: acquire
__mutex__() click to toggle source
# File lib/configurator/cast.rb, line 26
def __mutex__
  @__mutex__ ||= ::Monitor.new
end
acquire(type) click to toggle source
# File lib/configurator/cast.rb, line 46
def acquire(type)
  type_key = type_to_key(type)
  return casts[type_key] if casts.include?(type_key)

  __mutex__.synchronize do
    return casts[type_key] if casts.include?(type_key)

    casts[type_key] = case type
      when ::Array then
        Cast::Collection.new(type.first)
      when :uri, /uri/i then
        Cast::URI.new
      when :any, /any/i then
        Cast::Generic.new
      when ::Symbol, ::String then
        begin
          Cast.const_get(type.to_s.capitalize).new
        rescue NameError
          raise InvalidCastType, "Invalid cast type #{type}"
        end
      when ::Proc then
        Cast::Callable.new(type)
    else
      raise InvalidCastType, "Invalid cast type #{type}"
    end
  end

  casts[type_key]
end
Also aliased as: []
casts() click to toggle source
# File lib/configurator/cast.rb, line 30
def casts
  if @casts.nil?
    __mutex__.synchronize { @casts ||= {} }
  else
    @casts
  end
end
type_to_key(type) click to toggle source
# File lib/configurator/cast.rb, line 38
def type_to_key(type)
  case type
    when ::Array then "collection:%s" % type.first
    when ::Proc then "proc:%d" % type.object_id
    else type.to_s
  end
end