class Middleman::CoreExtensions::Data::DataStore
The core logic behind the data extension.
Public Class Methods
The regex which tells Middleman
which files are for data
@return [Regexp]
# File lib/middleman-core/core_extensions/data.rb, line 55 def matcher %r{[\w-]+\.(yml|yaml|json)$} end
Setup data store
@param [Middleman::Application] app The current instance of Middleman
# File lib/middleman-core/core_extensions/data.rb, line 85 def initialize(app) @app = app @local_data = {} end
Public Instance Methods
Store callback-based data
@param [Symbol] name Name of the data, used for namespacing @param [Proc] proc The callback which will return data @return [void]
# File lib/middleman-core/core_extensions/data.rb, line 76 def callbacks(name=nil, proc=nil) @_callback_sources ||= {} @_callback_sources[name.to_s] = proc unless name.nil? || proc.nil? @_callback_sources end
Get a hash hash from either internal static data or a callback
@param [String, Symbol] path The name of the data namespace @return [Hash, nil]
# File lib/middleman-core/core_extensions/data.rb, line 124 def data_for_path(path) response = nil @@local_sources ||= {} @@callback_sources ||= {} if self.store.has_key?(path.to_s) response = self.store[path.to_s] elsif self.callbacks.has_key?(path.to_s) response = self.callbacks[path.to_s].call() end response end
“Magically” find namespaces of data if they exist
@param [String] path The namespace to search for @return [Hash, nil]
# File lib/middleman-core/core_extensions/data.rb, line 143 def method_missing(path) if @local_data.has_key?(path.to_s) return @local_data[path.to_s] else result = data_for_path(path) if result return ::Middleman::Util.recursively_enhance(result) end end super end
Remove a given file from the internal cache
@param [String] file The file to be cleared @return [void]
# File lib/middleman-core/core_extensions/data.rb, line 114 def remove_file(file) extension = File.extname(file) basename = File.basename(file, extension) @local_data.delete(basename) if @local_data.has_key?(basename) end
Store static data hash
@param [Symbol] name Name of the data, used for namespacing @param [Hash] content The content for this data @return [void]
# File lib/middleman-core/core_extensions/data.rb, line 65 def store(name=nil, content=nil) @_local_sources ||= {} @_local_sources[name.to_s] = content unless name.nil? || content.nil? @_local_sources end
Convert all the data into a static hash
@return [Hash]
# File lib/middleman-core/core_extensions/data.rb, line 160 def to_h data = {} self.store.each do |k, v| data[k] = data_for_path(k) end self.callbacks.each do |k, v| data[k] = data_for_path(k) end (@local_data || {}).each do |k, v| data[k] = v end data end
Update the internal cache for a given file path
@param [String] file The file to be re-parsed @return [void]
# File lib/middleman-core/core_extensions/data.rb, line 94 def touch_file(file) file = File.expand_path(file, @app.root) extension = File.extname(file) basename = File.basename(file, extension) if %w(.yaml .yml).include?(extension) data = YAML.load_file(file) elsif extension == ".json" data = ActiveSupport::JSON.decode(File.read(file)) else return end @local_data[basename] = ::Middleman::Util.recursively_enhance(data) end