Class: I18n::Inflector::LazyEnumerator

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/i18n-inflector/lazy_enum.rb,
lib/i18n-inflector/lazy_enum.rb,
lib/i18n-inflector/lazy_enum.rb

Overview

This class adds some lazy operations for collections

Direct Known Subclasses

LazyArrayEnumerator, LazyHashEnumerator

Defined Under Namespace

Classes: Yielder

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ LazyEnumerator

Returns a new instance of LazyEnumerator.


45
46
47
# File 'lib/i18n-inflector/lazy_enum.rb', line 45

def initialize(*args, &block)
  args.empty? ? super(Yielder.new(&block)) : super(*args, &nil) 
end

Instance Method Details

#+(other) ⇒ I18n::Inflector::LazyEnumerator

Addition operator for collections

Returns:


68
69
70
71
72
73
74
75
76
77
# File 'lib/i18n-inflector/lazy_enum.rb', line 68

def +(other)
  self.class.new do |yielder|
    each do |v|
      yielder << v
    end
    other.each do |v|
      yielder << v
    end
  end
end

#append(value) ⇒ I18n::Inflector::LazyEnumerator

Appending operator for collections

Returns:


92
93
94
95
96
97
98
99
# File 'lib/i18n-inflector/lazy_enum.rb', line 92

def append(value)
  self.class.new do |yielder|
    each do |v|
      yielder << v
    end
    yielder << value
  end
end

#empty?Boolean

Checks if a collection is empty

Returns:

  • (Boolean)

    true if collection is empty, false otherwise


133
134
135
136
137
138
139
140
# File 'lib/i18n-inflector/lazy_enum.rb', line 133

def empty?
  self.class.new do |yielder|
    each do |k,v|
      return false
    end
  end
  true
end

#insert(value) ⇒ I18n::Inflector::LazyEnumerator

Insertion operator for collections

Returns:


81
82
83
84
85
86
87
88
# File 'lib/i18n-inflector/lazy_enum.rb', line 81

def insert(value)
  self.class.new do |yielder|
    yielder << value
    each do |v|
      yielder << v
    end
  end
end

#map(&block) ⇒ I18n::Inflector::LazyEnumerator

Mapping enumerator

Returns:


103
104
105
106
107
108
109
# File 'lib/i18n-inflector/lazy_enum.rb', line 103

def map(&block)
  self.class.new do |yielder|
    each do |v|
      yielder << block[v]
    end
  end
end

#reject(&block) ⇒ I18n::Inflector::LazyEnumerator

Rejecting enumerator

Returns:


123
124
125
126
127
128
129
# File 'lib/i18n-inflector/lazy_enum.rb', line 123

def reject(&block)
  self.class.new do |yielder|
    each do |v|
      yielder << v unless block[v]
    end
  end
end

#select(&block) ⇒ I18n::Inflector::LazyEnumerator

Selecting enumerator

Returns:


113
114
115
116
117
118
119
# File 'lib/i18n-inflector/lazy_enum.rb', line 113

def select(&block)
  self.class.new do |yielder|
    each do |v|
      yielder << v if block[v]
    end
  end
end