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 "yaml"
require "httparty"
require "thor" require "thor"
module Middleman::CoreExtensions::Data module Middleman::CoreExtensions::Data
@ -27,12 +26,12 @@ module Middleman::CoreExtensions::Data
response = nil response = nil
@@local_sources ||= {} @@local_sources ||= {}
@@remote_sources ||= {} @@callback_sources ||= {}
if @@local_sources.has_key?(path.to_s) if @@local_sources.has_key?(path.to_s)
response = @@local_sources[path.to_s] response = @@local_sources[path.to_s]
elsif @@remote_sources.has_key?(path.to_s) elsif @@callback_sources.has_key?(path.to_s)
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response response = @@callback_sources[path.to_s].call()
else else
file_path = File.join(@app.class.root, @app.class.data_dir, "#{path}.yml") file_path = File.join(@app.class.root, @app.class.data_dir, "#{path}.yml")
if File.exists? file_path if File.exists? file_path
@ -44,16 +43,16 @@ module Middleman::CoreExtensions::Data
recursively_enhance(response) recursively_enhance(response)
end end
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) def self.data_content(name, content)
@@local_sources ||= {} @@local_sources ||= {}
@@local_sources[name.to_s] = content @@local_sources[name.to_s] = content
end end
def self.data_callback(name, proc)
@@callback_sources ||= {}
@@callback_sources[name.to_s] = proc
end
private private
def recursively_enhance(data) def recursively_enhance(data)
@ -75,19 +74,14 @@ module Middleman::CoreExtensions::Data
end end
module ClassMethods module ClassMethods
# Makes HTTP json data available in the data object # Makes a hash available on the data var with a given name
#
# 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
def data_content(name, content) def data_content(name, content)
DataObject.data_content(name, content) DataObject.data_content(name, content)
end 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
end end

View file

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

View file

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