Clean up whitespace, play with cane for testing style quality and code complexity

This commit is contained in:
Thomas Reynolds 2012-08-13 15:39:06 -07:00
parent 955c66a54f
commit 446aa6a4ba
133 changed files with 895 additions and 871 deletions

View file

@ -1,10 +1,10 @@
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the data/ directory
# and makes them available to config.rb, templates and extensions
module Data
# Extension registered
class << self
# @private
@ -12,13 +12,13 @@ module Middleman
# Data formats
require "yaml"
require "active_support/json"
app.set :data_dir, "data"
app.send :include, InstanceMethods
end
alias :included :registered
end
# Instance methods
module InstanceMethods
# Setup data files before anything else so they are available when
@ -27,14 +27,14 @@ module Middleman
self.files.changed DataStore.matcher do |file|
self.data.touch_file(file) if file.match(%r{^#{self.data_dir}\/})
end
self.files.deleted DataStore.matcher do |file|
self.data.remove_file(file) if file.match(%r{^#{self.data_dir}\/})
end
super
end
# The data object
#
# @return [DataStore]
@ -42,13 +42,13 @@ module Middleman
@_data ||= DataStore.new(self)
end
end
# The core logic behind the data extension.
class DataStore
# Static methods
class << self
# The regex which tells Middleman which files are for data
#
# @return [Regexp]
@ -56,7 +56,7 @@ module Middleman
%r{[\w-]+\.(yml|yaml|json)$}
end
end
# Store static data hash
#
# @param [Symbol] name Name of the data, used for namespacing
@ -78,7 +78,7 @@ module Middleman
@_callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
@_callback_sources
end
# Setup data store
#
# @param [Middleman::Application] app The current instance of Middleman
@ -86,7 +86,7 @@ module Middleman
@app = app
@local_data = {}
end
# Update the internal cache for a given file path
#
# @param [String] file The file to be re-parsed
@ -95,7 +95,7 @@ module Middleman
file = File.expand_path(file, @app.root)
extension = File.extname(file)
basename = File.basename(file, extension)
if %w(.yaml .yml).include?(extension)
data = YAML.load_file(file)
elsif extension == ".json"
@ -106,7 +106,7 @@ module Middleman
@local_data[basename] = ::Middleman::Util.recursively_enhance(data)
end
# Remove a given file from the internal cache
#
# @param [String] file The file to be cleared
@ -116,26 +116,26 @@ module Middleman
basename = File.basename(file, extension)
@local_data.delete(basename) if @local_data.has_key?(basename)
end
# Get a hash hash from either internal static data or a callback
#
# @param [String, Symbol] path The name of the data namespace
# @return [Hash, nil]
def data_for_path(path)
response = nil
@@local_sources ||= {}
@@callback_sources ||= {}
if self.store.has_key?(path.to_s)
response = self.store[path.to_s]
elsif self.callbacks.has_key?(path.to_s)
response = self.callbacks[path.to_s].call()
end
response
end
# "Magically" find namespaces of data if they exist
#
# @param [String] path The namespace to search for
@ -145,36 +145,36 @@ module Middleman
return @local_data[path.to_s]
else
result = data_for_path(path)
if result
return ::Middleman::Util.recursively_enhance(result)
end
end
super
end
# Convert all the data into a static hash
#
# @return [Hash]
def to_h
data = {}
self.store.each do |k, v|
data[k] = data_for_path(k)
end
self.callbacks.each do |k, v|
data[k] = data_for_path(k)
end
(@local_data || {}).each do |k, v|
data[k] = v
end
data
end
end
end
end
end
end