Class: ArraySugar::IterationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/arraysugar.rb

Instance Method Summary collapse

Constructor Details

#initialize(array, mode) ⇒ IterationProxy

Returns a new instance of IterationProxy.



35
36
37
38
39
# File 'lib/arraysugar.rb', line 35

def initialize(array, mode)
  @array = array
  @mode = mode
  @leafs = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object



53
54
55
56
57
# File 'lib/arraysugar.rb', line 53

def method_missing(s, *a, &b)
  @s = s; @a = a; @b = b
  @array = @array.send(@mode) {|e| __do_call(e) }
  self
end

Instance Method Details

#__do_call(e) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arraysugar.rb', line 41

def __do_call(e)
  if @leafs && (e.is_a?(Array) || e.is_a?(Range))
    e.send(@mode) {|e| __do_call(e) }
  else
    if @nils_okay && e.nil?
      nil
    else
      e.send(@s, *@a, &@b)
    end
  end
end

#compactObject



79
80
81
82
# File 'lib/arraysugar.rb', line 79

def compact
  @array = @array.compact
  self
end

#inspectObject



75
76
77
# File 'lib/arraysugar.rb', line 75

def inspect
  to_a.inspect
end

#leafsObject



59
60
61
62
63
# File 'lib/arraysugar.rb', line 59

def leafs
  throw 'sorry, select does not yet work with leafs' if @mode =~ /select/ || @mode =~ /find_all/
  @leafs = true
  self
end

#nils_okayObject Also known as: nils_ok



65
66
67
68
# File 'lib/arraysugar.rb', line 65

def nils_okay
  @nils_okay = true
  self
end

#to_aObject



71
72
73
# File 'lib/arraysugar.rb', line 71

def to_a
  @array
end

#uniqObject



84
85
86
87
# File 'lib/arraysugar.rb', line 84

def uniq
  @array = @array.uniq
  self
end