mongoid-casino 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/casino.rb +2 -2
- data/lib/casino/collection.rb +9 -3
- data/lib/casino/document.rb +27 -0
- data/lib/casino/store.rb +6 -26
- data/lib/casino/version.rb +1 -1
- data/spec/lib/casino/collection_spec.rb +8 -0
- data/spec/lib/casino/document_spec.rb +11 -0
- data/spec/lib/casino/store_spec.rb +13 -11
- metadata +5 -2
data/lib/casino.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'csv'
|
2
2
|
require 'mongoid'
|
3
3
|
|
4
|
-
libs = %w(collection dimension focus intersection lobby
|
5
|
-
query question store version)
|
4
|
+
libs = %w(collection dimension document focus intersection lobby
|
5
|
+
projection query question store version)
|
6
6
|
path = File.dirname(__FILE__)
|
7
7
|
|
8
8
|
libs.each do |lib|
|
data/lib/casino/collection.rb
CHANGED
@@ -44,7 +44,11 @@ module Casino
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def store
|
47
|
-
@store ||= Store.new
|
47
|
+
@store ||= Store.new document
|
48
|
+
end
|
49
|
+
|
50
|
+
def document
|
51
|
+
@document ||= Casino::Document.new(key, questions.map(&:name)).compose
|
48
52
|
end
|
49
53
|
|
50
54
|
def key
|
@@ -127,7 +131,9 @@ module Casino
|
|
127
131
|
end
|
128
132
|
|
129
133
|
def intersection_ids
|
130
|
-
intersections.map
|
134
|
+
intersections.map do |intersection|
|
135
|
+
intersection.selector.mongoize
|
136
|
+
end
|
131
137
|
end
|
132
138
|
|
133
139
|
def determine_results(given_intersections)
|
@@ -160,7 +166,7 @@ module Casino
|
|
160
166
|
def pending_intersections
|
161
167
|
ids = stored_results.map(&:id)
|
162
168
|
intersections.reject do |intersection|
|
163
|
-
ids.include? intersection.selector
|
169
|
+
ids.include? intersection.selector.mongoize
|
164
170
|
end
|
165
171
|
end
|
166
172
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Casino
|
2
|
+
class Document
|
3
|
+
|
4
|
+
def initialize(key, question_names)
|
5
|
+
@key = key
|
6
|
+
@question_names = Array question_names
|
7
|
+
end
|
8
|
+
|
9
|
+
def compose
|
10
|
+
klass = Class.new
|
11
|
+
klass.send :include, Mongoid::Document
|
12
|
+
klass.store_in collection: @key
|
13
|
+
assign_fields klass
|
14
|
+
klass
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def assign_fields klass
|
20
|
+
@question_names.each do |name|
|
21
|
+
name = String(name).parameterize.underscore.to_sym
|
22
|
+
klass.field name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/casino/store.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Casino
|
2
2
|
class Store
|
3
|
-
attr_accessor :collection_name
|
3
|
+
attr_accessor :collection_class, :collection_name
|
4
4
|
|
5
5
|
delegate :drop, :find, :indexes, :insert, to: :collection
|
6
6
|
|
@@ -9,8 +9,9 @@ module Casino
|
|
9
9
|
|
10
10
|
delegate :where, :in, to: :criteria
|
11
11
|
|
12
|
-
def initialize(
|
13
|
-
self.
|
12
|
+
def initialize(document_class)
|
13
|
+
self.collection_class = document_class
|
14
|
+
self.collection_name = document_class.collection_name
|
14
15
|
end
|
15
16
|
|
16
17
|
def collection
|
@@ -23,18 +24,14 @@ module Casino
|
|
23
24
|
|
24
25
|
def merge(*documents)
|
25
26
|
documents.each do |document|
|
26
|
-
document = mongoize
|
27
|
+
document = document.mongoize
|
27
28
|
criteria = find _id: document['_id']
|
28
29
|
criteria.upsert document
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
32
33
|
def criteria
|
33
|
-
@criteria ||=
|
34
|
-
end
|
35
|
-
|
36
|
-
def mongoize(hash)
|
37
|
-
hash.keys.map { |key| build_mongoized_hash(hash, key) }.reduce(&:merge)
|
34
|
+
@criteria ||= collection_class.scoped
|
38
35
|
end
|
39
36
|
|
40
37
|
private
|
@@ -43,22 +40,5 @@ module Casino
|
|
43
40
|
@session ||= Mongoid.default_session
|
44
41
|
end
|
45
42
|
|
46
|
-
def build_criteria
|
47
|
-
klass = Class.new
|
48
|
-
klass.send(:include, Mongoid::Document)
|
49
|
-
klass.store_in collection: collection_name
|
50
|
-
klass.scoped
|
51
|
-
end
|
52
|
-
|
53
|
-
def evolve(value)
|
54
|
-
value.class.respond_to?(:evolve) ? value.class.evolve(value) : value
|
55
|
-
end
|
56
|
-
|
57
|
-
def build_mongoized_hash(hash, key)
|
58
|
-
value = hash[key]
|
59
|
-
value = value.is_a?(Hash) ? mongoize(value) : value
|
60
|
-
{ key => evolve(value) }
|
61
|
-
end
|
62
|
-
|
63
43
|
end
|
64
44
|
end
|
data/lib/casino/version.rb
CHANGED
@@ -7,6 +7,7 @@ describe Casino::Collection do
|
|
7
7
|
it { Collection.must_respond_to :question }
|
8
8
|
it { Collection.must_respond_to :lobby }
|
9
9
|
it { Collection.must_respond_to :register }
|
10
|
+
it { Collection.new.must_respond_to :document }
|
10
11
|
it { Collection.new.must_respond_to :query }
|
11
12
|
it { Collection.new.must_respond_to :intersection }
|
12
13
|
it { Collection.new.must_respond_to :answer }
|
@@ -66,6 +67,13 @@ describe Casino::Collection do
|
|
66
67
|
|
67
68
|
end
|
68
69
|
|
70
|
+
describe '#document' do
|
71
|
+
let(:storage_key) { "key" }
|
72
|
+
let(:questions) { ['Question Name'] }
|
73
|
+
subject { Collection.new.document }
|
74
|
+
it { subject.ancestors.must_include Mongoid::Document }
|
75
|
+
end
|
76
|
+
|
69
77
|
describe '#query' do
|
70
78
|
subject { Collection.new.query('', '', '') }
|
71
79
|
it { subject.must_be_instance_of Casino::Query }
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Casino::Document do
|
4
|
+
describe '#compose' do
|
5
|
+
subject { Casino::Document.new('key', ['Question Name']).compose }
|
6
|
+
it { subject.new.must_respond_to :question_name }
|
7
|
+
it { subject.new.must_respond_to :question_name= }
|
8
|
+
it { subject.ancestors.must_include Mongoid::Document }
|
9
|
+
it { subject.collection_name.must_equal :key }
|
10
|
+
end
|
11
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
describe Casino::Store do
|
2
2
|
|
3
3
|
let(:key) { "collection_name" }
|
4
|
-
let(:
|
4
|
+
let(:document) { Casino::Document.new(key, ['value']).compose }
|
5
|
+
let(:store) { Casino::Store.new(document) }
|
5
6
|
|
6
7
|
subject { store }
|
7
8
|
|
8
9
|
it { subject.must_respond_to :collection_name }
|
10
|
+
it { subject.must_respond_to :collection_class }
|
9
11
|
|
10
12
|
it "establishes a connection to the default session" do
|
11
13
|
store.collection.database.session.must_equal Mongoid.default_session
|
@@ -35,23 +37,23 @@ describe Casino::Store do
|
|
35
37
|
{ 'value' => { 'signups' => 10850, 'uniques' => 9822 } }
|
36
38
|
end
|
37
39
|
|
38
|
-
let(:
|
39
|
-
let(:
|
40
|
+
let(:attributes) { womens_boots.merge(value_hash) }
|
41
|
+
let(:attributes_two) { mens_boots.merge(value_hash) }
|
40
42
|
|
41
|
-
it "adds new
|
42
|
-
store.merge(
|
43
|
-
store.first.must_equal
|
43
|
+
it "adds new document attributes to the collection" do
|
44
|
+
store.merge(attributes)
|
45
|
+
store.first.must_equal attributes.mongoize
|
44
46
|
end
|
45
47
|
|
46
|
-
it "updates documents" do
|
47
|
-
store.merge(
|
48
|
-
store.merge(
|
48
|
+
it "updates documents with new attributes" do
|
49
|
+
store.merge(attributes)
|
50
|
+
store.merge(attributes.merge(value: { signups: 2 }))
|
49
51
|
store.first['value']['signups'].must_equal 2
|
50
52
|
end
|
51
53
|
|
52
54
|
it "does not replace the wrong document" do
|
53
|
-
store.merge(
|
54
|
-
store.merge(
|
55
|
+
store.merge(attributes)
|
56
|
+
store.merge(attributes_two)
|
55
57
|
store.find.count.wont_equal 1
|
56
58
|
end
|
57
59
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-casino
|
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-07-
|
12
|
+
date: 2013-07-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- lib/casino.rb
|
142
142
|
- lib/casino/collection.rb
|
143
143
|
- lib/casino/dimension.rb
|
144
|
+
- lib/casino/document.rb
|
144
145
|
- lib/casino/focus.rb
|
145
146
|
- lib/casino/intersection.rb
|
146
147
|
- lib/casino/intersection/match/all.rb
|
@@ -165,6 +166,7 @@ files:
|
|
165
166
|
- spec/fixtures/models/note.rb
|
166
167
|
- spec/lib/casino/collection_spec.rb
|
167
168
|
- spec/lib/casino/dimension_spec.rb
|
169
|
+
- spec/lib/casino/document_spec.rb
|
168
170
|
- spec/lib/casino/focus_spec.rb
|
169
171
|
- spec/lib/casino/intersection/match/all_spec.rb
|
170
172
|
- spec/lib/casino/intersection/match/base_spec.rb
|
@@ -215,6 +217,7 @@ test_files:
|
|
215
217
|
- spec/fixtures/models/note.rb
|
216
218
|
- spec/lib/casino/collection_spec.rb
|
217
219
|
- spec/lib/casino/dimension_spec.rb
|
220
|
+
- spec/lib/casino/document_spec.rb
|
218
221
|
- spec/lib/casino/focus_spec.rb
|
219
222
|
- spec/lib/casino/intersection/match/all_spec.rb
|
220
223
|
- spec/lib/casino/intersection/match/base_spec.rb
|