dilute 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -2
- data/lib/dilute/modelize.rb +26 -11
- data/lib/dilute/version.rb +1 -1
- data/spec/modelize_integration_spec.rb +38 -5
- data/spec/spec_helper.rb +21 -7
- metadata +5 -5
data/README.md
CHANGED
@@ -34,8 +34,27 @@ With a class set up you can:
|
|
34
34
|
note = Note.new({data: "whatever"})
|
35
35
|
note.save # Stored to ElasticSearch
|
36
36
|
Note.find(note.id) # fetches that stored record
|
37
|
-
Note.all #
|
38
|
-
Note.all
|
37
|
+
Note.all # returns a query object, use an enumerable method to get actual records
|
38
|
+
Note.all.each { |n| n.data }
|
39
|
+
# Search with "match"
|
40
|
+
Note.all.match(data: "whatever").each {|n| ... }
|
41
|
+
|
42
|
+
ActiveModel stuff: You can do callbacks on save and use ActiveModel validations.
|
43
|
+
|
44
|
+
class Note
|
45
|
+
include Dilute::Modelize
|
46
|
+
|
47
|
+
validates_presense_of :data
|
48
|
+
before_save :hi
|
49
|
+
|
50
|
+
def hi
|
51
|
+
puts "Hi, you're about to save a note!"
|
52
|
+
end
|
53
|
+
|
54
|
+
define_type do
|
55
|
+
attribute(:data).not_indexed
|
56
|
+
end
|
57
|
+
end
|
39
58
|
|
40
59
|
## Contributing
|
41
60
|
|
data/lib/dilute/modelize.rb
CHANGED
@@ -1,16 +1,27 @@
|
|
1
1
|
require "active_support/concern"
|
2
2
|
require "active_model/naming"
|
3
|
-
require
|
3
|
+
require "active_support/core_ext/string"
|
4
|
+
require "active_model/callbacks"
|
5
|
+
require "active_model/validations"
|
6
|
+
require "active_model/translation"
|
7
|
+
|
4
8
|
|
5
9
|
module Dilute::Modelize
|
6
10
|
extend ActiveSupport::Concern
|
7
11
|
|
8
12
|
included do
|
9
13
|
attr_reader :attributes
|
10
|
-
extend
|
14
|
+
extend ActiveModel::Naming
|
15
|
+
extend ActiveModel::Callbacks
|
16
|
+
include ActiveModel::Validations
|
17
|
+
define_model_callbacks :save
|
11
18
|
end
|
12
19
|
|
13
20
|
module ClassMethods
|
21
|
+
def refresh
|
22
|
+
type.server.refresh
|
23
|
+
end
|
24
|
+
|
14
25
|
def name
|
15
26
|
super || "DefaultDilutedModel"
|
16
27
|
end
|
@@ -72,15 +83,19 @@ module Dilute::Modelize
|
|
72
83
|
end
|
73
84
|
|
74
85
|
def save
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
86
|
+
run_callbacks :save do
|
87
|
+
if valid?
|
88
|
+
if id
|
89
|
+
type.put(id, attributes_without_elasticsearch_vars)
|
90
|
+
else
|
91
|
+
results = type.post(attributes_without_elasticsearch_vars)
|
92
|
+
if results["ok"] == true
|
93
|
+
attributes.merge!(results)
|
94
|
+
else
|
95
|
+
puts "Something went wrong!"
|
96
|
+
puts "attributes: #{attributes}"
|
97
|
+
end
|
98
|
+
end
|
84
99
|
end
|
85
100
|
end
|
86
101
|
end
|
data/lib/dilute/version.rb
CHANGED
@@ -7,6 +7,9 @@ describe Dilute::Modelize do
|
|
7
7
|
Class.new do
|
8
8
|
include Dilute::Modelize
|
9
9
|
|
10
|
+
before_save :a_callback
|
11
|
+
def a_callback ; true ; end
|
12
|
+
|
10
13
|
define_type(type_name: :abstract) do
|
11
14
|
attribute(:data).not_indexed
|
12
15
|
attribute(:header).not_indexed
|
@@ -20,6 +23,29 @@ describe Dilute::Modelize do
|
|
20
23
|
let(:new_diluted) { dilute_class.new({data: test_data}) }
|
21
24
|
let(:saved) { new_diluted.save ; new_diluted }
|
22
25
|
|
26
|
+
context "validations" do
|
27
|
+
let(:dilute_class) do
|
28
|
+
Class.new do
|
29
|
+
include Dilute::Modelize
|
30
|
+
|
31
|
+
validates_presence_of :data
|
32
|
+
|
33
|
+
define_type(type_name: :abstract) do
|
34
|
+
attribute(:data).not_indexed
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should validate" do
|
40
|
+
expect(dilute_class.new).not_to be_valid
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not save invalid record" do
|
44
|
+
destroy_test_index!
|
45
|
+
expect(dilute_class.new.save).to be_false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
23
49
|
it "should have accessors for new object" do
|
24
50
|
expect(new_diluted.data).to eq(test_data)
|
25
51
|
end
|
@@ -36,15 +62,13 @@ describe Dilute::Modelize do
|
|
36
62
|
|
37
63
|
it "I have no idea what I'm doing." do
|
38
64
|
true
|
39
|
-
# That's why you see the sleep 1s and such.
|
40
65
|
end
|
41
66
|
|
42
67
|
context "searching" do
|
43
68
|
before { destroy_test_index! }
|
44
69
|
it "should find docs" do
|
45
|
-
destroy_test_index!
|
46
70
|
saved
|
47
|
-
|
71
|
+
dilute_class.refresh
|
48
72
|
all = dilute_class.all
|
49
73
|
expect(all.to_a).to have(1).item
|
50
74
|
expect(all.first.data).to eq(test_data)
|
@@ -53,7 +77,7 @@ describe Dilute::Modelize do
|
|
53
77
|
it "should find docs matching pattern" do
|
54
78
|
dilute_class.new({keys: %w(taga tagb tagc)}).save
|
55
79
|
dilute_class.new({keys: %w(tagb tagc)}).save
|
56
|
-
|
80
|
+
dilute_class.refresh
|
57
81
|
search = dilute_class.all.match(keys: "taga")
|
58
82
|
expect(search.to_a).to have(1).item
|
59
83
|
end
|
@@ -61,7 +85,7 @@ describe Dilute::Modelize do
|
|
61
85
|
it "should find docs matching very short word" do
|
62
86
|
dilute_class.new({keys: %w(a)}).save
|
63
87
|
dilute_class.new({keys: %w(b)}).save
|
64
|
-
|
88
|
+
dilute_class.refresh
|
65
89
|
search = dilute_class.all.match(keys: "a")
|
66
90
|
expect(search.to_a).to have(1).item
|
67
91
|
end
|
@@ -89,4 +113,13 @@ describe Dilute::Modelize do
|
|
89
113
|
end
|
90
114
|
end
|
91
115
|
|
116
|
+
context "callbacks" do
|
117
|
+
it "should not save when callback is false" do
|
118
|
+
destroy_test_index!
|
119
|
+
def new_diluted.a_callback ; false ; end
|
120
|
+
expect(new_diluted.save).to be_false
|
121
|
+
dilute_class.refresh
|
122
|
+
expect(dilute_class.all.first).to be_nil
|
123
|
+
end
|
124
|
+
end
|
92
125
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,12 +19,26 @@ end
|
|
19
19
|
require "dilute"
|
20
20
|
|
21
21
|
def destroy_test_index!
|
22
|
+
refresh_server!
|
23
|
+
all = all_records
|
24
|
+
test_index.bulk_delete(all) if all.any?
|
25
|
+
refresh_server!
|
26
|
+
end
|
27
|
+
|
28
|
+
def refresh_server!
|
29
|
+
test_server.refresh
|
30
|
+
end
|
31
|
+
|
32
|
+
def all_records
|
33
|
+
test_index.search(query: {match_all: {}}).results
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_server
|
22
37
|
require "stretcher"
|
23
|
-
server
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
38
|
+
@server ||= Stretcher::Server.new("http://localhost:9200", log_level: :info)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_index
|
42
|
+
return @index if @index
|
43
|
+
@index = test_server.index("default_dilute_index_name")
|
30
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dilute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: stretcher
|
@@ -163,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
163
163
|
version: '0'
|
164
164
|
segments:
|
165
165
|
- 0
|
166
|
-
hash:
|
166
|
+
hash: 2531018883342757898
|
167
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
168
|
none: false
|
169
169
|
requirements:
|
@@ -172,10 +172,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
172
|
version: '0'
|
173
173
|
segments:
|
174
174
|
- 0
|
175
|
-
hash:
|
175
|
+
hash: 2531018883342757898
|
176
176
|
requirements: []
|
177
177
|
rubyforge_project:
|
178
|
-
rubygems_version: 1.8.
|
178
|
+
rubygems_version: 1.8.25
|
179
179
|
signing_key:
|
180
180
|
specification_version: 3
|
181
181
|
summary: An ActiveRecord-ish thing for using ElasticSearch through Stretcher.
|