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
  response = ::Middleman::Util.recursively_enhance(response)
  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_sources = {}
  @callback_sources = {}
end

def key?(key)

def key?(key)
  @local_data.key?(key.to_s) || data_for_path(key)
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)
    @local_data[path.to_s] = ::Middleman::Util.recursively_enhance(@local_data[path.to_s])
    return @local_data[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)
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 do |k, _|
    data[k] = data_for_path(k)
  end
  callbacks.each 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)
  if %w(.yaml .yml).include?(extension)
    data = YAML.load_file(file[:full_path])
  elsif extension == '.json'
    data = ActiveSupport::JSON.decode(file[:full_path].read)
  else
    return
  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
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))
end