activerecord-find_only 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dcb55a9dcdf663195d4f9afdfba53e0f72ede250d1278c6fa10ec91453405b05
4
+ data.tar.gz: a9c811cf6e122c1fb9394426c2fccd3dc0455b48e4a8f325cc7dc18bccef2c30
5
+ SHA512:
6
+ metadata.gz: 63ed293c02e7709f79a6f33da9f9ca00cb194de0658f8176c330db63eb1830cd6ad963fb266e3a9f09d5719cac5a2c4000962df20497c8985236c27f1626cbae
7
+ data.tar.gz: 24fe2b5aebc61a5fde354aab974f22f917a2531063841baa467a845946aa60c303b7f263352f10beee411524904a470507e5a1fbccd619ae68dcd6e4153dc726
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /bundle/
3
+ /.yardoc
4
+ /.byebug_history
5
+ /.rvmrc
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in activerecord-find_only.gemspec
6
+ gemspec
7
+
8
+ gem "byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-find_only (0.3.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.2.1)
11
+ activesupport (= 5.2.2.1)
12
+ activerecord (5.2.2.1)
13
+ activemodel (= 5.2.2.1)
14
+ activesupport (= 5.2.2.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (11.0.1)
23
+ concurrent-ruby (1.1.5)
24
+ diff-lcs (1.3)
25
+ i18n (1.6.0)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.11.3)
28
+ rake (10.5.0)
29
+ rspec (3.8.0)
30
+ rspec-core (~> 3.8.0)
31
+ rspec-expectations (~> 3.8.0)
32
+ rspec-mocks (~> 3.8.0)
33
+ rspec-core (3.8.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-expectations (3.8.2)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-mocks (3.8.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-support (3.8.0)
42
+ sqlite3 (1.3.13)
43
+ thread_safe (0.3.6)
44
+ tzinfo (1.2.5)
45
+ thread_safe (~> 0.1)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ activerecord-find_only!
52
+ bundler (~> 1.16)
53
+ byebug
54
+ rake (~> 10.0)
55
+ rspec (~> 3.0)
56
+ sqlite3 (~> 1.3.6)
57
+
58
+ BUNDLED WITH
59
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Joshua Flanagan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # ActiveRecord::FindOnly
2
+
3
+ Provides the `#find_only` and `#find_only!` methods for querying ActiveRecord objects.
4
+
5
+ `#find_only` - returns the only record that meets the criteria. If no records match
6
+ the criteria, `nil` is returned. Raises `TooManyRecords` if more than one
7
+ records matches the criteria.
8
+
9
+ `#find_only!` - returns the only record that meets the criteria. If no records match
10
+ the criteria, `RecordNotFound` is raised. Raises `TooManyRecords` if more than
11
+ one records matches the criteria.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'active_record-find_only'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install active_record-find_only
28
+
29
+ ## Usage
30
+
31
+ See the spec files for example usage.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_record/find_only/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-find_only"
8
+ spec.version = ActiveRecord::FindOnly::VERSION
9
+ spec.authors = ["Joshua Flanagan"]
10
+ spec.email = ["joshuaflanagan@gmail.com"]
11
+
12
+ spec.summary = %q{Query for a single item, and ensure it is the only one.}
13
+ spec.description = %q{Provides find_only and find_only! query methods}
14
+ spec.homepage = "https://github.com/joshuaflanagan/active_record-find_only"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activerecord"
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "sqlite3", "~> 1.3.6" # dictated by activerecord-5.2.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:12
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_record/find_only"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --standalone
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module FindOnly
3
+ VERSION = "0.3.0"
4
+ end
5
+ end
@@ -0,0 +1,57 @@
1
+ unless defined?(Arel)
2
+ raise 'activerecord-find_only has to be required after ActiveRecord/Arel'
3
+ end
4
+
5
+ unless defined?(ActiveRecord)
6
+ raise 'activerecord-find_only has to be required after ActiveRecord'
7
+ end
8
+
9
+ require "active_record"
10
+ require "active_record/find_only/version"
11
+
12
+ module ActiveRecord
13
+ module FindOnly
14
+ class TooManyRecords < ActiveRecord::ActiveRecordError
15
+ end
16
+
17
+ # Gives the only record that matches the criteria.
18
+ # If no record matches the criteria, nil is returned.
19
+ # If more than one record matches the criteria, TooManyRecords is raised.
20
+ def find_only
21
+ found_records = take(2)
22
+ if found_records.length > 1
23
+ raise TooManyRecords
24
+ end
25
+ found_records[0]
26
+ end
27
+
28
+ # Gives the only record that matches the criteria.
29
+ # If no record matches the criteria, RecordNotFound is raised.
30
+ # If more than one record matches the criteria, TooManyRecords is raised.
31
+ def find_only!
32
+ find_only || raise_record_not_found_exception!
33
+ end
34
+ end
35
+
36
+ Relation.class_eval do
37
+ include FindOnly
38
+ end
39
+
40
+ Querying.class_eval do
41
+ delegate :find_only, :find_only!, to: :all
42
+ end
43
+
44
+ Associations::CollectionProxy.class_eval do
45
+ def find_only
46
+ if loaded?
47
+ if records.length > 1
48
+ raise ActiveRecord::FindOnly::TooManyRecords
49
+ end
50
+ records[0]
51
+ else
52
+ super
53
+ end
54
+ end
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-find_only
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Flanagan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Provides find_only and find_only! query methods
84
+ email:
85
+ - joshuaflanagan@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - activerecord-find_only.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/active_record/find_only.rb
102
+ - lib/active_record/find_only/version.rb
103
+ homepage: https://github.com/joshuaflanagan/active_record-find_only
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.7.4
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Query for a single item, and ensure it is the only one.
127
+ test_files: []