cross_origin 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/cross_origin.rb +11 -3
- data/lib/cross_origin/collection.rb +14 -5
- data/lib/cross_origin/criteria.rb +26 -0
- data/lib/cross_origin/document.rb +25 -10
- data/lib/cross_origin/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4d011cf5d7bd463e95f1b603fe5856edc5ba01c4
|
|
4
|
+
data.tar.gz: 1ba1cc77c7f5b27ff53e53761f4bb59eb8b79fec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 46e294420575e038fcc83069efc030b58c19a6da6205288d9b4451f7a511b8c70eb7a82c0b2ccedf4aabcfa2773c8008508d6d189662aeb38a930043f424f438
|
|
7
|
+
data.tar.gz: 09f061e55f36454efaaca5c20b42f33c980e26e8dcd923320f6d26dd26ebe90d905d78cc2876c61675ff99496667006aed8d985b65e99435f2e9edf8f04a6b2b
|
data/README.md
CHANGED
|
@@ -26,7 +26,7 @@ Configurate your origins
|
|
|
26
26
|
CrossOrigin.config :test
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
and include the module `CrossOrigin::Document` in your Mongoid class
|
|
29
|
+
and include the module `CrossOrigin::Document` in your Mongoid class. Then use the `cross` method on records or criterias to send records to an origin collection.
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
## Development
|
data/lib/cross_origin.rb
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
require 'cross_origin/version'
|
|
2
2
|
require 'cross_origin/collection'
|
|
3
3
|
require 'cross_origin/document'
|
|
4
|
+
require 'cross_origin/criteria'
|
|
4
5
|
|
|
5
6
|
module CrossOrigin
|
|
6
7
|
|
|
7
8
|
class << self
|
|
8
9
|
|
|
10
|
+
def to_name(origin)
|
|
11
|
+
if origin.is_a?(Symbol)
|
|
12
|
+
origin
|
|
13
|
+
else
|
|
14
|
+
origin.to_s.to_sym
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
9
18
|
def [](origin)
|
|
10
|
-
origin
|
|
11
|
-
origin_options[origin]
|
|
19
|
+
origin_options[to_name(origin)]
|
|
12
20
|
end
|
|
13
21
|
|
|
14
22
|
def config(origin, options = {})
|
|
15
|
-
origin = origin
|
|
23
|
+
origin = to_name(origin)
|
|
16
24
|
fail "Not allowed for origin name: #{origin}" if origin == :default
|
|
17
25
|
origin_options[origin] || (origin_options[origin] = Config.new(origin, options))
|
|
18
26
|
end
|
|
@@ -42,14 +42,23 @@ module CrossOrigin
|
|
|
42
42
|
CrossOrigin.configurations.each do |config|
|
|
43
43
|
if skip || limit
|
|
44
44
|
opts = opts.dup
|
|
45
|
-
|
|
45
|
+
current_count = previous_view.count(super: true)
|
|
46
46
|
if skip
|
|
47
|
-
opts[:skip] = skip =
|
|
47
|
+
opts[:skip] = skip =
|
|
48
|
+
if current_count < skip
|
|
49
|
+
skip - current_count
|
|
50
|
+
else
|
|
51
|
+
count += current_count - skip
|
|
52
|
+
0
|
|
53
|
+
end
|
|
48
54
|
end
|
|
49
55
|
if limit
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
opts[:limit] = limit =
|
|
57
|
+
if count > limit
|
|
58
|
+
0
|
|
59
|
+
else
|
|
60
|
+
limit - count
|
|
61
|
+
end
|
|
53
62
|
end
|
|
54
63
|
end
|
|
55
64
|
views << (previous_view = config.collection_for(model).find(selector, opts).modifiers(modifiers))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module CrossOrigin
|
|
2
|
+
class Criteria < Mongoid::Criteria
|
|
3
|
+
|
|
4
|
+
def initialize(criteria)
|
|
5
|
+
criteria.instance_values.each { |name, value| instance_variable_set(:"@#{name}", value) }
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def cross(origin = :default)
|
|
9
|
+
origin = CrossOrigin.to_name(origin)
|
|
10
|
+
cross_origin = CrossOrigin[origin]
|
|
11
|
+
return unless cross_origin || origin == :default
|
|
12
|
+
origins = Hash.new { |h, k| h[k] = [] }
|
|
13
|
+
docs = []
|
|
14
|
+
each do |record|
|
|
15
|
+
unless record.origin == origin
|
|
16
|
+
origins[record.collection] << record.id
|
|
17
|
+
doc = record.attributes.dup
|
|
18
|
+
doc['origin'] = origin
|
|
19
|
+
docs << doc
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
((cross_origin && cross_origin.collection_for(klass)) || klass.collection).insert_many(docs) unless docs.empty?
|
|
23
|
+
origins.each {|collection, ids| collection.find(_id: {'$in' => ids}).delete_many }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
module CrossOrigin
|
|
3
2
|
module Document
|
|
4
3
|
extend ActiveSupport::Concern
|
|
@@ -9,22 +8,38 @@ module CrossOrigin
|
|
|
9
8
|
attr_readonly :origin
|
|
10
9
|
|
|
11
10
|
validates_inclusion_of :origin, in: ->(doc) { doc.origin_enum }
|
|
11
|
+
end
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
def origin_enum
|
|
14
|
+
[:default] + CrossOrigin.names.to_a
|
|
15
|
+
end
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
def collection_name
|
|
18
|
+
origin == :default ? super : CrossOrigin[origin].collection_name_for(self.class)
|
|
19
|
+
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
def client_name
|
|
22
|
+
origin == :default ? super : CrossOrigin[origin].name
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def cross(origin = :default)
|
|
26
|
+
origin = CrossOrigin.to_name(origin)
|
|
27
|
+
cross_origin = CrossOrigin[origin]
|
|
28
|
+
return false if self.origin == origin || (cross_origin.nil? && origin != :default)
|
|
29
|
+
query = collection.find(_id: attributes['_id'])
|
|
30
|
+
doc = query.first
|
|
31
|
+
query.delete_one
|
|
32
|
+
doc['origin'] = origin
|
|
33
|
+
attributes['origin'] = origin
|
|
34
|
+
collection.insert_one(doc)
|
|
24
35
|
end
|
|
25
36
|
|
|
26
37
|
module ClassMethods
|
|
27
38
|
|
|
39
|
+
def queryable
|
|
40
|
+
CrossOrigin::Criteria.new(super)
|
|
41
|
+
end
|
|
42
|
+
|
|
28
43
|
def collection
|
|
29
44
|
CrossOrigin::Collection.new(super, self)
|
|
30
45
|
end
|
data/lib/cross_origin/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cross_origin
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- macarci
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-04-
|
|
11
|
+
date: 2016-04-08 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -61,6 +61,7 @@ files:
|
|
|
61
61
|
- cross_origin.gemspec
|
|
62
62
|
- lib/cross_origin.rb
|
|
63
63
|
- lib/cross_origin/collection.rb
|
|
64
|
+
- lib/cross_origin/criteria.rb
|
|
64
65
|
- lib/cross_origin/document.rb
|
|
65
66
|
- lib/cross_origin/version.rb
|
|
66
67
|
homepage: https://github.com/macarci/cross_origin
|