valium 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -6
- data/lib/valium.rb +10 -14
- data/lib/valium/version.rb +1 -1
- data/spec/valium/valium_spec.rb +31 -54
- metadata +8 -8
data/README.md
CHANGED
@@ -15,8 +15,6 @@ just the values you're interested in seeing.
|
|
15
15
|
You can select a single value...
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
Post.where(:published => true)[:title]
|
19
|
-
# - OR -
|
20
18
|
Post.where(:published => true).value_of :title
|
21
19
|
# => ["First Post", "Another Awesome Post", ...]
|
22
20
|
```
|
@@ -24,9 +22,7 @@ Post.where(:published => true).value_of :title
|
|
24
22
|
... or several ...
|
25
23
|
|
26
24
|
```ruby
|
27
|
-
Employee.where(:title => 'Sr. Monkey Wrangler')
|
28
|
-
# - OR -
|
29
|
-
Employee.where(:title => 'Sr. Monkey Wrangler').value_of :first_name, :last_name, :hired_at
|
25
|
+
Employee.where(:title => 'Sr. Monkey Wrangler').values_of :first_name, :last_name, :hired_at
|
30
26
|
# => [["Ernie", "Miller", 2009-09-21 08:00:00 -0400],
|
31
27
|
["Herb", "Myers", 2002-02-13 09:00:00 -0400], ...]
|
32
28
|
```
|
@@ -40,7 +36,7 @@ class Animal < ActiveRecord::Base
|
|
40
36
|
serialize :extra_info
|
41
37
|
end
|
42
38
|
|
43
|
-
Animal.where(:genus => 'felis')
|
39
|
+
Animal.where(:genus => 'felis').values_of :species, :extra_info
|
44
40
|
# => [["catus", {:domestic => true}], ["lolcatus", {:can_has_cheezburger => true}], ...]
|
45
41
|
```
|
46
42
|
|
data/lib/valium.rb
CHANGED
@@ -49,7 +49,7 @@ module Valium
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
alias :
|
52
|
+
alias :values_of :value_of
|
53
53
|
|
54
54
|
def valium_select_multiple(attr_names)
|
55
55
|
columns = attr_names.map {|n| columns_hash[n]}
|
@@ -87,25 +87,21 @@ module Valium
|
|
87
87
|
|
88
88
|
module ValueOf
|
89
89
|
def value_of(*args)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
90
|
+
args.map! do |attr_name|
|
91
|
+
attr_name = attr_name.to_s
|
92
|
+
attr_name == 'id' ? klass.primary_key : attr_name
|
93
|
+
end
|
95
94
|
|
96
|
-
|
97
|
-
|
98
|
-
else
|
99
|
-
scoping { klass[*args] }
|
100
|
-
end
|
95
|
+
if loaded? && (empty? || args.all? {|a| first.attributes.has_key? a})
|
96
|
+
to_a.map {|record| args.map {|a| record[a]}}
|
101
97
|
else
|
102
|
-
|
98
|
+
scoping { klass.value_of *args }
|
103
99
|
end
|
104
100
|
end
|
105
|
-
|
106
|
-
alias :[] :value_of
|
107
101
|
end
|
108
102
|
|
103
|
+
alias :values_of :value_of
|
104
|
+
|
109
105
|
end # Major version check
|
110
106
|
end
|
111
107
|
|
data/lib/valium/version.rb
CHANGED
data/spec/valium/valium_spec.rb
CHANGED
@@ -2,56 +2,50 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Valium do
|
4
4
|
|
5
|
-
context 'with a symbol
|
6
|
-
subject { Person[:id] }
|
7
|
-
it { should have(100).ids }
|
8
|
-
it { should eq((1..100).to_a) }
|
9
|
-
end
|
10
|
-
|
11
|
-
context 'with value_of syntax' do
|
5
|
+
context 'with a symbol' do
|
12
6
|
subject { Person.value_of :id }
|
13
7
|
it { should have(100).ids }
|
14
8
|
it { should eq((1..100).to_a) }
|
15
9
|
end
|
16
10
|
|
17
|
-
context 'with a string
|
18
|
-
subject { Person
|
11
|
+
context 'with a string' do
|
12
|
+
subject { Person.value_of 'id' }
|
19
13
|
it { should have(100).ids }
|
20
14
|
it { should eq((1..100).to_a) }
|
21
15
|
end
|
22
16
|
|
23
|
-
context 'with multiple
|
24
|
-
subject { Person
|
17
|
+
context 'with multiple values' do
|
18
|
+
subject { Person.values_of :id, :last_name }
|
25
19
|
it { should have(100).elements }
|
26
20
|
it { should eq((1..100).map {|n| [n, "Number#{n}"]})}
|
27
21
|
end
|
28
22
|
|
29
23
|
context 'with a datetime column' do
|
30
|
-
subject { Person
|
24
|
+
subject { Person.value_of :created_at }
|
31
25
|
it { should have(100).datetimes }
|
32
26
|
it { should be_all {|d| Time === d}}
|
33
27
|
end
|
34
28
|
|
35
29
|
context 'with a serialized column' do
|
36
|
-
subject { Person
|
30
|
+
subject { Person.value_of :extra_info }
|
37
31
|
it { should have(100).hashes }
|
38
32
|
it { should eq 1.upto(100).map {|n| {:a_key => "Value Number #{n}"} }}
|
39
33
|
end
|
40
34
|
|
41
35
|
context 'with an alternate primary key and an :id select' do
|
42
|
-
subject { Widget
|
36
|
+
subject { Widget.value_of :id }
|
43
37
|
it { should have(100).ids }
|
44
|
-
it { should eq
|
38
|
+
it { should eq((1..100).to_a)}
|
45
39
|
end
|
46
40
|
|
47
41
|
context 'with an alternate primary key and an alternate primary key select' do
|
48
|
-
subject { Widget
|
42
|
+
subject { Widget.value_of :widget_id }
|
49
43
|
it { should have(100).ids }
|
50
|
-
it { should eq
|
44
|
+
it { should eq((1..100).to_a)}
|
51
45
|
end
|
52
46
|
|
53
47
|
context 'with a scope' do
|
54
|
-
subject { Person.where(:id => [1,50,100])
|
48
|
+
subject { Person.where(:id => [1,50,100]).value_of :last_name }
|
55
49
|
it { should have(3).last_names }
|
56
50
|
it { should eq ['Number1', 'Number50', 'Number100'] }
|
57
51
|
end
|
@@ -63,13 +57,13 @@ describe Valium do
|
|
63
57
|
end
|
64
58
|
|
65
59
|
context 'with a scope, an alternate primary key, and an :id select' do
|
66
|
-
subject {Widget.where(:widget_id => [1,50,100])
|
60
|
+
subject {Widget.where(:widget_id => [1,50,100]).value_of :id }
|
67
61
|
it { should have(3).ids }
|
68
62
|
it { should eq [1,50,100]}
|
69
63
|
end
|
70
64
|
|
71
65
|
context 'with a scope, an alternate primary key, and an alternate primary key select' do
|
72
|
-
subject { Widget.where(:widget_id => [1,50,100])
|
66
|
+
subject { Widget.where(:widget_id => [1,50,100]).value_of :widget_id }
|
73
67
|
it { should have(3).widget_ids }
|
74
68
|
it { should eq [1,50,100]}
|
75
69
|
end
|
@@ -83,10 +77,10 @@ describe Valium do
|
|
83
77
|
|
84
78
|
# We'll generate the first query when we call "subject", but won't
|
85
79
|
# need another query
|
86
|
-
specify { queries_for { subject
|
80
|
+
specify { queries_for { subject.value_of :id }.should have(1).query }
|
87
81
|
|
88
|
-
specify { subject
|
89
|
-
should eq Person.where(:id => [1,50,100])
|
82
|
+
specify { subject.values_of(:id, :created_at, :extra_info).
|
83
|
+
should eq Person.where(:id => [1,50,100]).values_of(:id, :created_at, :extra_info) }
|
90
84
|
end
|
91
85
|
|
92
86
|
context 'with a loaded scope, an alternate primary key, and an :id select' do
|
@@ -98,10 +92,10 @@ describe Valium do
|
|
98
92
|
|
99
93
|
# We'll generate the first query when we call "subject", but won't
|
100
94
|
# need another query
|
101
|
-
specify { queries_for { subject
|
95
|
+
specify { queries_for { subject.value_of :id }.should have(1).query }
|
102
96
|
|
103
|
-
specify { subject
|
104
|
-
should eq Widget.where(:widget_id => [1,50,100])
|
97
|
+
specify { subject.values_of(:id, :created_at, :extra_info).
|
98
|
+
should eq Widget.where(:widget_id => [1,50,100]).values_of(:id, :created_at, :extra_info) }
|
105
99
|
end
|
106
100
|
|
107
101
|
context 'with a loaded scope but missing attributes' do
|
@@ -113,13 +107,13 @@ describe Valium do
|
|
113
107
|
|
114
108
|
# We'll generate the first query when we call "subject", but won't
|
115
109
|
# need another query
|
116
|
-
specify { queries_for { subject
|
110
|
+
specify { queries_for { subject.value_of(:id) }.should have(1).query }
|
117
111
|
|
118
112
|
# We'll need to run our own query for the attributes
|
119
|
-
specify { queries_for { subject
|
113
|
+
specify { queries_for { subject.value_of(:first_name) }.should have(2).queries }
|
120
114
|
|
121
|
-
specify { subject
|
122
|
-
should eq Person.where(:id => [1,50,100])
|
115
|
+
specify { subject.values_of(:id, :created_at, :extra_info).
|
116
|
+
should eq Person.where(:id => [1,50,100]).values_of(:id, :created_at, :extra_info) }
|
123
117
|
end
|
124
118
|
|
125
119
|
context 'with a loaded scope, an alternate primary key, and missing attributes' do
|
@@ -131,41 +125,24 @@ describe Valium do
|
|
131
125
|
|
132
126
|
# We'll generate the first query when we call "subject", but won't
|
133
127
|
# need another query
|
134
|
-
specify { queries_for { subject
|
135
|
-
specify { queries_for { subject
|
128
|
+
specify { queries_for { subject.value_of(:id) }.should have(1).query }
|
129
|
+
specify { queries_for { subject.value_of(:widget_id) }.should have(1).query }
|
136
130
|
|
137
131
|
# We'll need to run our own query for the attributes
|
138
|
-
specify { queries_for { subject
|
132
|
+
specify { queries_for { subject.value_of :name }.should have(2).queries }
|
139
133
|
|
140
|
-
specify { subject
|
141
|
-
should eq Widget.where(:widget_id => [1,50,100])
|
134
|
+
specify { subject.values_of(:id, :created_at, :extra_info).
|
135
|
+
should eq Widget.where(:widget_id => [1,50,100]).values_of(:id, :created_at, :extra_info) }
|
142
136
|
end
|
143
137
|
|
144
138
|
context 'with a scope and multiple keys' do
|
145
|
-
subject { Person.where(:id => [1,50,100])
|
139
|
+
subject { Person.where(:id => [1,50,100]).values_of(:last_name, :id, :extra_info) }
|
146
140
|
it { should have(3).elements }
|
147
141
|
it { should eq [1,50,100].map {|n| ["Number#{n}", n, {:a_key => "Value Number #{n}"}]}}
|
148
142
|
end
|
149
143
|
|
150
|
-
context 'with a relation array index' do
|
151
|
-
subject { Person.where(:id => [1,50,100])[1] }
|
152
|
-
it { should eq Person.find(50) }
|
153
|
-
end
|
154
|
-
|
155
|
-
context 'with a relation array start and length' do
|
156
|
-
subject { Person.where(:id => 1..20)[10,3] }
|
157
|
-
it { should have(3).people }
|
158
|
-
it { should eq Person.offset(10).limit(3) }
|
159
|
-
end
|
160
|
-
|
161
|
-
context 'with a relation array range' do
|
162
|
-
subject { Person.where(:id => 1..20)[0..9] }
|
163
|
-
it { should have(10).people }
|
164
|
-
it { should eq Person.first(10) }
|
165
|
-
end
|
166
|
-
|
167
144
|
context 'with an association' do
|
168
|
-
subject { Person.first.widgets
|
145
|
+
subject { Person.first.widgets.value_of :id }
|
169
146
|
it { should have(10).elements }
|
170
147
|
it { should eq Person.first.widgets.map(&:id) }
|
171
148
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-08 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70207893119400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70207893119400
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70207893118360 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.6.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70207893118360
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70207893117720 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 1.3.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70207893117720
|
47
47
|
description: ! "\n Suffering from ActiveRecord instantiation anxiety? Try Valium.
|
48
48
|
It\n saves your CPU and memory for more important things, retrieving\n just
|
49
49
|
the values you're interested in seeing.\n "
|