class Middleman::CoreExtensions::Data::DataStore
The core logic behind the data extension.
def [](key)
-
(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)
-
(Hash)
-
Parameters:
-
proc
(Proc
) -- The callback which will return data -
name
(Symbol
) -- Name of the data, used for namespacing
def callbacks(name=nil, proc=nil) @_callback_sources ||= {} @_callback_sources[name.to_s] = proc unless name.nil? || proc.nil? @_callback_sources end
def data_for_path(path)
-
(Hash, nil)
-
Parameters:
-
path
(String, Symbol
) -- The name of the data namespace
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
def initialize(app)
-
app
(Middleman::Application
) -- The current instance of Middleman
def initialize(app) @app = app @local_data = {} end
def key?(key)
def key?(key) @local_data.key?(key.to_s) || data_for_path(key) end
def matcher
-
(Regexp)
-
def matcher %r{[\w-]+\.(yml|yaml|json)$} end
def method_missing(path)
-
(Hash, nil)
-
Parameters:
-
path
(String
) -- The namespace to search for
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
def remove_file(file)
-
(void)
-
Parameters:
-
file
(String
) -- The file to be cleared
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
def respond_to?(method, include_private=false)
def respond_to?(method, include_private=false) super || key?(method) end
def store(name=nil, content=nil)
-
(Hash)
-
Parameters:
-
content
(Hash
) -- The content for this data -
name
(Symbol
) -- Name of the data, used for namespacing
def store(name=nil, content=nil) @_local_sources ||= {} @_local_sources[name.to_s] = content unless name.nil? || content.nil? @_local_sources end
def to_h
-
(Hash)
-
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)
-
(void)
-
Parameters:
-
file
(String
) -- The file to be re-parsed
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