Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization and Fixes #15

Merged
merged 1 commit into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions lib/pluck_to_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,42 @@ module PluckToHash
module ClassMethods
def pluck_to_hash(*keys)
block_given = block_given?
keys = column_names if keys.blank?
formatted_keys = format_keys(keys)
keys, formatted_keys = format_keys(keys)
keys_one = keys.size == 1

pluck(*keys).map do |row|
row = [row] if keys.size == 1
value = HashWithIndifferentAccess[formatted_keys.zip(row)]
yield(value) if block_given
value
value = HashWithIndifferentAccess[formatted_keys.zip(keys_one ? [row] : row)]
block_given ? yield(value) : value
end
end

def pluck_to_struct(*keys)
block_given = block_given?
keys = column_names if keys.blank?
formatted_keys = format_keys(keys)
keys, formatted_keys = format_keys(keys)
keys_one = keys.size == 1

struct = Struct.new(*formatted_keys)
struct = Struct.new(*formatted_keys.map(&:to_sym))
pluck(*keys).map do |row|
row = [row] if keys.size == 1
value = struct.new(*row)
yield(value) if block_given
value
value = keys_one ? struct.new(*[row]) : struct.new(*row)
block_given ? yield(value) : value
end
end

def format_keys(keys)
keys.map do |k|
case k
when String
k.split(/ as /i)[-1].to_sym
when Symbol
k
end
if keys.blank?
[column_names, column_names]
else
[
keys,
keys.map do |k|
case k
when String
k.split(/ as /i)[-1]
when Symbol
k.to_s
end
end
]
end
end

Expand Down
17 changes: 6 additions & 11 deletions spec/pluck_to_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@
end
end


context 'the model does not have the attribute specified' do
it 'raises an error' do
expect do
TestModel.all.pluck_to_hash(:foo)
end.to raise_error
end.to raise_error(ActiveRecord::StatementInvalid)
end
end

context 'no models exist for the given criteria' do
it 'returns an empty relation' do
expect do
result = TestModel.where(id: -1).pluck_to_hash(:id)
expect(result).to be_empty
end.to_not raise_error
result = TestModel.where(id: -1).pluck_to_hash(:id)
expect(result).to be_empty
end
end

Expand Down Expand Up @@ -111,16 +108,14 @@
it 'raises an error' do
expect do
TestModel.all.pluck_h(:foo)
end.to raise_error
end.to raise_error(ActiveRecord::StatementInvalid)
end
end

context 'no models exist for the given criteria' do
it 'returns an empty relation' do
expect do
result = TestModel.where(id: -1).pluck_h(:id)
expect(result).to be_empty
end.to_not raise_error
result = TestModel.where(id: -1).pluck_h(:id)
expect(result).to be_empty
end
end

Expand Down
10 changes: 4 additions & 6 deletions spec/pluck_to_struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

it 'plucks the ids, test_attributes of the objects to a struct correctly' do
TestModel.all.pluck_to_struct(:test_attribute, :id).each_with_index do |model, ix|
id = ix+1
id = ix + 1
expect(model).to be_a(Struct)
expect(model.test_attribute).to eq("test#{id}")
expect(model.id).to eq(id)
Expand All @@ -30,16 +30,14 @@
it 'raises an error' do
expect do
TestModel.all.pluck_h(:foo)
end.to raise_error
end.to raise_error(ActiveRecord::StatementInvalid)
end
end

context 'no models exist for the given criteria' do
it 'returns an empty relation' do
expect do
result = TestModel.where(id: -1).pluck_h(:id)
expect(result).to be_empty
end.to_not raise_error
result = TestModel.where(id: -1).pluck_h(:id)
expect(result).to be_empty
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

class TestModel < ActiveRecord::Base
serialize :serialized_attribute, Array
end
end