class Middleman::CoreExtensions::Data::DataStore

The core logic behind the data extension.

def [](key)

Returns:
  • (Hash, nil) -

Parameters:
  • key (String, Symbol) -- The name of the data namespace
def [](key)
  __send__(key) if key?(key)
end

def callbacks(name=nil, proc=nil)

def callbacks(name=nil, proc=nil)
  @callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
  @callback_sources
end

def data_for_path(path)

def data_for_path(path)
  response = if store.key?(path.to_s)
    store[path.to_s]
  elsif callbacks.key?(path.to_s)
    callbacks[path.to_s].call
  end
  ::Middleman::Util.recursively_enhance(response)
end

def initialize(app, data_file_matcher)

Parameters:
  • app (Middleman::Application) -- The current instance of Middleman
def initialize(app, data_file_matcher)
  @app = app
  @data_file_matcher = data_file_matcher
  @local_data = {}
  @local_data_enhanced = nil
  @local_sources = {}
  @callback_sources = {}
end

def key?(key)

def key?(key)
  (@local_data.keys + @local_sources.keys + @callback_sources.keys).include?(key.to_s)
end

def method_missing(path)

Returns:
  • (Hash, nil) -

Parameters:
  • path (String) -- The namespace to search for
def method_missing(path)
  if @local_data.key?(path.to_s)
    # Any way to cache this?
    @local_data_enhanced ||= ::Middleman::Util.recursively_enhance(@local_data)
    return @local_data_enhanced[path.to_s]
  else
    result = data_for_path(path)
    return result if result
  end
  super
end

def remove_file(file)

def remove_file(file)
  data_path = file[:relative_path]
  extension = File.extname(data_path)
  basename  = File.basename(data_path, extension)
  data_branch = @local_data
  path = data_path.to_s.split(File::SEPARATOR)[0..-2]
  path.each do |dir|
    data_branch = data_branch[dir]
  end
  data_branch.delete(basename) if data_branch.key?(basename)
  @local_data_enhanced = nil
end

def respond_to?(method, include_private=false)

Needed so that method_missing makes sense
def respond_to?(method, include_private=false)
  super || key?(method)
end

def store(name=nil, content=nil)

def store(name=nil, content=nil)
  @local_sources[name.to_s] = content unless name.nil? || content.nil?
  @local_sources
end

def to_h

def to_h
  data = {}
  store.each_key do |k|
    data[k] = data_for_path(k)
  end
  callbacks.each_key do |k|
    data[k] = data_for_path(k)
  end
  (@local_data || {}).each do |k, v|
    data[k] = v
  end
  data
end

def touch_file(file)

def touch_file(file)
  data_path = file[:relative_path]
  extension = File.extname(data_path)
  basename  = File.basename(data_path, extension)
  return unless %w(.yaml .yml .json).include?(extension)
  if %w(.yaml .yml).include?(extension)
    data, postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :yaml)
    data[:postscript] = postscript if !postscript.nil? && data.is_a?(Hash)
  elsif extension == '.json'
    data, _postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :json)
  end
  data_branch = @local_data
  path = data_path.to_s.split(File::SEPARATOR)[0..-2]
  path.each do |dir|
    data_branch[dir] ||= {}
    data_branch = data_branch[dir]
  end
  data_branch[basename] = data
  @local_data_enhanced = nil
end

def update_files(updated_files, removed_files)

def update_files(updated_files, removed_files)
  updated_files.each(&method(:touch_file))
  removed_files.each(&method(:remove_file))
  @app.sitemap.rebuild_resource_list!(:touched_data_file)
end