abstract data api so extensions can use it

This commit is contained in:
Thomas Reynolds 2011-08-20 16:38:45 -07:00
parent 6636f23495
commit 8de3d2b35d
3 changed files with 15 additions and 22 deletions

View file

@ -1,5 +1,4 @@
require "yaml"
require "httparty"
require "thor"
module Middleman::CoreExtensions::Data
@ -27,12 +26,12 @@ module Middleman::CoreExtensions::Data
response = nil
@@local_sources ||= {}
@@remote_sources ||= {}
@@callback_sources ||= {}
if @@local_sources.has_key?(path.to_s)
response = @@local_sources[path.to_s]
elsif @@remote_sources.has_key?(path.to_s)
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
elsif @@callback_sources.has_key?(path.to_s)
response = @@callback_sources[path.to_s].call()
else
file_path = File.join(@app.class.root, @app.class.data_dir, "#{path}.yml")
if File.exists? file_path
@ -45,16 +44,16 @@ module Middleman::CoreExtensions::Data
end
end
def self.add_source(name, json_url)
@@remote_sources ||= {}
@@remote_sources[name.to_s] = json_url
end
def self.data_content(name, content)
@@local_sources ||= {}
@@local_sources[name.to_s] = content
end
def self.data_callback(name, proc)
@@callback_sources ||= {}
@@callback_sources[name.to_s] = proc
end
private
def recursively_enhance(data)
if data.is_a? Hash
@ -75,19 +74,14 @@ module Middleman::CoreExtensions::Data
end
module ClassMethods
# Makes HTTP json data available in the data object
#
# data_source :my_json, "http://my/file.json"
#
# Available in templates as:
#
# data.my_json
def data_source(name, url)
DataObject.add_source(name, url)
end
# Makes a hash available on the data var with a given name
def data_content(name, content)
DataObject.data_content(name, content)
end
# Makes a hash available on the data var with a given name
def data_callback(name, &block)
DataObject.data_callback(name, block)
end
end
end

View file

@ -1,3 +1,3 @@
module Middleman
VERSION = "2.0.3"
VERSION = "2.0.4"
end

View file

@ -53,7 +53,6 @@ eos
s.add_runtime_dependency("compass", ["~> 0.11.3"])
s.add_runtime_dependency("coffee-script", ["~> 2.2.0"])
s.add_runtime_dependency("sprockets", ["2.0.0.beta.14"])
s.add_runtime_dependency("httparty", ["~> 0.7.8"])
case Config::CONFIG['host_os'].downcase
when %r{mswin|mingw}