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.
- data/lib/hash_query.rb +6 -3
- data/lib/hash_query/version.rb +1 -1
- data/test/test_hash_query.rb +31 -20
- metadata +1 -1
data/lib/hash_query.rb
CHANGED
@@ -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
|
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
|
data/lib/hash_query/version.rb
CHANGED
data/test/test_hash_query.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class TestHquery_value < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
-
|
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?(:
|
66
|
-
assert h.respond_to?(:
|
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.
|
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.
|
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.
|
79
|
-
assert_equal 'R', @movie.
|
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.
|
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.
|
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.
|
92
|
-
assert_equal 5, @movie.
|
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.
|
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.
|
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.
|
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.
|
109
|
-
assert_equal 2, @movie.
|
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.
|
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.
|
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.
|
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
|