middleman/lib/middleman/core_extensions/data.rb

92 lines
2 KiB
Ruby
Raw Normal View History

2011-04-15 00:35:41 +02:00
require "yaml"
require "httparty"
require "thor"
2011-04-15 00:35:41 +02:00
module Middleman::CoreExtensions::Data
2011-04-15 00:35:41 +02:00
class << self
def registered(app)
app.extend ClassMethods
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-07-06 19:29:07 +02:00
@@data ||= DataObject.new(self)
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)
response = nil
@@local_sources ||= {}
@@remote_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
else
2011-07-06 19:29:07 +02:00
file_path = File.join(@app.class.root, "data", "#{path}.yml")
if File.exists? file_path
response = YAML.load_file(file_path)
end
end
if response
recursively_enhance(response)
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
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
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)
2011-07-06 19:29:07 +02:00
DataObject.add_source(name, url)
end
def data_content(name, content)
2011-07-06 19:29:07 +02:00
DataObject.data_content(name, content)
end
end
2011-04-15 00:35:41 +02:00
end