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 'i18n'
|
||||||
require 'virtualbox'
|
require 'virtualbox'
|
||||||
require 'radar'
|
require 'radar'
|
||||||
require 'vagrant/util/glob_loader'
|
|
||||||
|
|
||||||
module Vagrant
|
module Vagrant
|
||||||
# TODO: Move more classes over to the autoload model. We'll
|
# TODO: Move more classes over to the autoload model. We'll
|
||||||
|
@ -12,6 +11,7 @@ module Vagrant
|
||||||
autoload :Config, 'vagrant/config'
|
autoload :Config, 'vagrant/config'
|
||||||
autoload :DataStore, 'vagrant/data_store'
|
autoload :DataStore, 'vagrant/data_store'
|
||||||
autoload :Errors, 'vagrant/errors'
|
autoload :Errors, 'vagrant/errors'
|
||||||
|
autoload :Util, 'vagrant/util'
|
||||||
|
|
||||||
module Command
|
module Command
|
||||||
autoload :Base, 'vagrant/command/base'
|
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
|
# 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.
|
# is not that day. Low hanging fruit for anyone wishing to do it.
|
||||||
libdir = File.expand_path("lib/vagrant", Vagrant.source_root)
|
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
|
downloaders/base provisioners/base provisioners/chef systems/base
|
||||||
hosts/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,21 +1,23 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
# Eases the processes of loading specific files then globbing
|
module Util
|
||||||
# the rest from a specified directory.
|
# Eases the processes of loading specific files then globbing
|
||||||
module GlobLoader
|
# the rest from a specified directory.
|
||||||
# Glob requires all ruby files in a directory, optionally loading select
|
module GlobLoader
|
||||||
# files initially (since others may depend on them).
|
# 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`)
|
# @param [String] dir The directory to glob
|
||||||
# to load
|
# @param [Array<String>] initial_files Initial files (relative to `dir`)
|
||||||
def self.glob_require(dir, initial_files=[])
|
# to load
|
||||||
initial_files.each do |file|
|
def self.glob_require(dir, initial_files=[])
|
||||||
require File.expand_path(file, dir)
|
initial_files.each do |file|
|
||||||
end
|
require File.expand_path(file, dir)
|
||||||
|
end
|
||||||
|
|
||||||
# Glob require the rest
|
# Glob require the rest
|
||||||
Dir[File.join(dir, "**", "*.rb")].each do |f|
|
Dir[File.join(dir, "**", "*.rb")].each do |f|
|
||||||
require File.expand_path(f)
|
require File.expand_path(f)
|
||||||
|
end
|
||||||
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