class Middleman::CoreExtensions::Data::DataStore

The core logic behind the data extension.

Public Class Methods

new(app, data_file_matcher) click to toggle source

Setup data store

@param [Middleman::Application] app The current instance of Middleman

# File lib/middleman-core/core_extensions/data.rb, line 56
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

Public Instance Methods

[](key) click to toggle source

Make DataStore act like a hash. Return requested data, or nil if data does not exist

@param [String, Symbol] key The name of the data namespace @return [Hash, nil]

# File lib/middleman-core/core_extensions/data.rb, line 191
def [](key)
  __send__(key) if key?(key)
end
callbacks(name=nil, proc=nil) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 82
def callbacks(name=nil, proc=nil)
  @callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
  @callback_sources
end
data_for_path(path) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 154
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
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 195
def key?(key)
  (@local_data.keys + @local_sources.keys + @callback_sources.keys).include?(key.to_s)
end
Also aliased as: has_key?
method_missing(path) click to toggle source

“Magically” find namespaces of data if they exist

@param [String] path The namespace to search for @return [Hash, nil]

Calls superclass method
# File lib/middleman-core/core_extensions/data.rb, line 168
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
remove_file(file) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 132
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
respond_to?(method, include_private=false) click to toggle source

Needed so that method_missing makes sense

Calls superclass method
# File lib/middleman-core/core_extensions/data.rb, line 182
def respond_to?(method, include_private=false)
  super || key?(method)
end
store(name=nil, content=nil) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 71
def store(name=nil, content=nil)
  @local_sources[name.to_s] = content unless name.nil? || content.nil?
  @local_sources
end
to_h() click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 205
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
touch_file(file) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 100
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
update_files(updated_files, removed_files) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 88
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