Class: Middleman::CoreExtensions::Data

Inherits:
Extension
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/core_extensions/data.rb

Overview

The data extension parses YAML and JSON files in the data/ directory and makes them available to config.rb, templates and extensions

Defined Under Namespace

Classes: DataStore

Constant Summary

DATA_FILE_MATCHER =

The regex which tells Middleman which files are for data

/^(.*?)[\w-]+\.(yml|yaml|json)$/

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary collapse

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activated_extension, #after_build, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, helpers, option

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, config = {}, &block) ⇒ Data

Returns a new instance of Data



15
16
17
18
19
20
21
22
23
24
# File 'middleman-core/lib/middleman-core/core_extensions/data.rb', line 15

def initialize(app, config={}, &block)
  super
  @data_store = DataStore.new(app, DATA_FILE_MATCHER)
  app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'

  app.add_to_instance(:data, &method(:data_store))
  app.add_to_config_context(:data, &method(:data_store))

  start_watching(app.config[:data_dir])
end

Instance Attribute Details

#data_storeObject (readonly)

Returns the value of attribute data_store



10
11
12
# File 'middleman-core/lib/middleman-core/core_extensions/data.rb', line 10

def data_store
  @data_store
end

Instance Method Details

#after_configurationObject



39
40
41
42
43
# File 'middleman-core/lib/middleman-core/core_extensions/data.rb', line 39

def after_configuration
  return unless @original_data_dir != app.config[:data_dir]

  @watcher.update_path(app.config[:data_dir])
end

#start_watching(dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'middleman-core/lib/middleman-core/core_extensions/data.rb', line 26

def start_watching(dir)
  @original_data_dir = dir

  # Tell the file watcher to observe the :data_dir
  @watcher = app.files.watch :data,
                             path: File.join(app.root, dir),
                             ignore: proc { |f| !DATA_FILE_MATCHER.match(f[:relative_path]) }

  # Setup data files before anything else so they are available when
  # parsing config.rb
  app.files.changed(:data, &@data_store.method(:update_files))
end