Class: Middleman::CoreExtensions::Data::DataStoreController
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::Data::DataStoreController
- Extended by:
- Forwardable
- Defined in:
- middleman-core/lib/middleman-core/core_extensions/data/controller.rb
Overview
The core logic behind the data extension.
Instance Method Summary collapse
- #enhanced_data(k) ⇒ Object
-
#initialize(app, track_data_access) ⇒ DataStoreController
constructor
A new instance of DataStoreController.
- #key(k) ⇒ Object (also: #[])
- #key?(k) ⇒ Boolean (also: #has_key?)
-
#method_missing(method) ⇒ Hash?
"Magically" find namespaces of data if they exist.
- #proxied_data(k, parent = nil) ⇒ Object
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense.
-
#to_h ⇒ Hash
Convert all the data into a static hash.
- #vertices ⇒ Object
Constructor Details
#initialize(app, track_data_access) ⇒ DataStoreController
Returns a new instance of DataStoreController.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 20 def initialize(app, track_data_access) @data_collection_depth = app.config[:data_collection_depth] @track_data_access = track_data_access @local_file_data_store = Data::Stores::LocalFileDataStore.new(app) @in_memory_data_store = Data::Stores::InMemoryDataStore.new # Sorted in order of access precedence. @data_stores = [ @local_file_data_store, @in_memory_data_store ] @enhanced_cache = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Hash?
"Magically" find namespaces of data if they exist
90 91 92 93 94 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 90 def method_missing(method) return proxied_data(method) if key?(method) super end |
Instance Method Details
#enhanced_data(k) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 53 def enhanced_data(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 |
#key(k) ⇒ Object Also known as: []
41 42 43 44 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 41 def key(k) source = @data_stores.find { |s| s.key?(k) } source[k] unless source.nil? end |
#key?(k) ⇒ Boolean Also known as: has_key?
36 37 38 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 36 def key?(k) @data_stores.any? { |s| s.key?(k) } end |
#proxied_data(k, parent = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 71 def proxied_data(k, parent = nil) data = enhanced_data(k) return data unless @track_data_access case data when ::Middleman::Util::EnhancedHash Data::Proxies::HashProxy.new(k, data, @data_collection_depth, parent) when ::Array Data::Proxies::ArrayProxy.new(k, data, @data_collection_depth, parent) else raise 'Invalid data to wrap' end end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense
97 98 99 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 97 def respond_to_missing?(method, include_private = false) key?(method) || super end |
#to_h ⇒ Hash
Convert all the data into a static hash
104 105 106 107 108 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 104 def to_h @data_stores.reduce({}) do |sum, store| sum.merge(store.to_h) end end |
#vertices ⇒ Object
47 48 49 50 51 |
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 47 def vertices @data_stores.reduce(::Hamster::Set.empty) do |sum, s| sum | s.vertices end end |