move util methods into their own namespace

This commit is contained in:
Thomas Reynolds 2012-04-14 13:36:24 -07:00
parent 094de61e92
commit 0bc35db4a6
8 changed files with 50 additions and 55 deletions

View file

@ -1,32 +1,18 @@
require "rbconfig"
# Using Thor's indifferent hash access
require "thor"
# Using a bunch of convenience methods
require "active_support"
# Setup our load paths
libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
class String
def camelize
self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
end
# Simple callback library
require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
require "middleman-core/version"
require "middleman-core/util"
# Top-level Middleman object
module Middleman
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
JRUBY = !!(RbConfig::CONFIG["RUBY_INSTALL_NAME"] =~ /^jruby/i) unless const_defined?(:JRUBY)
DARWIN = RbConfig::CONFIG['target_os'] =~ /darwin/i unless const_defined?(:DARWIN)
LINUX = RbConfig::CONFIG['target_os'] =~ /linux/i unless const_defined?(:LINUX)
# Auto-load modules on-demand
autoload :Base, "middleman-core/base"
autoload :Cache, "middleman-core/cache"
@ -181,36 +167,6 @@ module Middleman
EXTENSION_FILE = File.join("lib", "middleman_extension.rb") unless const_defined?(:EXTENSION_FILE)
class << self
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def recursively_enhance(data)
if data.is_a? Hash
data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
# Normalize a path to not include a leading slash
# @param [String] path
# @return [String]
def normalize_path(path)
path.sub(/^\//, "").gsub("%20", " ")
end
# Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE
#
@ -296,4 +252,4 @@ module Middleman
server
end
end
end
end

View file

@ -101,7 +101,7 @@ module Middleman::CoreExtensions::Data
return
end
@local_data[basename] = ::Middleman.recursively_enhance(data)
@local_data[basename] = ::Middleman::Util.recursively_enhance(data)
end
# Remove a given file from the internal cache
@ -144,7 +144,7 @@ module Middleman::CoreExtensions::Data
result = data_for_path(path)
if result
return ::Middleman.recursively_enhance(result)
return ::Middleman::Util.recursively_enhance(result)
end
end

View file

@ -153,7 +153,7 @@ module Middleman::CoreExtensions::FrontMatter
if result
data, content = result
data = ::Middleman.recursively_enhance(data).freeze
data = ::Middleman::Util.recursively_enhance(data).freeze
file = file.sub(@app.source_dir, "")
@local_data[file] = [data, content]
path = File.join(@app.source_dir, file)

View file

@ -49,7 +49,7 @@ module Middleman::Sitemap::Extensions
if path.is_a? Regexp
@ignored_callbacks << Proc.new {|p| p =~ path }
elsif path.is_a? String
path_clean = ::Middleman.normalize_path(path)
path_clean = ::Middleman::Util.normalize_path(path)
if path_clean.include?("*") # It's a glob
@ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) }
else
@ -66,7 +66,7 @@ module Middleman::Sitemap::Extensions
# @param [String] path
# @return [Boolean]
def ignored?(path)
path_clean = ::Middleman.normalize_path(path)
path_clean = ::Middleman::Util.normalize_path(path)
@ignored_callbacks.any? { |b| b.call(path_clean) }
end

View file

@ -75,7 +75,7 @@ module Middleman::Sitemap::Extensions
# @param [String] target
# @return [void]
def proxy(path, target)
@proxy_paths[::Middleman.normalize_path(path)] = ::Middleman.normalize_path(target)
@proxy_paths[::Middleman::Util.normalize_path(path)] = ::Middleman::Util.normalize_path(target)
@app.sitemap.rebuild_resource_list!(:added_proxy)
end

View file

@ -65,7 +65,7 @@ module Middleman::Sitemap
# @param [String] request_path The original path of a resource.
# @return [Middleman::Sitemap::Resource]
def find_resource_by_path(request_path)
request_path = ::Middleman.normalize_path(request_path)
request_path = ::Middleman::Util.normalize_path(request_path)
@_lookup_cache[:path][request_path]
end
@ -73,7 +73,7 @@ module Middleman::Sitemap
# @param [String] request_path The destination (output) path of a resource.
# @return [Middleman::Sitemap::Resource]
def find_resource_by_destination_path(request_path)
request_path = ::Middleman.normalize_path(request_path)
request_path = ::Middleman::Util.normalize_path(request_path)
@_lookup_cache[:destination_path][request_path]
end

View file

@ -0,0 +1,35 @@
# Using Thor's indifferent hash access
require "thor"
module Middleman
module Util
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def self.recursively_enhance(data)
if data.is_a? Hash
data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
# Normalize a path to not include a leading slash
# @param [String] path
# @return [String]
def self.normalize_path(path)
path.sub(/^\//, "").gsub("%20", " ")
end
end
end

View file

@ -1,6 +1,10 @@
# File changes are forwarded to the currently running app via HTTP
require "net/http"
module Middleman
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
end
require "win32/process" if ::Middleman::WINDOWS
require "fileutils"