Skip to content

Commit

Permalink
feature(hash_counter): method date when called with a block, now retu…
Browse files Browse the repository at this point in the history
…rns the total number of lines
  • Loading branch information
Napolskih committed Oct 21, 2013
1 parent 6d04c95 commit bfa328a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/redis_counters/hash_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ def partitions(parts = {})
# Если блок, не передн, то аккумулирует данные,
# из всех запрошенных партиций, и затем возвращает их.
#
# Returns Array Of Hash.
# Returns Array Of Hash или общее кол-во строк данных, если передан блок.
#
def data(parts = {})
total_rows = 0
parts = partitions(parts)
prepared_parts(parts).flat_map do |partition|

result = prepared_parts(parts).flat_map do |partition|
rows = partition_data(partition)
total_rows += rows.size
block_given? ? yield(rows) : rows
end

block_given? ? total_rows : result
end

# Public: Транзакционно удаляет все данные счетчика.
Expand Down
2 changes: 2 additions & 0 deletions spec/redis_counters/hash_counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@
]
end

it { expect(counter.data(partitions) {}).to eq 3 }
it { expect { |b| counter.data(partitions, &b) }.to yield_control.twice }

it do
expect { |b| counter.data(partitions, &b) }.to(
yield_successive_args(
Expand Down

0 comments on commit bfa328a

Please sign in to comment.