Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor data extension * Things deserve their own files * Lint
- Loading branch information
Showing
10 changed files
with
354 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 32 additions & 203 deletions
235
middleman-core/lib/middleman-core/core_extensions/data.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
middleman-core/lib/middleman-core/core_extensions/data/controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'middleman-core/util/data' | ||
require 'middleman-core/core_extensions/data/stores/local_file' | ||
require 'middleman-core/core_extensions/data/stores/static' | ||
require 'middleman-core/core_extensions/data/stores/callback' | ||
|
||
module Middleman | ||
module CoreExtensions | ||
module Data | ||
# The core logic behind the data extension. | ||
class DataStoreController | ||
extend Forwardable | ||
|
||
def_delegator :@local_file_data_store, :update_files | ||
def_delegator :@static_data_store, :store | ||
def_delegator :@callback_data_store, :callbacks | ||
|
||
def initialize(app) | ||
@local_file_data_store = Data::Stores::LocalFileDataStore.new(app) | ||
@static_data_store = Data::Stores::StaticDataStore.new | ||
@callback_data_store = Data::Stores::CallbackDataStore.new | ||
|
||
# Sorted in order of access precedence. | ||
@data_stores = [ | ||
@local_file_data_store, | ||
@static_data_store, | ||
@callback_data_store | ||
] | ||
|
||
@enhanced_cache = {} | ||
end | ||
|
||
def key?(k) | ||
@data_stores.any? { |s| s.key?(k) } | ||
end | ||
alias has_key? key? | ||
|
||
def key(k) | ||
source = @data_stores.find { |s| s.key?(k) } | ||
source[k] unless source.nil? | ||
end | ||
|
||
def enhanced_key(k) | ||
value = key(k) | ||
|
||
if @enhanced_cache.key?(k) | ||
cached_id, cached_value = @enhanced_cache[k] | ||
|
||
return cached_value if cached_id == value.object_id | ||
|
||
@enhanced_cache.delete(k) | ||
end | ||
|
||
enhanced = ::Middleman::Util.recursively_enhance(value) | ||
|
||
@enhanced_cache[k] = [value.object_id, enhanced] | ||
|
||
enhanced | ||
end | ||
|
||
# "Magically" find namespaces of data if they exist | ||
# | ||
# @param [String] path The namespace to search for | ||
# @return [Hash, nil] | ||
def method_missing(method) | ||
return enhanced_key(method) if key?(method) | ||
|
||
super | ||
end | ||
|
||
# Needed so that method_missing makes sense | ||
def respond_to?(method, include_private = false) | ||
super || key?(method) | ||
end | ||
|
||
# Convert all the data into a static hash | ||
# | ||
# @return [Hash] | ||
def to_h | ||
@data_stores.reduce({}) do |sum, store| | ||
sum.merge(store.to_h) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
middleman-core/lib/middleman-core/core_extensions/data/stores/base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'middleman-core/contracts' | ||
|
||
module Middleman | ||
module CoreExtensions | ||
module Data | ||
module Stores | ||
class BaseDataStore | ||
include Contracts | ||
|
||
Contract Symbol => Bool | ||
def key?(_k) | ||
raise NotImplementedError | ||
end | ||
|
||
Contract Symbol => Or[Array, Hash] | ||
def [](_k) | ||
raise NotImplementedError | ||
end | ||
|
||
Contract ArrayOf[Symbol] | ||
def keys | ||
raise NotImplementedError | ||
end | ||
|
||
Contract Hash | ||
def to_h | ||
keys.each_with_object({}) do |k, sum| | ||
sum[k] = self[k] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.