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.


13
14
15
16
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 13

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


61
62
63
64
65
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 61

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.


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

def accessed_keys
  @accessed_keys
end

Instance Method Details

#log_access(key) ⇒ Object


50
51
52
53
54
55
56
57
58
59
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 50

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_missing?(method, include_private = false) ⇒ Boolean

Needed so that method_missing makes sense

Returns:

  • (Boolean)

68
69
70
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 68

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

#take_ownership_of_array(a) ⇒ Object


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

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

#take_ownership_of_hash(h) ⇒ Object


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

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


18
19
20
# File 'middleman-core/lib/middleman-core/data_proxy.rb', line 18

def take_ownership_of_proxies(locs)
  take_ownership_of_hash(locs)
end

#take_ownership_of_value(v) ⇒ Object


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

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