sexy_scopes 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ coverage
2
+ rdoc
3
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Samuel Lebeau
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 ADDED
@@ -0,0 +1,34 @@
1
+ sexy_scopes
2
+ ===========
3
+
4
+ sexy_scope is a small wrapper around `Arel::Attribute` that adds a little syntactic
5
+ sugar when creating scopes in ActiveRecord.
6
+ It adds an `attribute` class method which takes an attribute name and returns an
7
+ `Arel::Attribute` wrapper, which responds to common operators to return predicates
8
+ objects that can be used as arguments to `ActiveRecord::Base.where`.
9
+
10
+ Examples
11
+ --------
12
+
13
+ class Product < ActiveRecord::Base
14
+ scope :untitled, where(attribute(:name) == nil)
15
+
16
+ def self.cheaper_than(price)
17
+ where attribute(:price) < price
18
+ end
19
+
20
+ def self.search(term)
21
+ where attribute(:name) =~ "%#{term}%"
22
+ end
23
+ end
24
+
25
+ Classic `Arel::Attribute` methods (`lt`, `in`, `matches`, `not`, etc.) are still available
26
+ and predicates can be chained using `and` and `or`:
27
+
28
+ (Product.attribute(:name) == nil).and(Product.attribute(:category).in(["foo", "bar"])).to_sql
29
+ # => ("products"."name" IS NULL AND "products"."category" IN ('foo', 'bar'))
30
+
31
+ Copyright
32
+ ---------
33
+
34
+ Copyright (c) 2010 Samuel Lebeau. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sexy_scopes"
8
+ gem.summary = %Q{Small DSL to create ActiveRecord attribute predicates without writing SQL.}
9
+ gem.description = %Q{Small DSL to create ActiveRecord attribute predicates without writing SQL.}
10
+ gem.email = "samuel.lebeau@gmail.com"
11
+ gem.homepage = "http://github.com/samleb/sexy_scopes"
12
+ gem.authors = ["Samuel Lebeau"]
13
+ gem.add_dependency "activerecord", ">= 3.0.0.beta"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,22 @@
1
+ require 'delegate'
2
+ require 'active_record'
3
+ require 'arel'
4
+
5
+ module SexyScopes
6
+ module ClassMethods
7
+ def attribute(name)
8
+ Attribute.new(arel_table[name])
9
+ end
10
+ end
11
+
12
+ class Attribute < DelegateClass(Arel::Attribute)
13
+ alias_method :<, :lt
14
+ alias_method :<=, :lteq
15
+ alias_method :==, :eq
16
+ alias_method :>=, :gteq
17
+ alias_method :>, :gt
18
+ alias_method :=~, :matches
19
+ end
20
+
21
+ ActiveRecord::Base.extend ClassMethods
22
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sexy_scopes'
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sexy_scopes}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Samuel Lebeau"]
12
+ s.date = %q{2010-05-28}
13
+ s.description = %q{Small DSL to create ActiveRecord attribute predicates without writing SQL.}
14
+ s.email = %q{samuel.lebeau@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/sexy_scopes.rb",
26
+ "rails/init.rb",
27
+ "sexy_scopes.gemspec",
28
+ "spec/sexy_scopes_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/samleb/sexy_scopes}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Small DSL to create ActiveRecord attribute predicates without writing SQL.}
37
+ s.test_files = [
38
+ "spec/sexy_scopes_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0.beta"])
48
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
49
+ s.add_development_dependency(%q<yard>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta"])
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_dependency(%q<yard>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta"])
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_dependency(%q<yard>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SexyScopes do
4
+ it "extends `ActiveRecord::Base` with `SexyScopes::ClassMethods`" do
5
+ ActiveRecord::Base.singleton_class.included_modules.should include(SexyScopes::ClassMethods)
6
+ end
7
+
8
+ describe "the `attribute` class method" do
9
+ before do
10
+ @attribute = User.attribute(:username)
11
+ end
12
+
13
+ it "returns a `SexyScopes::Attribute` instance for the given attribute" do
14
+ @attribute.should be_a(SexyScopes::Attribute)
15
+ @attribute.name.should == :username
16
+ end
17
+ end
18
+
19
+ describe SexyScopes::Attribute do
20
+ before do
21
+ @attribute = User.attribute(:username)
22
+ @arel_attribute = User.arel_table[:username]
23
+ end
24
+
25
+ AREL_ALIASES = {
26
+ :< => :lt,
27
+ :<= => :lteq,
28
+ :== => :eq,
29
+ :>= => :gteq,
30
+ :> => :gt,
31
+ :=~ => :matches
32
+ }
33
+
34
+ AREL_ALIASES.each do |method, arel_method|
35
+ it "aliases `Arel::Attribute##{arel_method}` with operator `#{method}`" do
36
+ @attribute.send(method, :any_value).should == @arel_attribute.send(arel_method, :any_value)
37
+ end
38
+ end
39
+
40
+ (AREL_ALIASES.values + ['in']).each do |arel_method|
41
+ it "behaves like an `Arel::Attribute` when sent `#{arel_method}`" do
42
+ @attribute.send(arel_method, :any_value).should == @arel_attribute.send(arel_method, :any_value)
43
+ end
44
+ end
45
+ end
46
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'active_record'
4
+ require 'sexy_scopes'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
+
10
+ ActiveRecord::Schema.verbose = false
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :users do |t|
13
+ t.string :username
14
+ end
15
+ end
16
+
17
+ class User < ActiveRecord::Base
18
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sexy_scopes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Samuel Lebeau
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-28 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 31098225
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ - beta
35
+ version: 3.0.0.beta
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 13
47
+ segments:
48
+ - 1
49
+ - 2
50
+ - 9
51
+ version: 1.2.9
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: yard
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: Small DSL to create ActiveRecord attribute predicates without writing SQL.
69
+ email: samuel.lebeau@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.md
77
+ files:
78
+ - .gitignore
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - VERSION
83
+ - lib/sexy_scopes.rb
84
+ - rails/init.rb
85
+ - sexy_scopes.gemspec
86
+ - spec/sexy_scopes_spec.rb
87
+ - spec/spec.opts
88
+ - spec/spec_helper.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/samleb/sexy_scopes
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Small DSL to create ActiveRecord attribute predicates without writing SQL.
123
+ test_files:
124
+ - spec/sexy_scopes_spec.rb
125
+ - spec/spec_helper.rb