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:



85
86
87
88
# File 'lib/middleman-core/core_extensions/data.rb', line 85

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)


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/middleman-core/core_extensions/data.rb', line 143

def method_missing(path)
  if @local_data.has_key?(path.to_s)
    return @local_data[path.to_s]
  else
    result = data_for_path(path)

    if result
      return ::Middleman::Util.recursively_enhance(result)
    end
  end

  super
end

Class Method Details

.matcherRegexp

The regex which tells Middleman which files are for data

Returns:

  • (Regexp)


55
56
57
# File 'lib/middleman-core/core_extensions/data.rb', line 55

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

Instance Method Details

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

This method returns an undefined value.

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



76
77
78
79
80
# File 'lib/middleman-core/core_extensions/data.rb', line 76

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 hash from either internal static data or a callback

Parameters:

  • path (String, Symbol)

    The name of the data namespace

Returns:

  • (Hash, nil)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/middleman-core/core_extensions/data.rb', line 124

def data_for_path(path)
  response = nil

  @@local_sources ||= {}
  @@callback_sources ||= {}

  if self.store.has_key?(path.to_s)
    response = self.store[path.to_s]
  elsif self.callbacks.has_key?(path.to_s)
    response = self.callbacks[path.to_s].call()
  end

  response
end

#remove_file(file) ⇒ void

This method returns an undefined value.

Remove a given file from the internal cache

Parameters:

  • file (String)

    The file to be cleared



114
115
116
117
118
# File 'lib/middleman-core/core_extensions/data.rb', line 114

def remove_file(file)
  extension = File.extname(file)
  basename  = File.basename(file, extension)
  @local_data.delete(basename) if @local_data.has_key?(basename)
end

#store(name = nil, content = nil) ⇒ void

This method returns an undefined value.

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



65
66
67
68
69
# File 'lib/middleman-core/core_extensions/data.rb', line 65

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)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/middleman-core/core_extensions/data.rb', line 160

def to_h
  data = {}

  self.store.each do |k, v|
    data[k] = data_for_path(k)
  end

  self.callbacks.each do |k, v|
    data[k] = data_for_path(k)
  end

  (@local_data || {}).each do |k, v|
    data[k] = v
  end

  data
end

#touch_file(file) ⇒ void

This method returns an undefined value.

Update the internal cache for a given file path

Parameters:

  • file (String)

    The file to be re-parsed



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/middleman-core/core_extensions/data.rb', line 94

def touch_file(file)
  file = File.expand_path(file, @app.root)
  extension = File.extname(file)
  basename  = File.basename(file, extension)

  if %w(.yaml .yml).include?(extension)
    data = YAML.load_file(file)
  elsif extension == ".json"
    data = ActiveSupport::JSON.decode(File.read(file))
  else
    return
  end

  @local_data[basename] = ::Middleman::Util.recursively_enhance(data)
end