Skip to content

Commit

Permalink
Added constuctor options.
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Mar 2, 2015
1 parent 9da01e4 commit cdec950
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/queryable_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "queryable_hash/wrapper"

module QueryableHash
def self.wrap(hash)
Wrapper.new hash
def self.wrap(hash, **args)
Wrapper.new hash, **args
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.2"
VERSION = "0.0.3"
end
10 changes: 8 additions & 2 deletions lib/queryable_hash/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

module QueryableHash
class Wrapper < SimpleDelegator
attr_reader :nil_value, :raise_when_nil

def initialize(hash)
def initialize(hash, nil_value: nil, raise_when_nil: false)
@original_hash = hash
@nil_value = nil_value
@raise_when_nil = raise_when_nil
super hash
end

Expand All @@ -19,7 +22,10 @@ def find_all(*queries, nil_value: nil)
end
end

def find_first(*queries, nil_value: nil, raise_when_nil: false)
def find_first(*queries, nil_value: nil, raise_when_nil: nil)
nil_value = @nil_value if nil_value.nil?
raise_when_nil = @raise_when_nil if raise_when_nil.nil?

first = find_all(*queries, nil_value: nil_value).find do |result|
result != nil_value
end
Expand Down
20 changes: 18 additions & 2 deletions test/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ class QueryableHashTest < PryTest::Test
assert @queryable.find_first(query).nil?
end

test "find_first with missing key and nil value" do
test "find_first with missing key and nil_value" do
query = "nate.this.key.does.not.exist"
assert @queryable.find_first(query, nil_value: "missing") == "missing"
end

test "find_first with missing key and nil_value set by instance" do
query = "nate.this.key.does.not.exist"
queryable = QueryableHash.wrap(@data, nil_value: "missing")
assert queryable.find_first(query) == "missing"
end

test "find_first with multiple queries some invalid" do
term = @queryable.find_first(
"glossary.div.list.entry.term",
Expand All @@ -79,7 +85,7 @@ class QueryableHashTest < PryTest::Test
assert term == "Standard Generalized Markup Language"
end

test "find_first with raise" do
test "find_first with raise_when_nil" do
query = "nate.this.key.does.not.exist"
begin
@queryable.find_first(query, raise_when_nil: true)
Expand All @@ -89,6 +95,16 @@ class QueryableHashTest < PryTest::Test
assert e
end

test "find_first with raise_when_nil set by instance" do
query = "nate.this.key.does.not.exist"
begin
queryable = QueryableHash.wrap(@data, raise_when_nil: true)
queryable.find_first(query)
rescue NotFoundError => e
end
assert e
end

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

0 comments on commit cdec950

Please sign in to comment.