Data ext doesn't need to touch the app obj

This commit is contained in:
Thomas Reynolds 2014-07-05 16:05:00 -07:00
parent 300ef8d8fe
commit 60bbe44e0e
2 changed files with 13 additions and 11 deletions

View file

@ -9,32 +9,32 @@ module Middleman
# The regex which tells Middleman which files are for data # The regex which tells Middleman which files are for data
MATCHER = /[\w-]+\.(yml|yaml|json)$/ MATCHER = /[\w-]+\.(yml|yaml|json)$/
attr_reader :data_store
def initialize(app, options_hash={}, &block) def initialize(app, options_hash={}, &block)
super super
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 @data_store = DataStore.new(app)
app.send :include, InstanceMethods
end end
def before_configuration def before_configuration
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
app.add_to_config_context :data, &method(:data_store)
# 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
app.files.changed MATCHER do |file| app.files.changed MATCHER do |file|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/") extensions[:data].data_store.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
end end
app.files.deleted MATCHER do |file| app.files.deleted MATCHER do |file|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/") extensions[:data].data_store.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end end
end end
module InstanceMethods helpers do
# The data object
#
# @return [DataStore]
def data def data
@_data ||= DataStore.new(self) extensions[:data].data_store
end end
end end

View file

@ -11,11 +11,13 @@ module Middleman
end end
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
return resources unless app.extensions[:data]
resources.each do |resource| resources.each do |resource|
next unless resource.source_file =~ %r{\.liquid$} next unless resource.source_file =~ %r{\.liquid$}
# Convert data object into a hash for liquid # Convert data object into a hash for liquid
resource.add_metadata locals: { data: app.data.to_h } resource.add_metadata locals: { data: app.extensions[:data].data_store.to_h }
end end
end end
end end