pluckeroid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDJmYmU0ZTQ2NmJjOWFkNzY2YTA5ZWFmZDdiMjI3ZjhlOGFhNTMzZQ==
5
+ data.tar.gz: !binary |-
6
+ MDA3YmFlZmQ3Nzg4YzI3ZmUxNzdlODQ0NmMwZTI3YmM2ZjY2NjEwMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OWM4ZTc5ODQwNzE4NzE3YzRkZGZmMjUyODE5MGNiMDA3YzFiMTg3Y2NlZjM3
10
+ ZDk4Yjg5MzAzYTk2NzM1NjEyYjljNGViNzQ0MDllY2VhMjQ2MzQ3MmE5M2U2
11
+ YmQxYTBmY2QxM2RjNGNjNjUwZjVmMjgyOTFlOWE5Mzg5NTU3ZTA=
12
+ data.tar.gz: !binary |-
13
+ ZGFjMTdhYWFmMzdhN2I4NWZiZDFhOTdmNzZmNzVmNjFmMTQ2M2JhMTE2Yjcz
14
+ NzQ3N2YzNDU4MmYxZjQ1NWFkZmI0NjgyNWRmYmM3NTgzNjE1YTdhZjE2YjIz
15
+ ZmE2ZDgxMDk2YTI0ZmE4NzUzY2ExZDI1YWQxOGU1NzRiNmU3OGU=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: rbx-18mode
11
+ - rvm: rbx-19mode
12
+ fast_finish: true
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dimko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Pluckeroid [![Build Status](https://travis-ci.org/dimko/pluckeroid.png?branch=master)](https://travis-ci.org/dimko/pluckeroid)
2
+
3
+ Pluck for ActiveRecord on steroids
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pluckeroid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pluckeroid
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ Person.pluck(:id)
23
+ # => [1, 2]
24
+
25
+ Person.pluck_attributes(:id)
26
+ # => [{ 'id' => 1 }, { 'id' => 2 }]
27
+
28
+ Person.pluck(:id, :name)
29
+ # => [[1, 'Obi-Wan'], [2, 'Luke']]
30
+
31
+ Person.pluck_attributes(:id, :name)
32
+ # => [{ 'id' => 1, 'name' => 'Obi-Wan' }, { 'id' => 2, 'name' => 'Luke' }]
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
42
+
43
+ ## Thanks
44
+
45
+ Based on native `pluck` method and Ernie Miller's `valium` project.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |rspec|
5
+ rspec.rspec_opts = ['--color --backtrace']
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ module Pluckeroid
2
+ module ActiveRecord
3
+ module Base
4
+ def self.included(base)
5
+ base.singleton_class.class_eval do
6
+ delegate :pluck_attributes, :pluck, :values_of, :value_of, to: :scoped
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ class ActiveRecord::Base
14
+ include Pluckeroid::ActiveRecord::Base
15
+ end
@@ -0,0 +1,82 @@
1
+ module Pluckeroid
2
+ module ActiveRecord
3
+ module Relation
4
+ # Performs select by column names as direct SQL query.
5
+ # Returns <tt>Array</tt> of hashes each element of which contains
6
+ # specified column names as keys and the values of the column names as values.
7
+ # The values has same data type as column.
8
+ #
9
+ # Examples:
10
+ #
11
+ # Person.pluck(:id) # SELECT people.id FROM people
12
+ # # => [{ 'id' => 1 }, { 'id' => 2 }]
13
+ #
14
+ # Person.pluck_attributes(:id, :name) # SELECT people.id, people.name FROM people
15
+ # # => [{ 'id' => 1, 'name' => 'Obi-Wan' }, { 'id' => 2, 'name' => 'Luke' }]
16
+ #
17
+ #
18
+ def pluck_attributes(*column_names)
19
+ pluck_columns(column_names) do |attributes|
20
+ attributes.each do |key, _|
21
+ attributes[key] = klass.type_cast_attribute(key, attributes)
22
+ end
23
+ end
24
+ end
25
+
26
+ # Same as +pluck_attributes+ but returns <tt>Array</tt> with values
27
+ # of the specified column name.
28
+ #
29
+ # Examples:
30
+ #
31
+ # Person.pluck(:id) # SELECT people.id FROM people
32
+ # => [1, 2]
33
+ #
34
+ # Person.pluck(:id, :name) # SELECT people.id, people.name FROM people
35
+ # => [[1, 'Obi-Wan'], [2, 'Luke']]
36
+ #
37
+ def pluck(*column_names)
38
+ flatten = column_names.size == 1
39
+
40
+ pluck_columns(column_names) do |attributes|
41
+ values = attributes.map do |key, _|
42
+ klass.type_cast_attribute(key, attributes)
43
+ end
44
+
45
+ if flatten
46
+ values.first
47
+ else
48
+ values
49
+ end
50
+ end
51
+ end
52
+ alias :value_of :pluck
53
+ alias :values_of :pluck
54
+
55
+ protected
56
+
57
+ def pluck_columns(column_names)
58
+ if column_names.size.zero?
59
+ raise ArgumentError, 'wrong number of arguments (0 for 1)'
60
+ end
61
+
62
+ column_names = column_names.map do |column_name|
63
+ if Symbol === column_name && column_names.include?(column_name.to_s)
64
+ "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
65
+ else
66
+ column_name.to_s
67
+ end
68
+ end
69
+
70
+ relation = clone
71
+ relation.select_values = column_names
72
+ klass.connection.select_all(relation.arel).map! do |attributes|
73
+ yield klass.initialize_attributes(attributes)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ class ActiveRecord::Relation
81
+ include Pluckeroid::ActiveRecord::Relation
82
+ end
@@ -0,0 +1,3 @@
1
+ module Pluckeroid
2
+ VERSION = '0.0.1'
3
+ end
data/lib/pluckeroid.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+
3
+ require 'pluckeroid/active_record/base'
4
+ require 'pluckeroid/active_record/relation'
5
+ require 'pluckeroid/version'
6
+
7
+ module Pluckeroid
8
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'pluckeroid/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'pluckeroid'
7
+ spec.version = Pluckeroid::VERSION
8
+ spec.authors = ['Dimko']
9
+ spec.email = ['deemox@gmail.com']
10
+ spec.description = %q{Pluck for ActiveRecord on steroids}
11
+ spec.summary = %q{Pluck for ActiveRecord on steroids}
12
+ spec.homepage = 'https://github.com/dimko/pluckeroid'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activerecord', '~> 3.0'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'sqlite3', '~> 1.3.7'
23
+ spec.add_development_dependency 'rspec', '~> 2.6'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ # From valium
4
+
5
+ describe Pluckeroid do
6
+ context 'with a symbol' do
7
+ subject { Person.value_of :id }
8
+ it { should have(100).ids }
9
+ it { should eq((1..100).to_a) }
10
+ end
11
+
12
+ context 'with a string' do
13
+ subject { Person.value_of 'id' }
14
+ it { should have(100).ids }
15
+ it { should eq((1..100).to_a) }
16
+ end
17
+
18
+ context 'with multiple values' do
19
+ subject { Person.values_of :id, :last_name }
20
+ it { should have(100).elements }
21
+ it { should eq((1..100).map {|n| [n, "Number#{n}"]})}
22
+ end
23
+
24
+ context 'with a datetime column' do
25
+ subject { Person.value_of :created_at }
26
+ it { should have(100).datetimes }
27
+ it { should be_all {|d| Time === d}}
28
+ end
29
+
30
+ context 'with a serialized column' do
31
+ subject { Person.value_of :extra_info }
32
+ it { should have(100).hashes }
33
+ it { should eq 1.upto(100).map {|n| {:a_key => "Value Number #{n}"} }}
34
+ end
35
+
36
+ context 'with an alternate primary key and an alternate primary key select' do
37
+ subject { Widget.value_of :widget_id }
38
+ it { should have(100).ids }
39
+ it { should eq((1..100).to_a)}
40
+ end
41
+
42
+ context 'with a scope' do
43
+ subject { Person.where(:id => [1,50,100]).value_of :last_name }
44
+ it { should have(3).last_names }
45
+ it { should eq ['Number1', 'Number50', 'Number100'] }
46
+ end
47
+
48
+ context 'with a scope and value_of syntax' do
49
+ subject { Person.where(:id => [1,50,100]).value_of :id }
50
+ it { should have(3).ids }
51
+ it { should eq [1,50,100] }
52
+ end
53
+
54
+ context 'with a scope, an alternate primary key, and an alternate primary key select' do
55
+ subject { Widget.where(:widget_id => [1,50,100]).value_of :widget_id }
56
+ it { should have(3).widget_ids }
57
+ it { should eq [1,50,100]}
58
+ end
59
+
60
+ context 'with a scope and multiple keys' do
61
+ subject { Person.where(:id => [1,50,100]).values_of(:last_name, :id, :extra_info) }
62
+ it { should have(3).elements }
63
+ it { should eq [1,50,100].map {|n| ["Number#{n}", n, {:a_key => "Value Number #{n}"}]}}
64
+ end
65
+
66
+ context 'with an association' do
67
+ subject { Person.first.widgets.value_of :widget_id }
68
+ it { should have(10).elements }
69
+ it { should eq Person.first.widgets.map(&:id) }
70
+ end
71
+
72
+ context 'with joined association' do
73
+ subject { Person.joins(:widgets).value_of :widget_id }
74
+ it { should have(100).ids }
75
+ it { should eq (1..100).to_a }
76
+ end
77
+
78
+ context 'with joined association and multiple keys' do
79
+ subject { Person.joins(:widgets).values_of :last_name, :widget_id }
80
+ it { should have(100).elements }
81
+ it { should eq((1..100).map {|n| ["Number#{n % 10 + 1}", n]})}
82
+ end
83
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'pluckeroid'
5
+ require_relative 'support/schema'
6
+
7
+ RSpec.configure do |config|
8
+ config.before(:suite) do
9
+ Schema.create
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection \
4
+ :adapter => 'sqlite3',
5
+ :database => ':memory:'
6
+
7
+ class Person < ActiveRecord::Base
8
+ has_many :widgets
9
+ serialize :extra_info
10
+ end
11
+
12
+ class Widget < ActiveRecord::Base
13
+ self.primary_key = :widget_id
14
+ belongs_to :person
15
+ serialize :extra_info
16
+ end
17
+
18
+ module Schema
19
+ def self.create
20
+ ActiveRecord::Base.silence do
21
+ ActiveRecord::Migration.verbose = false
22
+
23
+ ActiveRecord::Schema.define do
24
+ create_table :people, :force => true do |t|
25
+ t.string :first_name
26
+ t.string :last_name
27
+ t.integer :age
28
+ t.text :extra_info
29
+ t.timestamps
30
+ end
31
+
32
+ create_table :widgets, :force => true, :primary_key => :widget_id do |t|
33
+ t.belongs_to :person
34
+ t.string :name
35
+ t.text :extra_info
36
+ t.timestamps
37
+ end
38
+ end
39
+ end
40
+
41
+ 1.upto(100) do |num|
42
+ Person.create! :first_name => "Person", :last_name => "Number#{num}", :age => num % 99,
43
+ :extra_info => {:a_key => "Value Number #{num}"}
44
+ end
45
+
46
+ 1.upto(100) do |num|
47
+ Widget.create! :person_id => num % 10 + 1, :name => "Widget #{num}",
48
+ :extra_info => {:a_key => "Value Number #{num}"}
49
+ end
50
+
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pluckeroid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dimko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.7
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Pluck for ActiveRecord on steroids
84
+ email:
85
+ - deemox@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/pluckeroid.rb
97
+ - lib/pluckeroid/active_record/base.rb
98
+ - lib/pluckeroid/active_record/relation.rb
99
+ - lib/pluckeroid/version.rb
100
+ - pluckeroid.gemspec
101
+ - spec/pluckeroid/pluckeroid_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/support/schema.rb
104
+ homepage: https://github.com/dimko/pluckeroid
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.1.5
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Pluck for ActiveRecord on steroids
128
+ test_files:
129
+ - spec/pluckeroid/pluckeroid_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/schema.rb