Module: W3cApi::Models::DelegateEnumerable

Included in:
CollectionBase
Defined in:
lib/w3c_api/models/delegate_enumerable.rb

Overview

Module for delegating Enumerable methods to a collection attribute

Instance Method Summary collapse

Instance Method Details

#delegate_enumerable(collection_attr) ⇒ Object

Define enumerable methods delegated to the specified collection attribute

Parameters:

  • collection_attr (Symbol)

    The name of the collection attribute



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/w3c_api/models/delegate_enumerable.rb', line 9

def delegate_enumerable(collection_attr)
  include Enumerable

  define_method(:each) do |&block|
    send(collection_attr).each(&block)
  end

  define_method(:map) do |&block|
    send(collection_attr).map(&block)
  end

  define_method(:select) do |&block|
    send(collection_attr).select(&block)
  end

  define_method(:[]) do |index|
    send(collection_attr)[index]
  end

  define_method(:first) do
    send(collection_attr).first
  end

  define_method(:last) do
    send(collection_attr).last
  end

  define_method(:empty?) do
    send(collection_attr).empty?
  end

  define_method(:size) do
    send(collection_attr).size
  end

  define_method(:length) do
    send(collection_attr).length
  end

  define_method(:to_a) do
    send(collection_attr)
  end
end