Class: Middleman::CoreExtensions::Data::DataStore
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::Data::DataStore
- Defined in:
- lib/middleman-core/core_extensions/data.rb
Overview
The core logic behind the data extension.
Class Method Summary collapse
-
.matcher ⇒ Regexp
The regex which tells Middleman which files are for data.
Instance Method Summary collapse
-
#[](key) ⇒ Hash?
Make DataStore act like a hash.
-
#callbacks(name = nil, proc = nil) ⇒ Hash
Store callback-based data.
-
#data_for_path(path) ⇒ Hash?
Get a hash from either internal static data or a callback.
- #has_key?(key) ⇒ Boolean
-
#initialize(app) ⇒ DataStore
constructor
Setup data store.
-
#method_missing(path) ⇒ Hash?
“Magically” find namespaces of data if they exist.
-
#remove_file(file) ⇒ void
Remove a given file from the internal cache.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense.
-
#store(name = nil, content = nil) ⇒ Hash
Store static data hash.
-
#to_h ⇒ Hash
Convert all the data into a static hash.
-
#touch_file(file) ⇒ void
Update the internal cache for a given file path.
Constructor Details
#initialize(app) ⇒ DataStore
Setup data store
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
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/middleman-core/core_extensions/data.rb', line 166 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
.matcher ⇒ Regexp
The regex which tells Middleman which files are for data
55 56 57 |
# File 'lib/middleman-core/core_extensions/data.rb', line 55 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
190 191 192 |
# File 'lib/middleman-core/core_extensions/data.rb', line 190 def [](key) __send__(key) if has_key?(key) end |
#callbacks(name = nil, proc = nil) ⇒ Hash
Store callback-based 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 from either internal static data or a callback
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/middleman-core/core_extensions/data.rb', line 147 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 |
#has_key?(key) ⇒ Boolean
194 195 196 |
# File 'lib/middleman-core/core_extensions/data.rb', line 194 def has_key?(key) @local_data.has_key?(key.to_s) || !!(data_for_path(key)) end |
#remove_file(file) ⇒ void
This method returns an undefined value.
Remove a given file from the internal cache
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/middleman-core/core_extensions/data.rb', line 125 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.has_key?(basename) end |
#respond_to?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense
181 182 183 |
# File 'lib/middleman-core/core_extensions/data.rb', line 181 def respond_to?(method, include_private = false) super || has_key?(method) end |
#store(name = nil, content = nil) ⇒ Hash
Store static data hash
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_h ⇒ Hash
Convert all the data into a static hash
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/middleman-core/core_extensions/data.rb', line 201 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/middleman-core/core_extensions/data.rb', line 94 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 |