find_by_shortcut 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDcwYWE4YjIxMjU5YThjYTEzMjQ3YjM0NDgxZTZlYWViOTU5NWZmNg==
5
+ data.tar.gz: !binary |-
6
+ MDkyM2NmMzk0YzQyNDM5NGYwYWY0YTlmNTQzZTU5NzNiZGRlYTQxNg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZTJkODU2N2NmNDMzYzAxMjVkZDE4MjgxYTAyNjliYzE4MDEyYTJmYjY3NDVm
10
+ ODVkN2JjODE0ZTQwZTg1N2U5YmM2YmZjMWJkNjU5ZWVmYzdiOGEyN2ZkNTA0
11
+ MzU2NWNkNWQ4ODQyNjIxMDQzYmYwYjAwMWJlNTU2ZmVlYmZkODE=
12
+ data.tar.gz: !binary |-
13
+ ZjhlYWZkN2ViMzdjMDg4NzkzMTVkMDYxYzZjNWFhMDhlYmQwNzFkMmE3NTc1
14
+ ZTIyNzJhYjI1N2RhYTcwMGEwZWE2ZjMxYTUzMzQwNjU5NjA4MGQ5ZjQyYjJl
15
+ YWNlZDY0OWZkMDRhNWIyNTQxMjEwOWFiZWE2MDc2YmY5YmU5N2M=
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ .*.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in find_by_shortcut.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Nomitch
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,53 @@
1
+ = find_by_shortcut
2
+
3
+ A module for faster Active Record queries in the ruby console.
4
+
5
+ Allows for quick 'find_by_' calls on classes that inherit from ActiveRecord::Base.
6
+
7
+ Instead of typing "find_by_attribute()" after a Class name, you can type "fb" followed by
8
+ the first character(s) of the attribute you want to search by. If multiple attributes
9
+ begin with those the characters following fb, the first will be selected. Do not add any
10
+ spaces or underscores after "fb".
11
+
12
+ An alias "f" for the "find" instance method on ActiveRecord::Base classes is created.
13
+
14
+ Additionally, a kernel method "f" is added which searches through all ActiveRecord
15
+ objects for an attribute value.
16
+
17
+
18
+ EXAMPLES:
19
+
20
+ Setup: If User inherits from ActiveRecord::Base, and has the following attributes: "id",
21
+ "username", and "email", you can use find_by_shortcut as follows:
22
+
23
+ User.f 1
24
+ - is the equivalent of User.find(1)
25
+
26
+
27
+ User.fbu "OkinawaSteel"
28
+ - is the equivalent of User.find_by_username("OkinawaSteel")
29
+
30
+
31
+ User.fbe "email@isp.com"
32
+
33
+ User.fbem "email@isp.com"
34
+
35
+ User.fbemail "email@isp.com"
36
+
37
+ - are all equivalents of User.find_by_email("email@isp.com")
38
+
39
+ f "email@isp.com"
40
+
41
+ - will search through your Active Record classes until it finds something with 'email@isp.com' as one of its attributes (slow on large databases)
42
+
43
+ == Contributing to find_by_shortcut
44
+
45
+ * Fork the project and do whatever you want.
46
+ * Shoot me an email at 'mikenomitch@gmail.com' if you want to discuss.
47
+ * Start a feature/bugfix branch.
48
+ * Commit and push until you are happy with your contribution.
49
+
50
+ == Copyright
51
+
52
+ Standard MIT License. See LICENSE.txt for more info (basically, feel free to do
53
+ whatever you want with this).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+ require 'rails'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib/find_by_shortcut'
8
+ t.test_files = FileList['test/lib/find_by_shortcut/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'find_by_shortcut/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "find_by_shortcut"
8
+ spec.version = FindByShortcut::VERSION
9
+ spec.authors = ["Mike Nomitch", "Erik Nomitch"]
10
+ spec.email = ["mikenomitch@gmail.com"]
11
+ spec.description = %Q{Shortcuts for Active Record queries in the ruby console.}
12
+ spec.summary = %Q{Allows for quick 'find_by_' calls on classes that inherit from ActiveRecord::Base. Instead of typing "find_by_attribute()" after a Class name, you can type "fb" followed by
13
+ the first character(s) of the attribute you want to search by. If multiple attributes begin with those the characters following fb, the first will be selected. Do not add any spaces or underscores after "fb".}
14
+ spec.homepage = "https://github.com/mikenomitch/find_by_shortcut"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rails"
25
+ end
@@ -0,0 +1,61 @@
1
+ require "find_by_shortcut/version"
2
+
3
+ class ActiveRecord::Base
4
+
5
+ # Open the eigenclass of ActiveRecord::Base because we need to alias a static
6
+ # method.
7
+
8
+ # This would allow you to call Email.f 10
9
+ # Which shouldn't be confused with the 'f' method below.
10
+ class << ActiveRecord::Base
11
+ alias_method :f, :find
12
+ end
13
+
14
+ # LOOK INTO PASSING THE ARGS TO SUPER, MAYBE REMOVE thE *???
15
+
16
+ def self.method_missing(method, *args)
17
+ if method.to_s.starts_with?("fb")
18
+ # Take all characters after "fb"
19
+ remainder = method.to_s[2..-1]
20
+ # Check if there is an attribute that starts with these chatacters
21
+ attribute = self.new.attributes.keys.select{|e| e.starts_with?(remainder)}.first
22
+ # If not, run the normal 'method missing' method
23
+ if attribute == nil
24
+ super
25
+ else
26
+ # but if so, call find by on that attribute
27
+ new_method = "find_by_" + attribute
28
+ send(new_method.to_s, args)
29
+ end
30
+ # if an unknown method doesn't start with fb, run the normal 'method missing' method
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+
37
+ # Feel free to comment out the section below if you don't want eager loading on your application, as this will slow it down a little bit.
38
+ # This section allows you to call something like "f 'example@email.com'" and it will try to find the right class to match the attribute you just called
39
+ # On medium/large databases this will be too slow to be effective
40
+
41
+ def f (value)
42
+ # Load each class before it is explicitly called
43
+ Rails.application.eager_load!
44
+ # Enumerate through all the classes that inherit from ActiveRecord::Base
45
+ ActiveRecord::Base.descendants.each do |klass|
46
+ # Enumberate through each attribute of each class
47
+ klass.new.attributes.keys.each do |attribute|
48
+ # Call find_by for each attribute with the value that came after 'f'
49
+ find_method = "find_by_" + attribute
50
+ if klass.columns_hash[attribute].type.to_s == value.class.to_s.downcase
51
+ item = klass.send(find_method, value)
52
+ if item != nil
53
+ # If something is found, return it and stop the enumeration
54
+ return item
55
+ end
56
+ end
57
+ end
58
+ end
59
+ # If nothing is found, return nil
60
+ return nil
61
+ end
@@ -0,0 +1,3 @@
1
+ module FindByShortcut
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe FindByShortcut do
4
+
5
+ it "must be defined" do
6
+ FindByShortcut::VERSION.wont_be_nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/find_by_shortcut.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: find_by_shortcut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Nomitch
8
+ - Erik Nomitch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rails
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: Shortcuts for Active Record queries in the ruby console.
57
+ email:
58
+ - mikenomitch@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - find_by_shortcut.gemspec
69
+ - lib/find_by_shortcut.rb
70
+ - lib/find_by_shortcut/version.rb
71
+ - test/lib/find_by_shortcut/version_test.rb
72
+ - test/test_helper.rb
73
+ homepage: https://github.com/mikenomitch/find_by_shortcut
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Allows for quick 'find_by_' calls on classes that inherit from ActiveRecord::Base.
97
+ Instead of typing "find_by_attribute()" after a Class name, you can type "fb" followed
98
+ by the first character(s) of the attribute you want to search by. If multiple attributes
99
+ begin with those the characters following fb, the first will be selected. Do not
100
+ add any spaces or underscores after "fb".
101
+ test_files:
102
+ - test/lib/find_by_shortcut/version_test.rb
103
+ - test/test_helper.rb