Convert external data to a real extension

This commit is contained in:
Ben Hollis 2014-05-25 18:43:49 -07:00
parent e649bc2809
commit fed95f9c5e
3 changed files with 36 additions and 47 deletions

View file

@ -142,9 +142,6 @@ module Middleman
# Handle exceptions # Handle exceptions
include Middleman::CoreExtensions::ShowExceptions include Middleman::CoreExtensions::ShowExceptions
# Activate Data package
include Middleman::CoreExtensions::Data
# Setup custom rendering # Setup custom rendering
include Middleman::CoreExtensions::Rendering include Middleman::CoreExtensions::Rendering
@ -188,6 +185,7 @@ module Middleman
# Parse YAML from templates. Must be before sitemap so sitemap # Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter! # extensions see updated frontmatter!
activate :front_matter activate :front_matter
activate :data
activate :file_watcher activate :file_watcher
# Initialize the Sitemap # Initialize the Sitemap

View file

@ -6,7 +6,10 @@ end
# Data looks at the data/ folder for YAML files and makes them available # Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests. # to dynamic requests.
Middleman::Extensions.register :data do
require 'middleman-core/core_extensions/data' require 'middleman-core/core_extensions/data'
Middleman::CoreExtensions::Data
end
# Parse YAML from templates # Parse YAML from templates
Middleman::Extensions.register :front_matter do Middleman::Extensions.register :front_matter do

View file

@ -1,37 +1,35 @@
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the data/ directory
# and makes them available to config.rb, templates and extensions
module Data
# Extension registered
class << self
# @private
def included(app)
# Data formats
require 'yaml' require 'yaml'
require 'active_support/json' require 'active_support/json'
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the `data/` directory
# and makes them available to `config.rb`, templates and extensions
class Data < Extension
# The regex which tells Middleman which files are for data
MATCHER = /[\w-]+\.(yml|yaml|json)$/
def initialize(app, options_hash={}, &block)
super
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in' app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
# Directly include the #data method instead of using helpers so that this is available immediately
app.send :include, InstanceMethods app.send :include, InstanceMethods
end end
end
# Instance methods def before_configuration
module InstanceMethods
# Setup data files before anything else so they are available when # Setup data files before anything else so they are available when
# parsing config.rb # parsing config.rb
def initialize app.files.changed MATCHER do |file|
files.changed DataStore.matcher do |file|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/") data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
end end
files.deleted DataStore.matcher do |file| app.files.deleted MATCHER do |file|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/") data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end end
super
end end
module InstanceMethods
# The data object # The data object
# #
# @return [DataStore] # @return [DataStore]
@ -42,25 +40,14 @@ module Middleman
# The core logic behind the data extension. # The core logic behind the data extension.
class DataStore class DataStore
# Static methods
class << self
# The regex which tells Middleman which files are for data
#
# @return [Regexp]
def matcher
%r{[\w-]+\.(yml|yaml|json)$}
end
end
# Store static data hash # Store static data hash
# #
# @param [Symbol] name Name of the data, used for namespacing # @param [Symbol] name Name of the data, used for namespacing
# @param [Hash] content The content for this data # @param [Hash] content The content for this data
# @return [Hash] # @return [Hash]
def store(name=nil, content=nil) def store(name=nil, content=nil)
@_local_sources ||= {} @local_sources[name.to_s] = content unless name.nil? || content.nil?
@_local_sources[name.to_s] = content unless name.nil? || content.nil? @local_sources
@_local_sources
end end
# Store callback-based data # Store callback-based data
@ -69,9 +56,8 @@ module Middleman
# @param [Proc] proc The callback which will return data # @param [Proc] proc The callback which will return data
# @return [Hash] # @return [Hash]
def callbacks(name=nil, proc=nil) def callbacks(name=nil, proc=nil)
@_callback_sources ||= {} @callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
@_callback_sources[name.to_s] = proc unless name.nil? || proc.nil? @callback_sources
@_callback_sources
end end
# Setup data store # Setup data store
@ -80,6 +66,8 @@ module Middleman
def initialize(app) def initialize(app)
@app = app @app = app
@local_data = {} @local_data = {}
@local_sources = {}
@callback_sources = {}
end end
# Update the internal cache for a given file path # Update the internal cache for a given file path