Skip to content

Commit

Permalink
feature: do not integerize hash key in hash counters
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalahutdinov committed Jul 14, 2017
1 parent 8385b8a commit e3d68ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
11 changes: 10 additions & 1 deletion lib/treasury/hash_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
module Treasury
module HashSerializer
def deserialize(hash_string)
hash_string.blank? ? Hash.new : Hash[hash_string.split(',').map { |item| item.split(':').map(&:to_i) }]
return Hash.new if hash_string.blank?

hash_data = hash_string
.split(',')
.map do |item|
key, value = item.split(':')
[key, value.to_i]
end

Hash[hash_data]
end

def serialize(hash)
Expand Down
14 changes: 7 additions & 7 deletions lib/treasury/processors/hash_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ module HashOperations
include HashSerializer

def increment_raw_value(raw_hash, key, step = 1)
numeric_key = key.to_i
key_string = key.to_s
process_value(raw_hash) do |data|
data[numeric_key] = data[numeric_key].to_i + step.to_i
data[key_string] = data[key_string].to_i + step.to_i
end
end

def decrement_raw_value(raw_hash, key, step = 1)
numeric_key = key.to_i
key_string = key.to_s
process_value(raw_hash) do |data|
if data.key?(numeric_key)
new_value = data[numeric_key] - step.to_i
if data.key?(key_string)
new_value = data[key_string] - step.to_i
if new_value > 0
data[numeric_key] = new_value
data[key_string] = new_value
else
data.delete numeric_key
data.delete key_string
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/treasury/fields/hash_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

it do
expect(hash_field_class).to receive(:init_accessor).with(object: 123, field: :count)
expect(value_as_hash).to eq(10 => 100, 20 => 200)
expect(value_as_hash).to eq('10' => 100, '20' => 200)
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/treasury/hash_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
context 'when value ok' do
let(:value) { '123:321,234:432' }

it { expect(deserialized).to eq(123 => 321, 234 => 432) }
it { expect(deserialized).to eq('123' => 321, '234' => 432) }
end
end
end
14 changes: 7 additions & 7 deletions spec/lib/treasury/processors/hash_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
let(:result) { dummy.increment_raw_value(raw_value, company_id) }

context 'when company_id is in raw_value' do
let(:company_id) { 123 }
let(:company_id) { '123' }

it { expect(result).to eq '123:322,234:432' }
end

context 'when company_id is not in raw_value' do
let(:company_id) { 666 }
let(:company_id) { '666' }

it { expect(result).to eq '123:321,234:432,666:1' }
end

context 'when step greater than 1' do
let(:result) { dummy.increment_raw_value raw_value, company_id, step }

let(:company_id) { 123 }
let(:company_id) { '123' }
let(:step) { "5" }

it { expect(result).to eq '123:326,234:432' }
Expand All @@ -38,28 +38,28 @@
let(:result) { dummy.decrement_raw_value(raw_value, company_id) }

context 'when company_id is in raw_value' do
let(:company_id) { 123 }
let(:company_id) { '123' }

it { expect(result).to eq '123:320,234:432' }
end

context 'when company_id is not in raw_value' do
let(:company_id) { 666 }
let(:company_id) { '666' }

it { expect(result).to eq '123:321,234:432' }
end

context 'when new values goes zero' do
let(:raw_value) { '123:1,234:432' }
let(:company_id) { 123 }
let(:company_id) { '123' }

it { expect(result).to eq '234:432' }
end

context 'when step greater than 1' do
let(:result) { dummy.decrement_raw_value raw_value, company_id, step }

let(:company_id) { 123 }
let(:company_id) { '123' }
let(:step) { "5" }

it { expect(result).to eq '123:316,234:432' }
Expand Down

0 comments on commit e3d68ca

Please sign in to comment.