hash-query 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,11 +40,16 @@ module HashQuery
40
40
  # entity.query('a:string')
41
41
  #
42
42
  # Returns a value or collection of values that are found.
43
- def query(selectors)
43
+ def query_values(selectors)
44
44
  @_query ||= HashQuery::Query.new(self)
45
45
  @_query.query(selectors)
46
46
  end
47
47
 
48
+ def query_value(selectors)
49
+ values = query_values(selectors)
50
+ values && values.first
51
+ end
52
+
48
53
  end
49
54
 
50
55
  class Query
@@ -59,8 +64,6 @@ module HashQuery
59
64
 
60
65
  if found.empty?
61
66
  nil
62
- elsif found.size == 1
63
- found.first
64
67
  else
65
68
  found
66
69
  end
@@ -1,3 +1,3 @@
1
1
  module HashQuery
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -1,8 +1,8 @@
1
1
  require 'helper'
2
2
 
3
- class TestHquery < MiniTest::Unit::TestCase
3
+ class TestHquery_value < MiniTest::Unit::TestCase
4
4
 
5
- def setup
5
+ def setup
6
6
  @movie = {
7
7
  'title' => 'Goodfellas',
8
8
  'rating' => 'R',
@@ -62,65 +62,76 @@ class TestHquery < MiniTest::Unit::TestCase
62
62
 
63
63
  def test_included_in_hash
64
64
  h = {}
65
- assert h.respond_to?(:query)
66
- assert h.respond_to?(:query)
65
+ assert h.respond_to?(:query_value)
66
+ assert h.respond_to?(:query_values)
67
67
  end
68
68
 
69
69
  def test_finds_nothing
70
- assert_nil @hquery.query('z')
70
+ assert_nil @hquery.query_value('z')
71
71
  end
72
72
 
73
73
  def test_finding_deep_array_value
74
- assert_equal [8,9,10,11], @hquery.query('i')
74
+ assert_equal [8,9,10,11], @hquery.query_value('i')
75
75
  end
76
76
 
77
77
  def test_can_be_querried_shallow
78
- assert_equal 'Goodfellas', @movie.query('title')
79
- assert_equal 'R', @movie.query('rating')
78
+ assert_equal 'Goodfellas', @movie.query_value('title')
79
+ assert_equal 'R', @movie.query_value('rating')
80
80
  end
81
81
 
82
82
  def test_can_be_querried_one_level_deep
83
- assert_equal 'Martin Scorsese', @movie.query('director name')
83
+ assert_equal 'Martin Scorsese', @movie.query_value('director name')
84
84
  end
85
85
 
86
86
  def test_can_be_querried_shallow_for_a_deep_value
87
- assert_equal 23, @movie.query('award_count')
87
+ assert_equal 23, @movie.query_value('award_count')
88
88
  end
89
89
 
90
90
  def test_can_be_querried_two_levels_deep
91
- assert_equal 2, @movie.query('actors awards academy')
92
- assert_equal 5, @movie.query('actors awards emmies')
91
+ assert_equal 2, @movie.query_value('actors awards academy')
92
+ assert_equal 5, @movie.query_value('actors awards emmies')
93
93
  end
94
94
 
95
95
  def test_can_be_querried_to_find_an_array
96
- assert_equal ['a', 'b', 'c'], @movie.query('encodings')
96
+ assert_equal ['a', 'b', 'c'], @movie.query_value('encodings')
97
97
  end
98
98
 
99
99
  def test_can_be_querried_for_multiple_values
100
- assert_equal ['Robert De Niro', 'Ray Liotta'], @movie.query('actors name')
100
+ assert_equal ['Robert De Niro', 'Ray Liotta'], @movie.query_values('actors name')
101
+ end
102
+
103
+ def test_can_be_querried_for_one_value_when_multiple_exist
104
+ assert_equal 'Robert De Niro', @movie.query_value('actors name')
101
105
  end
102
106
 
103
107
  def test_can_query_for_type
104
- assert_equal 'Goodfellas', @movie.query('title:string')
108
+ assert_equal 'Goodfellas', @movie.query_value('title:string')
105
109
  end
106
110
 
107
111
  def test_can_deep_query_for_type
108
- assert_equal 2, @movie.query('academy:fixnum')
109
- assert_equal 2, @movie.query('actors awards academy:fixnum')
112
+ assert_equal 2, @movie.query_value('academy:fixnum')
113
+ assert_equal 2, @movie.query_value('actors awards academy:fixnum')
110
114
  end
111
115
 
112
116
  def test_will_return_nil_if_type_is_not_matched
113
- assert_equal nil, @movie.query('title:fixnum')
117
+ assert_equal nil, @movie.query_value('title:fixnum')
114
118
  end
115
119
 
116
120
  def test_can_query_for_complex_type
117
- assert_equal ['a', 'b', 'c'], @movie.query('encodings:array')
121
+ assert_equal ['a', 'b', 'c'], @movie.query_value('encodings:array')
118
122
  end
119
123
 
120
124
  def test_raises_exception_if_type_not_found
121
125
  assert_raises NameError do
122
- @movie.query('title:bogus')
126
+ @movie.query_value('title:bogus')
123
127
  end
124
128
  end
125
129
 
130
+ def test_changed_hash_after_initialization
131
+ hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
132
+ assert_equal 2, hash.query_value('b')
133
+ hash['b'] = 3
134
+ assert_equal 3, hash.query_value('b')
135
+ end
136
+
126
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-query
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: