Class: Middleman::CoreExtensions::Data::Stores::LocalFileDataStore

Inherits:
BaseDataStore
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb

Overview

JSON and YAML files in the data/ directory

Constant Summary collapse

YAML_EXTS =
Set.new %w[.yaml .yml]
JSON_EXTS =
Set.new %w[.json]
TOML_EXTS =
Set.new %w[.toml]
ALL_EXTS =
YAML_EXTS | JSON_EXTS | TOML_EXTS

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Method Summary collapse

Methods included from Contracts

#Contract

Methods inherited from BaseDataStore

#[], #key?, #keys, #to_h, #vertices

Constructor Details

#initialize(app) ⇒ LocalFileDataStore

Contract IsA['::Middleman::Application'] => Any


27
28
29
30
31
32
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 27

def initialize(app)
  super()

  @app = app
  @local_data = {}
end

Instance Method Details

#remove_file(file) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 79

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.to_sym]
  end

  data_branch.delete(basename.to_sym) if data_branch.key?(basename.to_sym)
end

#touch_file(file) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 47

def touch_file(file)
  data_path = file[:relative_path]
  extension = File.extname(data_path)
  basename  = File.basename(data_path, extension)

  return unless ALL_EXTS.include?(extension)

  if YAML_EXTS.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 JSON_EXTS.include?(extension)
    data, _postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :json)
  elsif TOML_EXTS.include?(extension)
    data, _postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :toml)
  end

  data_branch = @local_data

  paths = data_path.to_s.split(File::SEPARATOR)[0..-2]
  paths.each do |dir|
    data_branch[dir.to_sym] ||= {}
    data_branch = data_branch[dir.to_sym]
  end

  data_branch[basename.to_sym] = data
end

#update_files(updated_files, removed_files) ⇒ Object


35
36
37
38
39
40
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 35

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