Class: Middleman::CoreExtensions::Data::Proxies::ArrayProxy

Inherits:
BaseProxy
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb

Constant Summary collapse

WRAPPED_LIST_METHODS =
Set.new %i[each each_with_index select sort shuffle reverse rotate sample]

Instance Attribute Summary

Attributes inherited from BaseProxy

#accessed_keys, #depth

Instance Method Summary collapse

Methods inherited from BaseProxy

#_replace_parent, #_top, #as_json, #initialize

Constructor Details

This class inherits a constructor from Middleman::CoreExtensions::Data::Proxies::BaseProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


17
18
19
20
21
22
23
24
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 17

def method_missing(name, *args, &block)
  if self.class.const_get(:WRAPPED_LIST_METHODS).include?(name)
    log_access(:__full_access__)
    return wrapped_array.send(name, *args, &block)
  end

  super
end

Instance Method Details

#fetch(index, default = (_missing_default = true), &block) ⇒ Object


26
27
28
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 26

def fetch(index, default = (_missing_default = true), &block)
  wrap_data index, @data.fetch(index, default, &block)
end

#first(length = (missing_length = true)) ⇒ Object


46
47
48
49
50
51
52
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 46

def first(length = (missing_length = true))
  if missing_length || length == 1
    slice(0)
  else
    slice(0, length)
  end
end

#last(length = (missing_length = true)) ⇒ Object


54
55
56
57
58
59
60
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 54

def last(length = (missing_length = true))
  if missing_length || length == 1
    slice(-1)
  else
    slice(size - length, length)
  end
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)

13
14
15
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 13

def respond_to_missing?(name, *)
  self.class.const_get(:WRAPPED_LIST_METHODS).include?(name) || super
end

#slice(arg, length = (missing_length = true)) ⇒ Object Also known as: []


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'middleman-core/lib/middleman-core/core_extensions/data/proxies/array.rb', line 30

def slice(arg, length = (missing_length = true))
  if missing_length
    if arg.is_a?(Range)
      log_access(:__full_access__)
      @data.slice(arg)
    else
      relative_index = (@data.size + arg) % @data.size
      wrap_data(relative_index, @data[relative_index])
    end
  else
    log_access(:__full_access__)
    @data.slice(arg, length)
  end
end