valium 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - ree
5
+ - rbx
6
+ - ruby-head
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
3
 
4
+ gem 'rake'
5
+
4
6
  rails = ENV['RAILS'] || 'master'
5
7
 
6
8
  case rails
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ernie Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Valium
1
+ # Valium [![Build Status](https://secure.travis-ci.org/ernie/valium.png)](http://travis-ci.org/ernie/valium)
2
2
 
3
3
  Suffering from ActiveRecord instantiation anxiety? Try Valium. It
4
4
  saves your CPU and memory for more important things, retrieving
@@ -81,6 +81,14 @@ spent in the deserialization process than with normal attributes,
81
81
  Valium can be up to twice as fast as mapping over ActiveRecord
82
82
  objects, in my tests.
83
83
 
84
+ ## Limitations
85
+
86
+ Valium will only retrieve columns from the model you are querying against.
87
+ Joined associations may be used to limit the scope of the query, but their
88
+ attribute values can't be selected. There's a bit of discussion on
89
+ [issue #2](https://github.com/ernie/valium/issues/2) about why this is,
90
+ if you're interested in reading more.
91
+
84
92
  ## Conclusion
85
93
 
86
94
  You knew everything I mentioned above, already. In fact,
data/lib/valium.rb CHANGED
@@ -6,6 +6,12 @@ module Valium
6
6
 
7
7
  if ActiveRecord::VERSION::MINOR == 0 # We need to use the old deserialize code
8
8
 
9
+ CollectionProxy = ActiveRecord::Associations::AssociationProxy
10
+
11
+ CollectionProxy.class_eval do
12
+ delegate :scoping, :klass, :to => :scoped
13
+ end
14
+
9
15
  def valium_deserialize(value, klass)
10
16
  if value.is_a?(String) && value =~ /^---/
11
17
  result = YAML::load(value) rescue value
@@ -22,6 +28,8 @@ module Valium
22
28
 
23
29
  else # we're on 3.1+, yay for coder.load!
24
30
 
31
+ CollectionProxy = ActiveRecord::Associations::CollectionProxy
32
+
25
33
  def valium_deserialize(value, coder)
26
34
  coder.load(value)
27
35
  end
@@ -77,18 +85,18 @@ module Valium
77
85
  end
78
86
  end
79
87
 
80
- module Relation
88
+ module ValueOf
81
89
  def value_of(*args)
82
90
  if args.size > 0 && args.all? {|a| String === a || Symbol === a}
83
91
  args.map! do |attr_name|
84
92
  attr_name = attr_name.to_s
85
- attr_name == 'id' ? @klass.primary_key : attr_name
93
+ attr_name == 'id' ? klass.primary_key : attr_name
86
94
  end
87
95
 
88
96
  if loaded? && (empty? || args.all? {|a| first.attributes.has_key? a})
89
97
  to_a.map {|record| args.map {|a| record[a]}}
90
98
  else
91
- scoping { @klass[*args] }
99
+ scoping { klass[*args] }
92
100
  end
93
101
  else
94
102
  to_a[*args]
@@ -102,4 +110,5 @@ module Valium
102
110
  end
103
111
 
104
112
  ActiveRecord::Base.extend Valium
105
- ActiveRecord::Relation.send :include, Valium::Relation
113
+ ActiveRecord::Relation.send :include, Valium::ValueOf
114
+ Valium::CollectionProxy.send :include, Valium::ValueOf
@@ -1,3 +1,3 @@
1
1
  module Valium
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,10 +6,12 @@ ActiveRecord::Base.establish_connection(
6
6
  )
7
7
 
8
8
  class Person < ActiveRecord::Base
9
+ has_many :widgets
9
10
  serialize :extra_info
10
11
  end
11
12
 
12
13
  class Widget < ActiveRecord::Base
14
+ belongs_to :person
13
15
  serialize :extra_info
14
16
  set_primary_key :widget_id
15
17
  end
@@ -29,8 +31,9 @@ module Schema
29
31
  end
30
32
 
31
33
  create_table :widgets, :force => true, :primary_key => :widget_id do |t|
32
- t.string :name
33
- t.text :extra_info
34
+ t.belongs_to :person
35
+ t.string :name
36
+ t.text :extra_info
34
37
  t.timestamps
35
38
  end
36
39
  end
@@ -42,7 +45,7 @@ module Schema
42
45
  end
43
46
 
44
47
  1.upto(100) do |num|
45
- Widget.create! :name => "Widget #{num}",
48
+ Widget.create! :person_id => num % 10, :name => "Widget #{num}",
46
49
  :extra_info => {:a_key => "Value Number #{num}"}
47
50
  end
48
51
 
@@ -164,4 +164,10 @@ describe Valium do
164
164
  it { should eq Person.first(10) }
165
165
  end
166
166
 
167
+ context 'with an association' do
168
+ subject { Person.first.widgets[:id] }
169
+ it { should have(10).elements }
170
+ it { should eq Person.first.widgets.map(&:id) }
171
+ end
172
+
167
173
  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.2.1
4
+ version: 0.3.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-08-25 00:00:00.000000000Z
12
+ date: 2011-09-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70159201437300 !ruby/object:Gem::Requirement
16
+ requirement: &70286893107380 !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: *70159201437300
24
+ version_requirements: *70286893107380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70159201401980 !ruby/object:Gem::Requirement
27
+ requirement: &70286893106720 !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: *70159201401980
35
+ version_requirements: *70286893106720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70159201388400 !ruby/object:Gem::Requirement
38
+ requirement: &70286893106000 !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: *70159201388400
46
+ version_requirements: *70286893106000
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 "
@@ -54,7 +54,9 @@ extensions: []
54
54
  extra_rdoc_files: []
55
55
  files:
56
56
  - .gitignore
57
+ - .travis.yml
57
58
  - Gemfile
59
+ - LICENSE
58
60
  - README.md
59
61
  - Rakefile
60
62
  - lib/valium.rb
@@ -85,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  version: '0'
86
88
  requirements: []
87
89
  rubyforge_project: valium
88
- rubygems_version: 1.8.6
90
+ rubygems_version: 1.8.10
89
91
  signing_key:
90
92
  specification_version: 3
91
93
  summary: Access attribute values directly, without instantiating ActiveRecord objects.