From 055f7c2a4163fb7218741f32ce6506b29380c830 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 9 Nov 2011 21:19:11 -0800 Subject: [PATCH] refactor data to use guard hooks --- lib/middleman/core_extensions/data.rb | 83 ++++++++++++++++-------- lib/middleman/core_extensions/sitemap.rb | 58 ++++++----------- 2 files changed, 74 insertions(+), 67 deletions(-) diff --git a/lib/middleman/core_extensions/data.rb b/lib/middleman/core_extensions/data.rb index 44ebae10..85af4c52 100755 --- a/lib/middleman/core_extensions/data.rb +++ b/lib/middleman/core_extensions/data.rb @@ -8,6 +8,19 @@ module Middleman::CoreExtensions::Data app.set :data_dir, "data" app.extend ClassMethods app.helpers Helpers + + app.file_changed do |file| + if file.match(%r{^#{settings.data_dir}\/[\w-]+\.(yml|yaml|json)}) + data.touch_file(file) + end + end + + app.file_deleted do |file| + if file.match(%r{^#{settings.data_dir}\/[\w-]+\.(yml|yaml|json)}) + data.remove_file(file) + end + end + end alias :included :registered end @@ -21,6 +34,37 @@ module Middleman::CoreExtensions::Data class DataObject def initialize(app) @app = app + @local_data = {} + setup + end + + def setup + data_path = File.join(@app.root, @app.data_dir) + local_path = File.join(data_path, "*.{yaml,yml,json}") + Dir[local_path].each do |f| + touch_file(f)#.sub(data_path, "")) + end + end + + def touch_file(file) + extension = File.extname(file) + basename = File.basename(file, extension) + + if %w(.yaml .yml).include?(extension) + data = YAML.load_file(file) + elsif extension == ".json" + data = ActiveSupport::JSON.decode(File.read(file)) + else + return + end + + @local_data[basename] = recursively_enhance(data) + end + + def remove_file(file) + extension = File.extname(file) + basename = File.basename(file, extension) + @local_data.delete(basename) if @local_data.has_key?(basename) end def data_for_path(path) @@ -33,32 +77,23 @@ module Middleman::CoreExtensions::Data response = @@local_sources[path.to_s] elsif @@callback_sources.has_key?(path.to_s) response = @@callback_sources[path.to_s].call() - else - file_path = File.join(@app.root, @app.data_dir, "#{path}.yml") - if File.exists? file_path - response = YAML.load_file(file_path) - else - file_path = File.join(@app.root, @app.data_dir, "#{path}.yaml") - if File.exists? file_path - response = YAML.load_file(file_path) - else - file_path = File.join(@app.root, @app.data_dir, "#{path}.json") - response = ActiveSupport::JSON.decode(File.read(file_path)) if File.exists? file_path - end - end end response end def method_missing(path) - result = data_for_path(path) - - if result - recursively_enhance(result) + if @local_data.has_key?(path.to_s) + return @local_data[path.to_s] else - super + result = data_for_path(path) + + if result + return recursively_enhance(result) + end end + + super end def to_h @@ -75,16 +110,8 @@ module Middleman::CoreExtensions::Data data[k] = data_for_path(k) end - yaml_path = File.join(@app.root, @app.data_dir, "*.{yaml,yml}") - Dir[yaml_path].each do |f| - p = f.split("/").last.gsub(".yml", "").gsub(".yaml", "") - data[p] = data_for_path(p) - end - - json_path = File.join(@app.root, @app.data_dir, "*.json") - Dir[json_path].each do |f| - p = f.split("/").last.gsub(".json", "") - data[p] = data_for_path(p) + (@local_data || {}).each do |k, v| + data[k] = v end data diff --git a/lib/middleman/core_extensions/sitemap.rb b/lib/middleman/core_extensions/sitemap.rb index 06a38b02..544926a3 100644 --- a/lib/middleman/core_extensions/sitemap.rb +++ b/lib/middleman/core_extensions/sitemap.rb @@ -1,73 +1,53 @@ require 'find' -require 'hooks' module Middleman::CoreExtensions::Sitemap class << self def registered(app) - sm = SitemapStore.new - - app.set :sitemap, sm - - app.initialized do |scope| - sm.setup(scope) - end + app.extend ClassMethods + app.helpers Helpers app.file_changed do |file| - sm.touch_file(file) + sitemap.touch_file(file) end app.file_deleted do |file| - sm.remove_file(file) + sitemap.remove_file(file) end - - app.extend ClassMethods end alias :included :registered end + module Helpers + def sitemap + self.class.sitemap + end + end + module ClassMethods + def sitemap + @sitemap ||= SitemapStore.new(self) + end + # Keep a path from building def ignore(path) - action = Proc.new { ignore_path(path) } - - if sitemap.setup? - action.call - else - sitemap.class.after_setup(&action) - end + sitemap.ignore_path(path) end def reroute(url, target) - action = Proc.new { set_path(url, target) } - - if sitemap.setup? - action.call - else - sitemap.class.after_setup(&action) - end + sitemap.set_path(url, target) end end class SitemapStore - include ::Hooks - define_hook :after_setup - - def initialize + def initialize(app) + @app = app + @source = File.expand_path(@app.views, @app.root) @map = {} @ignored_paths = false @generic_paths = false @proxied_paths = false - @is_setup = false - end - - def setup(app) - @app = app - @source = File.expand_path(@app.views, @app.root) build_static_map - - run_hook :after_setup - @is_setup = true end def setup?