Autoload the util classes. HashWithIndifferentAccess for data store
This commit is contained in:
parent
0f70812142
commit
59e1e43c74
|
@ -2,7 +2,6 @@ require 'json'
|
|||
require 'i18n'
|
||||
require 'virtualbox'
|
||||
require 'radar'
|
||||
require 'vagrant/util/glob_loader'
|
||||
|
||||
module Vagrant
|
||||
# TODO: Move more classes over to the autoload model. We'll
|
||||
|
@ -12,6 +11,7 @@ module Vagrant
|
|||
autoload :Config, 'vagrant/config'
|
||||
autoload :DataStore, 'vagrant/data_store'
|
||||
autoload :Errors, 'vagrant/errors'
|
||||
autoload :Util, 'vagrant/util'
|
||||
|
||||
module Command
|
||||
autoload :Base, 'vagrant/command/base'
|
||||
|
@ -41,7 +41,7 @@ I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_ro
|
|||
# Load them up. One day we'll convert this to autoloads. Today
|
||||
# is not that day. Low hanging fruit for anyone wishing to do it.
|
||||
libdir = File.expand_path("lib/vagrant", Vagrant.source_root)
|
||||
Vagrant::GlobLoader.glob_require(libdir, %w{util/stacked_proc_runner
|
||||
Vagrant::Util::GlobLoader.glob_require(libdir, %w{
|
||||
downloaders/base provisioners/base provisioners/chef systems/base
|
||||
hosts/base})
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
module Vagrant
|
||||
module Util
|
||||
autoload :Busy, 'vagrant/util/busy'
|
||||
autoload :GlobLoader, 'vagrant/util/glob_loader'
|
||||
autoload :HashWithIndifferentAccess, 'vagrant/util/hash_with_indifferent_access'
|
||||
autoload :PlainLogger, 'vagrant/util/plain_logger'
|
||||
autoload :Platform, 'vagrant/util/platform'
|
||||
autoload :ResourceLogger, 'vagrant/util/resource_logger'
|
||||
autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner'
|
||||
autoload :TemplateRenderer, 'vagrant/util/template_renderer'
|
||||
end
|
||||
end
|
|
@ -1,22 +1,24 @@
|
|||
module Vagrant
|
||||
# Eases the processes of loading specific files then globbing
|
||||
# the rest from a specified directory.
|
||||
module GlobLoader
|
||||
# Glob requires all ruby files in a directory, optionally loading select
|
||||
# files initially (since others may depend on them).
|
||||
#
|
||||
# @param [String] dir The directory to glob
|
||||
# @param [Array<String>] initial_files Initial files (relative to `dir`)
|
||||
# to load
|
||||
def self.glob_require(dir, initial_files=[])
|
||||
initial_files.each do |file|
|
||||
require File.expand_path(file, dir)
|
||||
end
|
||||
module Util
|
||||
# Eases the processes of loading specific files then globbing
|
||||
# the rest from a specified directory.
|
||||
module GlobLoader
|
||||
# Glob requires all ruby files in a directory, optionally loading select
|
||||
# files initially (since others may depend on them).
|
||||
#
|
||||
# @param [String] dir The directory to glob
|
||||
# @param [Array<String>] initial_files Initial files (relative to `dir`)
|
||||
# to load
|
||||
def self.glob_require(dir, initial_files=[])
|
||||
initial_files.each do |file|
|
||||
require File.expand_path(file, dir)
|
||||
end
|
||||
|
||||
# Glob require the rest
|
||||
Dir[File.join(dir, "**", "*.rb")].each do |f|
|
||||
require File.expand_path(f)
|
||||
# Glob require the rest
|
||||
Dir[File.join(dir, "**", "*.rb")].each do |f|
|
||||
require File.expand_path(f)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
module Vagrant
|
||||
module Util
|
||||
# A hash with indifferent access. Mostly taken from Thor/Rails (thanks).
|
||||
# Normally I'm not a fan of using an indifferent access hash sine Symbols
|
||||
# are basically memory leaks in Ruby, but since Vagrant is typically a quick
|
||||
# one-off binary run and it doesn't use too many hash keys where this is
|
||||
# used, the effect should be minimal.
|
||||
#
|
||||
# hash[:foo] #=> 'bar'
|
||||
# hash['foo'] #=> 'bar'
|
||||
#
|
||||
class HashWithIndifferentAccess < ::Hash
|
||||
def initialize(hash={})
|
||||
super()
|
||||
|
||||
hash.each do |key, value|
|
||||
self[convert_key(key)] = value
|
||||
end
|
||||
end
|
||||
|
||||
def [](key)
|
||||
super(convert_key(key))
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
super(convert_key(key), value)
|
||||
end
|
||||
|
||||
def delete(key)
|
||||
super(convert_key(key))
|
||||
end
|
||||
|
||||
def values_at(*indices)
|
||||
indices.collect { |key| self[convert_key(key)] }
|
||||
end
|
||||
|
||||
def merge(other)
|
||||
dup.merge!(other)
|
||||
end
|
||||
|
||||
def merge!(other)
|
||||
other.each do |key, value|
|
||||
self[convert_key(key)] = value
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def key?(key)
|
||||
super(convert_key(key))
|
||||
end
|
||||
|
||||
alias_method :include?, :key?
|
||||
alias_method :has_key?, :key?
|
||||
alias_method :member?, :key?
|
||||
|
||||
protected
|
||||
|
||||
def convert_key(key)
|
||||
key.is_a?(Symbol) ? key.to_s : key
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,30 @@
|
|||
require "test_helper"
|
||||
|
||||
class HashWithIndifferentAccessUtilTest < Test::Unit::TestCase
|
||||
setup do
|
||||
@klass = Vagrant::Util::HashWithIndifferentAccess
|
||||
@instance = @klass.new
|
||||
end
|
||||
|
||||
should "be a hash" do
|
||||
assert @instance.is_a?(Hash)
|
||||
end
|
||||
|
||||
should "allow indifferent access when setting with a string" do
|
||||
@instance["foo"] = "bar"
|
||||
assert_equal "bar", @instance[:foo]
|
||||
end
|
||||
|
||||
should "allow indifferent access when setting with a symbol" do
|
||||
@instance[:foo] = "bar"
|
||||
assert_equal "bar", @instance["foo"]
|
||||
end
|
||||
|
||||
should "allow indifferent key lookup" do
|
||||
@instance["foo"] = "bar"
|
||||
assert @instance.key?(:foo)
|
||||
assert @instance.has_key?(:foo)
|
||||
assert @instance.include?(:foo)
|
||||
assert @instance.member?(:foo)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue