Make indifferent access easy to use anywhere. Change frontmatter.data to plain old

This commit is contained in:
Thomas Reynolds 2011-12-26 11:24:33 -08:00
parent b9c5c89e40
commit 6ef32652c7
4 changed files with 41 additions and 39 deletions

View file

@ -1,5 +1,8 @@
require "rbconfig"
# Using Thor's indifferent hash access
require "thor"
# Setup our load paths
libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
@ -163,6 +166,28 @@ module Middleman
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
# Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE
#

View file

@ -2,9 +2,6 @@
require "yaml"
require "active_support/json"
# Using Thor's indifferent hash access
require "thor"
# The data extension parses YAML and JSON files in the data/ directory
# and makes them available to config.rb, templates and extensions
module Middleman::CoreExtensions::Data
@ -119,7 +116,7 @@ module Middleman::CoreExtensions::Data
return
end
@local_data[basename] = recursively_enhance(data)
@local_data[basename] = ::Middleman.recursively_enhance(data)
end
# Remove a given file from the internal cache
@ -162,7 +159,7 @@ module Middleman::CoreExtensions::Data
result = data_for_path(path)
if result
return recursively_enhance(result)
return ::Middleman.recursively_enhance(result)
end
end
@ -192,28 +189,5 @@ module Middleman::CoreExtensions::Data
data
end
private
# 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
end
end

View file

@ -28,18 +28,18 @@ module Middleman::CoreExtensions::FrontMatter
matcher = %r{source/.*(#{exts})}
file_changed matcher do |file|
frontmatter.touch_file(file)
frontmatter_extension.touch_file(file)
end
file_deleted matcher do |file|
frontmatter.remove_file(file)
frontmatter_extension.remove_file(file)
end
provides_metadata matcher do |path|
relative_path = path.sub(source_dir, "")
fmdata = if frontmatter.has_data?(relative_path)
frontmatter.data(relative_path)[0]
fmdata = if frontmatter_extension.has_data?(relative_path)
frontmatter(relative_path)[0]
else
{}
end
@ -61,8 +61,12 @@ module Middleman::CoreExtensions::FrontMatter
end
end
def frontmatter
@frontmatter ||= FrontMatter.new(self)
def frontmatter_extension
@_frontmatter_extension ||= FrontMatter.new(self)
end
def frontmatter(*args)
frontmatter_extension.data(*args)
end
end
@ -88,8 +92,10 @@ module Middleman::CoreExtensions::FrontMatter
result = parse_front_matter(content)
if result
data, content = result
data = ::Middleman.recursively_enhance(data)
file = file.sub(@app.source_dir, "")
@local_data[file] = result
@local_data[file] = [data, content]
path = File.join(@app.source_dir, file)
@app.cache.set([:raw_template, path], result[1])
@app.frontmatter_did_change(path)

View file

@ -118,13 +118,10 @@ module Middleman::Sitemap
end
def data
data, content = app.frontmatter.data(relative_path)
data, content = app.frontmatter(relative_path)
data || nil
end
def parent
parts = path.split("/")
if path.include?(app.index_file)