Class: Middleman::CoreExtensions::Data::DataStoreController

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(app, track_data_access) ⇒ DataStoreController


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 18

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


87
88
89
90
91
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 87

def method_missing(method)
  return proxied_data(method) if key?(method)

  super
end

Instance Method Details

#enhanced_data(k) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 51

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: []


39
40
41
42
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 39

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?


34
35
36
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 34

def key?(k)
  @data_stores.any? { |s| s.key?(k) }
end

#proxied_data(k, parent = nil) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 69

def proxied_data(k, parent = nil)
  data = enhanced_data(k)

  return data unless @track_data_access

  if data.is_a? ::Middleman::Util::EnhancedHash
    Data::Proxies::HashProxy.new(k, data, @data_collection_depth, parent)
  elsif data.is_a? ::Array
    Data::Proxies::ArrayProxy.new(k, data, @data_collection_depth, parent)
  else
    raise 'Invalid data to wrap'
  end
end

#respond_to?(method, include_private = false) ⇒ Boolean

Needed so that method_missing makes sense


94
95
96
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 94

def respond_to?(method, include_private = false)
  super || key?(method)
end

#to_hHash

Convert all the data into a static hash


101
102
103
104
105
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 101

def to_h
  @data_stores.reduce({}) do |sum, store|
    sum.merge(store.to_h)
  end
end

#verticesObject


45
46
47
48
49
# File 'middleman-core/lib/middleman-core/core_extensions/data/controller.rb', line 45

def vertices
  @data_stores.reduce(::Hamster::Set.empty) do |sum, s|
    sum | s.vertices
  end
end