Skip to content

Commit

Permalink
Added raise option to find_first.
Browse files Browse the repository at this point in the history
Also added find alias.
  • Loading branch information
hopsoft committed Mar 2, 2015
1 parent b3eb50b commit 9da01e4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Lines of Code](http://img.shields.io/badge/lines_of_code-38-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
[![Lines of Code](http://img.shields.io/badge/lines_of_code-46-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
[![Code Status](http://img.shields.io/codeclimate/github/hopsoft/queryable_hash.svg?style=flat)](https://codeclimate.com/github/hopsoft/queryable_hash)
[![Dependency Status](http://img.shields.io/gemnasium/hopsoft/queryable_hash.svg?style=flat)](https://gemnasium.com/hopsoft/queryable_hash)
[![Build Status](http://img.shields.io/travis/hopsoft/queryable_hash.svg?style=flat)](https://travis-ci.org/hopsoft/queryable_hash)
Expand Down
1 change: 1 addition & 0 deletions lib/queryable_hash.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "queryable_hash/version"
require_relative "queryable_hash/not_found_error"
require_relative "queryable_hash/wrapper"

module QueryableHash
Expand Down
4 changes: 4 additions & 0 deletions lib/queryable_hash/not_found_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module QueryableHash
class NotFoundError < StandardError
end
end
2 changes: 1 addition & 1 deletion lib/queryable_hash/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module QueryableHash
VERSION = "0.0.1"
VERSION = "0.0.2"
end
9 changes: 7 additions & 2 deletions lib/queryable_hash/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ def find_all(*queries, nil_value: nil)
end
end

def find_first(*queries, nil_value: nil)
def find_first(*queries, nil_value: nil, raise_when_nil: false)
first = find_all(*queries, nil_value: nil_value).find do |result|
result != nil_value
end
first || nil_value
first ||= nil_value

raise NotFoundError if raise_when_nil && first == nil_value
first
end

alias_method :find, :find_first

def to_hash
@original_hash
end
Expand Down
10 changes: 10 additions & 0 deletions test/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class QueryableHashTest < PryTest::Test
assert term == "Standard Generalized Markup Language"
end

test "find_first with raise" do
query = "nate.this.key.does.not.exist"
begin
@queryable.find_first(query, raise_when_nil: true)
rescue NotFoundError => e
end

assert e
end

test "to_hash" do
assert @queryable.to_hash == @data
end
Expand Down

0 comments on commit 9da01e4

Please sign in to comment.