waistband 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +1 -5
- data/lib/waistband.rb +8 -7
- data/lib/waistband/index.rb +15 -2
- data/lib/waistband/stringified_array.rb +15 -0
- data/lib/waistband/stringified_hash.rb +31 -0
- data/lib/waistband/version.rb +1 -1
- data/spec/lib/index_spec.rb +14 -9
- data/spec/lib/model_spec.rb +1 -1
- data/spec/lib/stringified_array_spec.rb +9 -0
- data/spec/lib/stringified_hash_spec.rb +27 -0
- metadata +16 -25
- data/lib/waistband/stringify_all.rb +0 -39
- data/spec/lib/stringify_all_spec.rb +0 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0bb1048847e31a803f414112221ee12f51ec7df9
|
4
|
+
data.tar.gz: 2ba18a9f7eb6ab073ce1a2c0f20b06fa4006b90f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6e394da1f1941fcba5487e59debe69f063c4eb7893e33fb9bbfcf1c8fdc9017baceeaba2ada6b2ecdfb04fb2ffa943905439aea31855c4f7a4b7a2bd06094be
|
7
|
+
data.tar.gz: f45002b13606503854402badae133138e67cc351f6eae142b0e49c130023e71253bc0bed174ea65e79a30c8d97eb4bb8d0fd4070d957eec1131d1c5c0ad68ab7
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ development:
|
|
70
70
|
## List of config settings:
|
71
71
|
|
72
72
|
* `name`: name of the index. You can (and probably should) have a different name for the index for your test environment.
|
73
|
-
* `stringify`: determines wether whatever is stored into the index is going to be converted to a string before storage. Usually false unless you need it to be true for specific cases, like if for some `key => value` pairs the value is of different types some times.
|
73
|
+
* `stringify`: determines wether whatever is stored into the index is going to be converted to a string before storage. Usually false unless you need it to be true for specific cases, like if for some `key => value` pairs the value is of different types some times.
|
74
74
|
* `settings`: settings for the Elastic Search index. Refer to the ["admin indices update settings"](http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/) document for more info.
|
75
75
|
* `mappings`: the index mappings. More often than not you'll want to include all of the document attribute, so you'll do something like in the example above. For more info, refer to the [mapping reference]("http://www.elasticsearch.org/guide/reference/mapping/").
|
76
76
|
|
@@ -163,10 +163,6 @@ query.results
|
|
163
163
|
|
164
164
|
For paginating the results, you can use the `#paginated_results` method, which requires the [Kaminari](https://github.com/amatsuda/kaminari), gem. If you use another gem, you can just override the method, etc.
|
165
165
|
|
166
|
-
### Stringify All
|
167
|
-
|
168
|
-
For convenience, Waistband provides two mixins useful to stringify Arrays and Hashes recursively in the `Waistband::StringifyAll::Array` and `Waistband::StringifyAll::Hash` modules. Feel free to include them into your Array or Hash classes as necessary in an initializer if needed.
|
169
|
-
|
170
166
|
### Model
|
171
167
|
|
172
168
|
The gem offers a `Waistband::Model` that you can use to store model type data into the Elastic Search index. You just inherit from the class and define the following class methods:
|
data/lib/waistband.rb
CHANGED
@@ -2,13 +2,14 @@ require "waistband/version"
|
|
2
2
|
|
3
3
|
module Waistband
|
4
4
|
|
5
|
-
autoload :Configuration,
|
6
|
-
autoload :
|
7
|
-
autoload :
|
8
|
-
autoload :
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :
|
5
|
+
autoload :Configuration, "waistband/configuration"
|
6
|
+
autoload :StringifiedArray, "waistband/stringified_array"
|
7
|
+
autoload :StringifiedHash, "waistband/stringified_hash"
|
8
|
+
autoload :QueryResult, "waistband/query_result"
|
9
|
+
autoload :Query, "waistband/query"
|
10
|
+
autoload :Index, "waistband/index"
|
11
|
+
autoload :QuickError, "waistband/quick_error"
|
12
|
+
autoload :Model, "waistband/model"
|
12
13
|
|
13
14
|
class << self
|
14
15
|
|
data/lib/waistband/index.rb
CHANGED
@@ -41,9 +41,22 @@ module Waistband
|
|
41
41
|
|
42
42
|
def store!(key, data)
|
43
43
|
# map everything to strings
|
44
|
-
|
44
|
+
if @stringify
|
45
|
+
original_data = data
|
45
46
|
|
46
|
-
|
47
|
+
if data.is_a?(Array)
|
48
|
+
data = Waistband::StringifiedArray.new(data)
|
49
|
+
elsif data.is_a?(Hash)
|
50
|
+
data = Waistband::StringifiedHash.new_from(data)
|
51
|
+
end
|
52
|
+
|
53
|
+
data = data.stringify_all if data.respond_to?(:stringify_all)
|
54
|
+
end
|
55
|
+
|
56
|
+
result = RestClient.put(url_for_key(key), data.to_json)
|
57
|
+
data = original_data if @stringify
|
58
|
+
|
59
|
+
result
|
47
60
|
end
|
48
61
|
|
49
62
|
def delete!(key)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Waistband
|
2
|
+
class StringifiedHash < Hash
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def new_from(original)
|
7
|
+
copy = new
|
8
|
+
original.each do |k, v|
|
9
|
+
copy[k] = v
|
10
|
+
end
|
11
|
+
copy
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def stringify_all
|
17
|
+
stringified = {}
|
18
|
+
|
19
|
+
each do |key, val|
|
20
|
+
if val.respond_to?(:to_s)
|
21
|
+
stringified[key] = val.to_s
|
22
|
+
else
|
23
|
+
stringified[key] = val
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
stringified
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/waistband/version.rb
CHANGED
data/spec/lib/index_spec.rb
CHANGED
@@ -2,8 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Waistband::Index do
|
4
4
|
|
5
|
-
let(:index)
|
6
|
-
let(:
|
5
|
+
let(:index) { Waistband::Index.new('events') }
|
6
|
+
let(:index2) { Waistband::Index.new('search') }
|
7
|
+
let(:attrs) { {'ok' => {'yeah' => true}} }
|
7
8
|
|
8
9
|
it "initializes values" do
|
9
10
|
index.instance_variable_get('@index_name').should eql 'events_test'
|
@@ -45,17 +46,23 @@ describe Waistband::Index do
|
|
45
46
|
|
46
47
|
describe "storing" do
|
47
48
|
|
48
|
-
before { index.store!('__test_write', attrs) }
|
49
|
-
|
50
49
|
it "stores data" do
|
51
|
-
index.
|
50
|
+
index.store!('__test_write', {'ok' => 'yeah'})
|
51
|
+
index.read('__test_write').should eql({'ok' => 'yeah'})
|
52
|
+
end
|
53
|
+
|
54
|
+
it "data is stringified" do
|
55
|
+
index.store!('__test_write', attrs)
|
56
|
+
index.read('__test_write').should eql({"ok"=>"{\"yeah\"=>true}"})
|
52
57
|
end
|
53
58
|
|
54
|
-
it "data is indirectly accessible" do
|
55
|
-
|
59
|
+
it "data is indirectly accessible when not stringified" do
|
60
|
+
index2.store!('__test_not_string', attrs)
|
61
|
+
index2.read('__test_not_string')[:ok][:yeah].should eql true
|
56
62
|
end
|
57
63
|
|
58
64
|
it "deletes data" do
|
65
|
+
index.store!('__test_write', attrs)
|
59
66
|
index.delete!('__test_write')
|
60
67
|
index.read('__test_write').should be_nil
|
61
68
|
end
|
@@ -65,8 +72,6 @@ describe Waistband::Index do
|
|
65
72
|
end
|
66
73
|
|
67
74
|
it "doesn't mix data between two indexes" do
|
68
|
-
index2 = Waistband::Index.new('search')
|
69
|
-
|
70
75
|
index.store!('__test_write', {'data' => 'index_1'})
|
71
76
|
index2.store!('__test_write', {'data' => 'index_2'})
|
72
77
|
|
data/spec/lib/model_spec.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Waistband::StringifiedHash do
|
4
|
+
|
5
|
+
let(:hash) { Waistband::StringifiedHash.new }
|
6
|
+
|
7
|
+
it "stringifies everything in a hash" do
|
8
|
+
hash['name'] = :peter
|
9
|
+
hash['description'] = {'full' => 'ok'}
|
10
|
+
hash.stringify_all.should eql({'name' => 'peter', 'description' => "{\"full\"=>\"ok\"}"})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "recurses" do
|
14
|
+
hash['name'] = :peter
|
15
|
+
hash['description'] = [1, 2, 3]
|
16
|
+
hash.stringify_all.should eql({'name' => 'peter', 'description' => "[1, 2, 3]"})
|
17
|
+
end
|
18
|
+
|
19
|
+
it "creates a stringified array from a hash" do
|
20
|
+
copy = Waistband::StringifiedHash.new_from({'name' => 'peter', 'description' => [1, 2, 3]})
|
21
|
+
copy.should be_a Waistband::StringifiedHash
|
22
|
+
copy['name'].should eql 'peter'
|
23
|
+
copy['description'].should eql [1, 2, 3]
|
24
|
+
copy.stringify_all.should eql({'name' => 'peter', 'description' => "[1, 2, 3]"})
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waistband
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- David Jairala
|
@@ -14,23 +13,20 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 3.0.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 3.0.0
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rest-client
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: json
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: bundler
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ~>
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ~>
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,17 +69,15 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rake
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - '>='
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: Ruby interface to Elastic Search
|
@@ -113,7 +102,8 @@ files:
|
|
113
102
|
- lib/waistband/query.rb
|
114
103
|
- lib/waistband/query_result.rb
|
115
104
|
- lib/waistband/quick_error.rb
|
116
|
-
- lib/waistband/
|
105
|
+
- lib/waistband/stringified_array.rb
|
106
|
+
- lib/waistband/stringified_hash.rb
|
117
107
|
- lib/waistband/version.rb
|
118
108
|
- spec/config/waistband/waistband.yml
|
119
109
|
- spec/config/waistband/waistband_events.yml
|
@@ -123,34 +113,34 @@ files:
|
|
123
113
|
- spec/lib/model_spec.rb
|
124
114
|
- spec/lib/query_result_spec.rb
|
125
115
|
- spec/lib/query_spec.rb
|
126
|
-
- spec/lib/
|
116
|
+
- spec/lib/stringified_array_spec.rb
|
117
|
+
- spec/lib/stringified_hash_spec.rb
|
127
118
|
- spec/spec_helper.rb
|
128
119
|
- spec/support/index_helper.rb
|
129
120
|
- waistband.gemspec
|
130
121
|
homepage: https://github.com/taskrabbit/waistband
|
131
122
|
licenses:
|
132
123
|
- MIT
|
124
|
+
metadata: {}
|
133
125
|
post_install_message:
|
134
126
|
rdoc_options: []
|
135
127
|
require_paths:
|
136
128
|
- lib
|
137
129
|
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
-
none: false
|
139
130
|
requirements:
|
140
|
-
- -
|
131
|
+
- - '>='
|
141
132
|
- !ruby/object:Gem::Version
|
142
133
|
version: 1.9.3
|
143
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
-
none: false
|
145
135
|
requirements:
|
146
|
-
- -
|
136
|
+
- - '>='
|
147
137
|
- !ruby/object:Gem::Version
|
148
138
|
version: '0'
|
149
139
|
requirements: []
|
150
140
|
rubyforge_project:
|
151
|
-
rubygems_version:
|
141
|
+
rubygems_version: 2.0.6
|
152
142
|
signing_key:
|
153
|
-
specification_version:
|
143
|
+
specification_version: 4
|
154
144
|
summary: Elastic Search all the things!
|
155
145
|
test_files:
|
156
146
|
- spec/config/waistband/waistband.yml
|
@@ -161,6 +151,7 @@ test_files:
|
|
161
151
|
- spec/lib/model_spec.rb
|
162
152
|
- spec/lib/query_result_spec.rb
|
163
153
|
- spec/lib/query_spec.rb
|
164
|
-
- spec/lib/
|
154
|
+
- spec/lib/stringified_array_spec.rb
|
155
|
+
- spec/lib/stringified_hash_spec.rb
|
165
156
|
- spec/spec_helper.rb
|
166
157
|
- spec/support/index_helper.rb
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Waistband
|
2
|
-
|
3
|
-
module StringifyAll
|
4
|
-
|
5
|
-
module Array
|
6
|
-
|
7
|
-
def stringify_all
|
8
|
-
self.map do |val|
|
9
|
-
if val.respond_to?(:to_s)
|
10
|
-
val.to_s
|
11
|
-
else
|
12
|
-
val
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
module Hash
|
20
|
-
|
21
|
-
def stringify_all
|
22
|
-
stringified = {}
|
23
|
-
|
24
|
-
each do |key, val|
|
25
|
-
if val.respond_to?(:to_s)
|
26
|
-
stringified[key] = val.to_s
|
27
|
-
else
|
28
|
-
stringified[key] = val
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
stringified
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Waistband::StringifyAll do
|
4
|
-
|
5
|
-
before do
|
6
|
-
Array.send(:include, Waistband::StringifyAll::Array)
|
7
|
-
Hash.send(:include, Waistband::StringifyAll::Hash)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "stringifies everything in an array" do
|
11
|
-
[1, 2, 3].stringify_all.should eql %w(1 2 3)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "stringifies everything in a hash" do
|
15
|
-
{'name' => :peter, 'description' => {'full' => 'ok'}}.stringify_all.should eql({'name' => 'peter', 'description' => "{\"full\"=>\"ok\"}"})
|
16
|
-
end
|
17
|
-
|
18
|
-
it "recurses" do
|
19
|
-
{'name' => :peter, 'description' => [1, 2, 3]}.stringify_all.should eql({'name' => 'peter', 'description' => "[1, 2, 3]"})
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|