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?, for, #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:


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

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:


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

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:


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

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:


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

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:


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

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.


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

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

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

Hash mapping enumerator

Returns:


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

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:


263
264
265
266
267
268
269
# File 'lib/i18n-inflector/lazy_enum.rb', line 263

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:


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

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


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

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

#valuesObject

This method converts resulting values to an array.


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

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