Class: I18n::Inflector::LazyHashEnumerator

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

Overview

This class implements simple enumerators for hashes that allow to do lazy operations on them.

Instance Method Summary collapse

Methods inherited from LazyEnumerator

#+, #empty?, #initialize

Constructor Details

This class inherits a constructor from I18n::Inflector::LazyEnumerator

Instance Method Details

#append(key, value) ⇒ I18n::Inflector::LazyHashEnumerator

Appending operator for Hash enumerators

Returns:



176
177
178
179
180
181
182
183
# File 'lib/i18n-inflector/lazy_enum.rb', line 176

def append(key, value)
  self.class.new do |yielder|
    each do |k,v|
      yielder.yield(k,v)
    end
    yielder.yield(key, value)
  end
end

#ary_map(&block) ⇒ I18n::Inflector::LazyHashEnumerator

Hash to Array mapping enumerator

Returns:



197
198
199
200
201
202
203
# File 'lib/i18n-inflector/lazy_enum.rb', line 197

def ary_map(&block)
  LazyHashEnumerator.new do |yielder|
    each do |value|
      yielder << block[value]
    end
  end
end

#each_key(&block) ⇒ I18n::Inflector::LazyArrayEnumerator.new

Keys enumerator

Returns:



223
224
225
226
227
228
229
# File 'lib/i18n-inflector/lazy_enum.rb', line 223

def each_key(&block)
  LazyArrayEnumerator.new do |yielder|
    each do |k,v|
      yielder << k
    end
  end
end

#each_value(&block) ⇒ I18n::Inflector::LazyArrayEnumerator.new

Values enumerator

Returns:



233
234
235
236
237
238
239
# File 'lib/i18n-inflector/lazy_enum.rb', line 233

def each_value(&block)
  LazyArrayEnumerator.new do |yielder|
    each do |k,v|
      yielder << v
    end
  end
end

#insert(key, value) ⇒ I18n::Inflector::LazyHashEnumerator

Insertion operator for Hash enumerators

Returns:



165
166
167
168
169
170
171
172
# File 'lib/i18n-inflector/lazy_enum.rb', line 165

def insert(key, value)
  self.class.new do |yielder|
    yielder.yield(key, value)
    each do |k,v|
      yielder.yield(k,v)
    end
  end
end

#keysObject

This method converts resulting keys to an array.



207
208
209
210
211
# File 'lib/i18n-inflector/lazy_enum.rb', line 207

def keys
  ary = []
  each{ |k,v| ary << k }
  return ary
end

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

Hash mapping enumerator

Returns:



187
188
189
190
191
192
193
# File 'lib/i18n-inflector/lazy_enum.rb', line 187

def map(&block)
  LazyHashEnumerator.new do |yielder|
    each do |k,v|
      yielder.yield(k,block[k,v])
    end
  end
end

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

Hash rejecting enumerator

Returns:



253
254
255
256
257
258
259
# File 'lib/i18n-inflector/lazy_enum.rb', line 253

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

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

Hash selecting enumerator

Returns:



243
244
245
246
247
248
249
# File 'lib/i18n-inflector/lazy_enum.rb', line 243

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

#to_hHash

Creates a Hash kind of object by collecting all data from enumerated collection.

Returns:

  • (Hash)

    the resulting hash



157
158
159
160
161
# File 'lib/i18n-inflector/lazy_enum.rb', line 157

def to_h
  h = Hash.new
  each{|k,v| h[k]=v }
  h
end

#valuesObject

This method converts resulting values to an array.



215
216
217
218
219
# File 'lib/i18n-inflector/lazy_enum.rb', line 215

def values
  ary = []
  each{ |k,v| ary << v }
  return ary
end