Skip to content

Commit

Permalink
Merge pull request #144 from syguer/add_finder
Browse files Browse the repository at this point in the history
Implement .find_by!
  • Loading branch information
syguer committed Apr 4, 2017
2 parents 1f2dd84 + 424849f commit 59ba7a5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2017-03-24 (v1.5.0)
- add support for `.find_by!`(@syguer)

2015-09-13 (v1.4.1)
- fix bug where `#attributes` didn't contain default values #107
- add support for `.find_by` and `#_read_attribute`. Thanks, @andrewfader!
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,17 @@ If you prefer to store your data in YAML, see below.

ActiveHash gives you ActiveRecord-esque methods like:
```ruby
Country.all # => returns all Country objects
Country.count # => returns the length of the .data array
Country.first # => returns the first country object
Country.last # => returns the last country object
Country.find 1 # => returns the first country object with that id
Country.find [1,2] # => returns all Country objects with ids in the array
Country.find :all # => same as .all
Country.find :all, args # => the second argument is totally ignored, but allows it to play nicely with AR
Country.find_by_id 1 # => find the first object that matches the id
Country.all # => returns all Country objects
Country.count # => returns the length of the .data array
Country.first # => returns the first country object
Country.last # => returns the last country object
Country.find 1 # => returns the first country object with that id
Country.find [1,2] # => returns all Country objects with ids in the array
Country.find :all # => same as .all
Country.find :all, args # => the second argument is totally ignored, but allows it to play nicely with AR
Country.find_by_id 1 # => find the first object that matches the id
Country.find_by(name: 'US') # => returns the first country object with specified argument
Country.find_by!(name: 'US') # => same as find_by, but raise exception when not found
```
It also gives you a few dynamic finder methods. For example, if you defined :name as a field, you'd get:
```ruby
Expand Down
1 change: 1 addition & 0 deletions active_hash.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
"Matthew O'Riordan",
"Brett Richardson",
"Rachel Heaton",
"Keisuke Izumiya"
]
s.email = %q{jeff@zilkey.com}
s.summary = %q{An ActiveRecord-like model that uses a hash or file as a datasource}
Expand Down
4 changes: 4 additions & 0 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def find_by(options)
where(options).first
end

def find_by!(options)
find_by(options) || (raise RecordNotFound.new("Couldn't find #{name}"))
end

def match_options?(record, options)
options.all? do |col, match|
if match.kind_of?(Array)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_hash/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveHash
module Gem
VERSION = "1.4.1"
VERSION = "1.5.0"
end
end
22 changes: 22 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ class Region < ActiveHash::Base
end
end

describe ".find_by!" do
before do
Country.field :name
Country.field :language
Country.data = [
{:id => 1, :name => "US", :language => 'English'}
]
end

subject { Country.find_by!(name: word) }

context 'when data exists' do
let(:word) { 'US' }
it { expect(subject.id).to eq 1 }
end

context 'when data not found' do
let(:word) { 'UK' }
it { expect{ subject }.to raise_error ActiveHash::RecordNotFound }
end
end

describe ".count" do
before do
Country.data = [
Expand Down

0 comments on commit 59ba7a5

Please sign in to comment.