Class: Middleman::CoreExtensions::Data::DataStore

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

Overview

The core logic behind the data extension.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ DataStore

Setup data store

Parameters:



81
82
83
84
# File 'lib/middleman-core/core_extensions/data.rb', line 81

def initialize(app)
  @app = app
  @local_data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(path) ⇒ Hash?

“Magically” find namespaces of data if they exist

Parameters:

  • path (String)

    The namespace to search for

Returns:

  • (Hash, nil)


159
160
161
162
163
164
165
166
167
168
# 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

Class Method Details

.matcherRegexp

The regex which tells Middleman which files are for data

Returns:

  • (Regexp)


51
52
53
# File 'lib/middleman-core/core_extensions/data.rb', line 51

def matcher
  %r{[\w-]+\.(yml|yaml|json)$}
end

Instance Method Details

#[](key) ⇒ Hash?

Make DataStore act like a hash. Return requested data, or nil if data does not exist

Parameters:

  • key (String, Symbol)

    The name of the data namespace

Returns:

  • (Hash, nil)


180
181
182
# File 'lib/middleman-core/core_extensions/data.rb', line 180

def [](key)
  __send__(key) if key?(key)
end

#callbacks(name = nil, proc = nil) ⇒ Hash

Store callback-based data

Parameters:

  • name (Symbol) (defaults to: nil)

    Name of the data, used for namespacing

  • proc (Proc) (defaults to: nil)

    The callback which will return data

Returns:

  • (Hash)


72
73
74
75
76
# 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) ⇒ Hash?

Get a hash from either internal static data or a callback

Parameters:

  • path (String, Symbol)

    The name of the data namespace

Returns:

  • (Hash, nil)


143
144
145
146
147
148
149
150
151
152
153
# 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

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


184
185
186
# File 'lib/middleman-core/core_extensions/data.rb', line 184

def key?(key)
  @local_data.key?(key.to_s) || data_for_path(key)
end

#remove_file(file)

This method returns an undefined value.

Remove a given file from the internal cache

Parameters:

  • file (String)

    The file to be cleared



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Boolean

Needed so that method_missing makes sense

Returns:

  • (Boolean)


171
172
173
# 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) ⇒ Hash

Store static data hash

Parameters:

  • name (Symbol) (defaults to: nil)

    Name of the data, used for namespacing

  • content (Hash) (defaults to: nil)

    The content for this data

Returns:

  • (Hash)


61
62
63
64
65
# 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_hHash

Convert all the data into a static hash

Returns:

  • (Hash)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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)

This method returns an undefined value.

Update the internal cache for a given file path

Parameters:

  • file (String)

    The file to be re-parsed



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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