activerecord-refinements 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-refinements.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Akira Matsuda
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,29 @@
1
+ # ActiveRecord::Refinements
2
+
3
+ ActiveRecord + Ruby 2.0 refinements
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord-refinements'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord-refinements
18
+
19
+ ## Usage
20
+
21
+ Please read the spec.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord-refinements/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "activerecord-refinements"
8
+ gem.version = Activerecord::Refinements::VERSION
9
+ gem.authors = ["Akira Matsuda"]
10
+ gem.email = ["ronnie@dio.jp"]
11
+ gem.description = 'Adding clean and powerful query syntax on AR using refinements'
12
+ gem.summary = 'ActiveRecord + Ruby 2.0 refinements'
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'activerecord', ['>= 0']
21
+ gem.add_development_dependency 'sqlite3', ['>= 0']
22
+ gem.add_development_dependency 'rspec', ['>= 0']
23
+ end
@@ -0,0 +1,50 @@
1
+ require 'activerecord-refinements/version'
2
+ require 'active_record'
3
+
4
+ module ActiveRecord
5
+ module Refinements
6
+ refine Symbol do
7
+ def ==(val)
8
+ {self => val}
9
+ end
10
+
11
+ def !=(val)
12
+ ["#{self} <> ?", val]
13
+ end
14
+
15
+ def =~(val)
16
+ ["#{self} like ?", val]
17
+ end
18
+
19
+ def >(val)
20
+ ["#{self} > ?", val]
21
+ end
22
+
23
+ def >=(val)
24
+ ["#{self} >= ?", val]
25
+ end
26
+
27
+ def <(val)
28
+ ["#{self} < ?", val]
29
+ end
30
+
31
+ def <=(val)
32
+ ["#{self} <= ?", val]
33
+ end
34
+ end
35
+ end
36
+
37
+ module QueryMethods
38
+ def where(opts = nil, *rest, &block)
39
+ return self if opts.blank? && block.nil?
40
+
41
+ relation = clone
42
+ if block
43
+ relation.where_values += build_where(Module.new { using ActiveRecord::Refinements }.module_eval(&block))
44
+ else
45
+ relation.where_values += build_where(opts, rest)
46
+ end
47
+ relation
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Activerecord
2
+ module Refinements
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
+ Bundler.require
7
+
8
+ require 'active_record'
9
+
10
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
11
+ ActiveRecord::Base.establish_connection('test')
12
+
13
+ class User < ActiveRecord::Base
14
+ # scope :tender, where { :name =~ 'tender%' }
15
+ end
16
+
17
+ class CreateAllTables < ActiveRecord::Migration
18
+ def up
19
+ create_table(:users) {|t| t.string :name; t.integer :age}
20
+ end
21
+ end
22
+ ActiveRecord::Migration.verbose = false
23
+ CreateAllTables.new.up
24
+
25
+ # app.config.root = File.dirname(__FILE__)
26
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Symbol enhancements' do
4
+ describe '#==' do
5
+ subject { User.where { :name == 'matz' }.to_sql }
6
+ it { should =~ /WHERE "users"."name" = 'matz'/ }
7
+ end
8
+
9
+ describe '#=~' do
10
+ subject { User.where { :name != 'nobu' }.to_sql }
11
+ it { should =~ /WHERE \(name <> 'nobu'\)/ }
12
+ end
13
+
14
+ describe '#>' do
15
+ subject { User.where { :age > 3 }.to_sql }
16
+ it { should =~ /WHERE \(age > 3\)/ }
17
+ end
18
+
19
+ describe '#>=' do
20
+ subject { User.where { :age >= 18 }.to_sql }
21
+ it { should =~ /WHERE \(age >= 18\)/ }
22
+ end
23
+
24
+ describe '#<' do
25
+ subject { User.where { :age < 60 }.to_sql }
26
+ it { should =~ /WHERE \(age < 60\)/ }
27
+ end
28
+
29
+ describe '#<=' do
30
+ subject { User.where { :age <= 35 }.to_sql }
31
+ it { should =~ /WHERE \(age <= 35\)/ }
32
+ end
33
+
34
+ describe '#=~' do
35
+ subject { User.where { :name =~ 'tender%' }.to_sql }
36
+ it { should =~ /WHERE \(name like 'tender%'\)/ }
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-refinements
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Akira Matsuda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sqlite3
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: Adding clean and powerful query syntax on AR using refinements
63
+ email:
64
+ - ronnie@dio.jp
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - .gitignore
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - activerecord-refinements.gemspec
78
+ - lib/activerecord-refinements.rb
79
+ - lib/activerecord-refinements/version.rb
80
+ - spec/spec_helper.rb
81
+ - spec/where_spec.rb
82
+ homepage: ""
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.24
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: ActiveRecord + Ruby 2.0 refinements
115
+ test_files:
116
+ - spec/spec_helper.rb
117
+ - spec/where_spec.rb