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]
ALL_EXTS =
YAML_EXTS | JSON_EXTS

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

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



24
25
26
27
28
29
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 24

def initialize(app)
  super()

  @app = app
  @local_data = {}
end

Instance Method Details

#remove_file(file) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 74

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



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

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)
  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



32
33
34
35
36
37
# File 'middleman-core/lib/middleman-core/core_extensions/data/stores/local_file.rb', line 32

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