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

Class Method Summary collapse

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

Class Method Details

.for(enumerable) ⇒ I18n::Inflector::LazyEnumerator

Create a new instance that iterates over the passed Enumerable

Returns:


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

def self.for(enumerable)
  new do |y|
    enumerable.each do |e|
      y << e
    end
  end
end

Instance Method Details

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

Addition operator for collections

Returns:


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

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:


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

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


143
144
145
146
147
148
149
150
# File 'lib/i18n-inflector/lazy_enum.rb', line 143

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:


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

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:


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

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:


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

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:


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

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