2011-04-15 00:35:41 +02:00
|
|
|
require "yaml"
|
2011-04-29 20:58:07 +02:00
|
|
|
require "thor"
|
2011-04-15 00:35:41 +02:00
|
|
|
|
2011-07-06 18:50:34 +02:00
|
|
|
module Middleman::CoreExtensions::Data
|
2011-04-15 00:35:41 +02:00
|
|
|
class << self
|
|
|
|
def registered(app)
|
2011-07-06 19:30:24 +02:00
|
|
|
app.set :data_dir, "data"
|
2011-04-29 20:58:07 +02:00
|
|
|
app.extend ClassMethods
|
2011-07-06 18:50:34 +02:00
|
|
|
app.helpers Helpers
|
2011-04-15 00:35:41 +02:00
|
|
|
end
|
2011-04-15 18:57:45 +02:00
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
|
|
|
module Helpers
|
|
|
|
def data
|
2011-08-31 22:15:59 +02:00
|
|
|
self.class.data
|
2011-04-15 18:57:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DataObject
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2011-04-15 00:35:41 +02:00
|
|
|
end
|
|
|
|
|
2011-04-15 18:57:45 +02:00
|
|
|
def method_missing(path)
|
2011-04-29 20:58:07 +02:00
|
|
|
response = nil
|
|
|
|
|
2011-06-06 03:59:04 +02:00
|
|
|
@@local_sources ||= {}
|
2011-08-21 01:38:45 +02:00
|
|
|
@@callback_sources ||= {}
|
2011-04-29 20:58:07 +02:00
|
|
|
|
2011-06-06 03:59:04 +02:00
|
|
|
if @@local_sources.has_key?(path.to_s)
|
|
|
|
response = @@local_sources[path.to_s]
|
2011-08-21 01:38:45 +02:00
|
|
|
elsif @@callback_sources.has_key?(path.to_s)
|
|
|
|
response = @@callback_sources[path.to_s].call()
|
2011-06-06 03:59:04 +02:00
|
|
|
else
|
2011-08-31 22:15:59 +02:00
|
|
|
file_path = File.join(@app.root, @app.data_dir, "#{path}.yml")
|
2011-06-06 03:59:04 +02:00
|
|
|
if File.exists? file_path
|
|
|
|
response = YAML.load_file(file_path)
|
|
|
|
end
|
2011-04-29 20:58:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if response
|
|
|
|
recursively_enhance(response)
|
|
|
|
end
|
|
|
|
end
|
2011-06-06 03:59:04 +02:00
|
|
|
|
|
|
|
def self.data_content(name, content)
|
|
|
|
@@local_sources ||= {}
|
|
|
|
@@local_sources[name.to_s] = content
|
|
|
|
end
|
2011-08-21 01:38:45 +02:00
|
|
|
|
|
|
|
def self.data_callback(name, proc)
|
|
|
|
@@callback_sources ||= {}
|
|
|
|
@@callback_sources[name.to_s] = proc
|
|
|
|
end
|
2011-04-29 20:58:07 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
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
|
2011-04-15 18:57:45 +02:00
|
|
|
end
|
|
|
|
end
|
2011-04-15 00:35:41 +02:00
|
|
|
end
|
2011-04-15 18:57:45 +02:00
|
|
|
|
2011-04-29 20:58:07 +02:00
|
|
|
module ClassMethods
|
2011-08-31 22:15:59 +02:00
|
|
|
def data
|
|
|
|
@data ||= DataObject.new(self)
|
|
|
|
end
|
|
|
|
|
2011-08-21 01:38:45 +02:00
|
|
|
# Makes a hash available on the data var with a given name
|
2011-06-06 03:59:04 +02:00
|
|
|
def data_content(name, content)
|
2011-07-06 19:29:07 +02:00
|
|
|
DataObject.data_content(name, content)
|
2011-06-06 03:59:04 +02:00
|
|
|
end
|
2011-08-21 01:38:45 +02:00
|
|
|
|
|
|
|
# Makes a hash available on the data var with a given name
|
|
|
|
def data_callback(name, &block)
|
|
|
|
DataObject.data_callback(name, block)
|
|
|
|
end
|
2011-04-29 20:58:07 +02:00
|
|
|
end
|
2011-04-15 00:35:41 +02:00
|
|
|
end
|