class Middleman::CoreExtensions::Data::DataStore

The core logic behind the data extension.

Public Class Methods

matcher() click to toggle source

The regex which tells Middleman which files are for data

@return [Regexp]

# File lib/middleman-core/core_extensions/data.rb, line 51
def matcher
  %r{[\w-]+\.(yml|yaml|json)$}
end
new(app) 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 81
def initialize(app)
  @app = app
  @local_data = {}
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 180
def [](key)
  __send__(key) if key?(key)
end
callbacks(name=nil, proc=nil) click to toggle source

Store callback-based data

@param [Symbol] name Name of the data, used for namespacing @param [Proc] proc The callback which will return data @return [Hash]

# File lib/middleman-core/core_extensions/data.rb, line 72
def callbacks(name=nil, proc=nil)
  @_callback_sources ||= {}
  @_callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
  @_callback_sources
end
data_for_path(path) click to toggle source

Get a 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 143
def data_for_path(path)
  response = nil

  if store.key?(path.to_s)
    response = store[path.to_s]
  elsif callbacks.key?(path.to_s)
    response = callbacks[path.to_s].call
  end

  response
end
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/middleman-core/core_extensions/data.rb, line 184
def key?(key)
  @local_data.key?(key.to_s) || data_for_path(key)
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 159
def method_missing(path)
  if @local_data.key?(path.to_s)
    return @local_data[path.to_s]
  else
    result = data_for_path(path)
    return ::Middleman::Util.recursively_enhance(result) if result
  end

  super
end
remove_file(file) click to toggle source

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 121
def remove_file(file)
  root = Pathname(@app.root)
  full_path = root + file
  extension = File.extname(file)
  basename  = File.basename(file, extension)

  data_path = full_path.relative_path_from(root + @app.config[:data_dir])

  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
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 171
def respond_to?(method, include_private=false)
  super || key?(method)
end
store(name=nil, content=nil) click to toggle source

Store static data hash

@param [Symbol] name Name of the data, used for namespacing @param [Hash] content The content for this data @return [Hash]

# File lib/middleman-core/core_extensions/data.rb, line 61
def store(name=nil, content=nil)
  @_local_sources ||= {}
  @_local_sources[name.to_s] = content unless name.nil? || content.nil?
  @_local_sources
end
to_h() click to toggle source

Convert all the data into a static hash

@return [Hash]

# File lib/middleman-core/core_extensions/data.rb, line 193
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
touch_file(file) click to toggle source

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 90
def touch_file(file)
  root = Pathname(@app.root)
  full_path = root + file
  extension = File.extname(file)
  basename  = File.basename(file, extension)

  data_path = full_path.relative_path_from(root + @app.config[:data_dir])

  if %w(.yaml .yml).include?(extension)
    data = YAML.load_file(full_path)
  elsif extension == '.json'
    data = ActiveSupport::JSON.decode(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] ||= ::Middleman::Util.recursively_enhance({})
    data_branch = data_branch[dir]
  end

  data_branch[basename] = ::Middleman::Util.recursively_enhance(data)
end