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



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

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



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

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

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



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

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



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

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)


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

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



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

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