refactor data to use guard hooks

This commit is contained in:
Thomas Reynolds 2011-11-09 21:19:11 -08:00
parent 1b9ad20ed1
commit 055f7c2a41
2 changed files with 74 additions and 67 deletions

View file

@ -8,6 +8,19 @@ module Middleman::CoreExtensions::Data
app.set :data_dir, "data" app.set :data_dir, "data"
app.extend ClassMethods app.extend ClassMethods
app.helpers Helpers 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 end
alias :included :registered alias :included :registered
end end
@ -21,6 +34,37 @@ module Middleman::CoreExtensions::Data
class DataObject class DataObject
def initialize(app) def initialize(app)
@app = 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 end
def data_for_path(path) def data_for_path(path)
@ -33,34 +77,25 @@ module Middleman::CoreExtensions::Data
response = @@local_sources[path.to_s] response = @@local_sources[path.to_s]
elsif @@callback_sources.has_key?(path.to_s) elsif @@callback_sources.has_key?(path.to_s)
response = @@callback_sources[path.to_s].call() 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 end
response response
end end
def method_missing(path) def method_missing(path)
if @local_data.has_key?(path.to_s)
return @local_data[path.to_s]
else
result = data_for_path(path) result = data_for_path(path)
if result if result
recursively_enhance(result) return recursively_enhance(result)
else
super
end end
end end
super
end
def to_h def to_h
data = {} data = {}
@ -75,16 +110,8 @@ module Middleman::CoreExtensions::Data
data[k] = data_for_path(k) data[k] = data_for_path(k)
end end
yaml_path = File.join(@app.root, @app.data_dir, "*.{yaml,yml}") (@local_data || {}).each do |k, v|
Dir[yaml_path].each do |f| data[k] = v
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)
end end
data data

View file

@ -1,73 +1,53 @@
require 'find' require 'find'
require 'hooks'
module Middleman::CoreExtensions::Sitemap module Middleman::CoreExtensions::Sitemap
class << self class << self
def registered(app) def registered(app)
sm = SitemapStore.new app.extend ClassMethods
app.helpers Helpers
app.set :sitemap, sm
app.initialized do |scope|
sm.setup(scope)
end
app.file_changed do |file| app.file_changed do |file|
sm.touch_file(file) sitemap.touch_file(file)
end end
app.file_deleted do |file| app.file_deleted do |file|
sm.remove_file(file) sitemap.remove_file(file)
end end
app.extend ClassMethods
end end
alias :included :registered alias :included :registered
end end
module Helpers
def sitemap
self.class.sitemap
end
end
module ClassMethods module ClassMethods
def sitemap
@sitemap ||= SitemapStore.new(self)
end
# Keep a path from building # Keep a path from building
def ignore(path) def ignore(path)
action = Proc.new { ignore_path(path) } sitemap.ignore_path(path)
if sitemap.setup?
action.call
else
sitemap.class.after_setup(&action)
end
end end
def reroute(url, target) def reroute(url, target)
action = Proc.new { set_path(url, target) } sitemap.set_path(url, target)
if sitemap.setup?
action.call
else
sitemap.class.after_setup(&action)
end
end end
end end
class SitemapStore class SitemapStore
include ::Hooks def initialize(app)
define_hook :after_setup @app = app
@source = File.expand_path(@app.views, @app.root)
def initialize
@map = {} @map = {}
@ignored_paths = false @ignored_paths = false
@generic_paths = false @generic_paths = false
@proxied_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 build_static_map
run_hook :after_setup
@is_setup = true
end end
def setup? def setup?