Class: Enumark::Grouping

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

Defined Under Namespace

Classes: Group

Instance Method Summary collapse

Constructor Details

#initialize(enumark, key, &post) ⇒ Grouping

Returns a new instance of Grouping.



8
9
10
11
12
13
14
15
# File 'lib/enumark/grouping.rb', line 8

def initialize(enumark, key, &post)
  @lock = Mutex.new
  @collection = nil

  @enumark = enumark
  @key = key
  @post = post
end

Instance Method Details

#each(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/enumark/grouping.rb', line 17

def each(&block)
  unless @collection
    @lock.synchronize do
      @collection = @enumark.group_by(&@key)
      @collection = @post.call(@collection) if @post
      @collection = @collection.map{ |k, items| Group.new(k, items) }
    end
  end

  @collection.each(&block)
end