active_index 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ Adds index option to active record finds:
2
+
3
+ Lamp.find(:all, :index => 'index_lamps_on_name', :conditions => {:name => 'ryan'})
4
+
5
+ Have to use a string since figuring out what index to use (unique or not)
6
+ is not deterministic.
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "active_index"
5
+ gemspec.summary = "Add index option to active record without using the from hack directly"
6
+ gemspec.email = "ryan@angilly.com"
7
+ gemspec.homepage = "http://github.com/ryana/active_index"
8
+ gemspec.authors = ["Ryan Angilly"]
9
+ end
10
+ rescue LoadError
11
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
12
+ end
13
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,2 @@
1
+ $: << File.dirname(__FILE__)
2
+ require 'active_index/core'
@@ -0,0 +1,45 @@
1
+ module ActiveIndex
2
+ module Core
3
+ module ClassMethods
4
+
5
+ def find_with_index(*args)
6
+ args_dup = args.dup
7
+
8
+ case args.first
9
+ when Symbol
10
+ h = args.second
11
+ else
12
+ h = nil
13
+ end
14
+
15
+ if h && (h[:index])
16
+ index_option = h.delete(:index)
17
+ args_dup.second.merge!(:from => "`#{table_name}` USE INDEX `#{index_name_for(index_option)}`")
18
+ else
19
+ end
20
+
21
+ find_without_index(*args_dup)
22
+ end
23
+
24
+ private
25
+ def index_name_for(option)
26
+ case option
27
+ # when Array
28
+ # "index_on_" + option.map(&:to_s).join('_')
29
+ # when Symbol
30
+ # "index_on_#{option}"
31
+ when String
32
+ option
33
+ else
34
+ raise 'Must pass String to index option'
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.extend ActiveIndex::Core::ClassMethods
43
+ class << ActiveRecord::Base
44
+ alias_method_chain :find, :index
45
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'sqlite3'
3
+ require 'active_record'
4
+ require File.dirname(__FILE__) + '/../lib/active_index'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'mocha'
8
+
9
+ MY_DB_NAME = 'test.db.sqlite3'
10
+ MY_DB = SQLite3::Database.new(MY_DB_NAME)
11
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)
12
+ ActiveRecord::Base.connection.execute 'DROP TABLE IF EXISTS `lamps`'
13
+ ActiveRecord::Base.connection.create_table :lamps do |t|
14
+ t.integer :id
15
+ t.string :name
16
+ end
17
+
18
+ class Lamp < ActiveRecord::Base
19
+ end
20
+
21
+ class ActiveIndexTest < Test::Unit::TestCase
22
+
23
+ context "The Lamp class" do
24
+ should "respond to find_with_index" do
25
+ assert Lamp.respond_to?(:find_with_index)
26
+ assert Lamp.respond_to?(:find_without_index)
27
+ end
28
+ end
29
+
30
+ context 'With some records' do
31
+ setup do
32
+ Lamp.create!(:name => 'kanye')
33
+ Lamp.create!(:name => 'ryan')
34
+ end
35
+
36
+ should "be alias method chained" do
37
+ Lamp.expects(:find_without_index).once
38
+ Lamp.expects(:find_with_index).never
39
+ Lamp.find_by_name('33')
40
+ end
41
+
42
+ should "still work with find" do
43
+ assert !Lamp.find_by_name('whoa')
44
+ end
45
+
46
+ should "find things" do
47
+ assert Lamp.find_by_name('kanye')
48
+ end
49
+
50
+ should "use string when given" do
51
+ str = 'index_on_names_idx'
52
+ Lamp.expects(:find_without_index).with(:first, :from => "`lamps` USE INDEX `#{str}`", :conditions => {:name => 'dude'}).once
53
+ Lamp.find(:first, :index => str, :conditions => {:name => 'dude'})
54
+ end
55
+
56
+ should_eventually "use symbol when given" do
57
+ Lamp.expects(:find_without_index).with(:first, :from => "`lamps` USE INDEX `index_on_name`", :conditions => {:name => 'dude'}).once
58
+ Lamp.find(:first, :index => :name, :conditions => {:name => 'dude'})
59
+ end
60
+
61
+ should_eventually "build out index when given array" do
62
+ Lamp.expects(:find_without_index).with(:first, :from => "`lamps` USE INDEX `index_on_name_id`", :conditions => {:name => 'dude'}).once
63
+ Lamp.find(:first, :index => [:name, :id], :conditions => {:name => 'dude'})
64
+ end
65
+ end
66
+ end
Binary file
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_index
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Angilly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-23 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ryan@angilly.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/active_index.rb
29
+ - lib/active_index/core.rb
30
+ - test/main.rb
31
+ - test/test.db.sqlite3
32
+ has_rdoc: true
33
+ homepage: http://github.com/ryana/active_index
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Add index option to active record without using the from hack directly
60
+ test_files:
61
+ - test/main.rb