Class: Middleman::DataProxy

Inherits:
Object
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/data_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ DataProxy

Returns a new instance of DataProxy


11
12
13
14
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 11

def initialize(ctx)
  @ctx = ctx
  @accessed_keys = ::Hamster::Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


58
59
60
61
62
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 58

def method_missing(method, *args, &block)
  return @ctx.internal_data_store.proxied_data(method, self) if @ctx.internal_data_store.key?(method)

  super
end

Instance Attribute Details

#accessed_keysObject (readonly)

Returns the value of attribute accessed_keys


9
10
11
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 9

def accessed_keys
  @accessed_keys
end

Instance Method Details

#log_access(key) ⇒ Object


47
48
49
50
51
52
53
54
55
56
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 47

def log_access(key)
  return if @accessed_keys.include?(key)

  @accessed_keys <<= key

  @ctx.vertices <<= ::Middleman::Dependencies::DataCollectionPathVertex.from_data(
    @ctx.app,
    key
  )
end

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

Needed so that method_missing makes sense

Returns:

  • (Boolean)

65
66
67
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 65

def respond_to?(method, include_private = false)
  super || @ctx.internal_data_store.key?(method)
end

#take_ownership_of_array(a) ⇒ Object


27
28
29
30
31
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 27

def take_ownership_of_array(a)
  a.map do |v|
    take_ownership_of_value(v)
  end
end

#take_ownership_of_hash(h) ⇒ Object


20
21
22
23
24
25
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 20

def take_ownership_of_hash(h)
  h.keys.each_with_object({}) do |key, sum|
    v = h[key]
    sum[key] = take_ownership_of_value(v)
  end
end

#take_ownership_of_proxies(locs) ⇒ Object


16
17
18
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 16

def take_ownership_of_proxies(locs)
  take_ownership_of_hash(locs)
end

#take_ownership_of_value(v) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 33

def take_ownership_of_value(v)
  if v.is_a?(::Array)
    take_ownership_of_array(v)
  elsif v.is_a?(::Hash)
    take_ownership_of_hash(v)
  elsif v.is_a?(::Middleman::CoreExtensions::Data::Proxies::ArrayProxy)
    v.clone.tap { |p| p._top._replace_parent(self) }
  elsif v.is_a?(::Middleman::CoreExtensions::Data::Proxies::HashProxy)
    v.clone.tap { |p| p._top._replace_parent(self) }
  else
    v
  end
end