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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f049945e2e24d37459f557a062abe66fd78a4d71
4
- data.tar.gz: cb53cf3deedd9c36190e212954dbf96074233815
3
+ metadata.gz: 4d011cf5d7bd463e95f1b603fe5856edc5ba01c4
4
+ data.tar.gz: 1ba1cc77c7f5b27ff53e53761f4bb59eb8b79fec
5
5
  SHA512:
6
- metadata.gz: 97653cbbcc6c8e88a6c4238ff2d93027331d230a1f1181f092e3417a9b78258abd433d82aeb2656a74709b735fd91ea2240f2353cab2b4cdf809e8fd821dd017
7
- data.tar.gz: ba58ab5a06600327e1798ac38bb3ebaf7ff8d56f216e29ae02a55de5a31caa903b87b32eb50a721f31b99cc2ef927cd2222ce48becc0818401cd50c6973a137f
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 = origin.to_s.to_sym unless origin.is_a?(Symbol)
11
- origin_options[origin]
19
+ origin_options[to_name(origin)]
12
20
  end
13
21
 
14
22
  def config(origin, options = {})
15
- origin = origin.to_s.to_sym unless origin.is_a?(Symbol)
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
- count += previous_view.count(super: true, skip: 0)
45
+ current_count = previous_view.count(super: true)
46
46
  if skip
47
- opts[:skip] = skip = (count < skip ? skip - count : 0)
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
- current_skip = skip || 0
51
- skipped_count = (current_skip > count ? 0 : count - current_skip)
52
- opts[:limit] = limit = (limit > skipped_count ? limit - skipped_count : 0)
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
- def origin_enum
14
- [:default] + CrossOrigin.names.to_a
15
- end
13
+ def origin_enum
14
+ [:default] + CrossOrigin.names.to_a
15
+ end
16
16
 
17
- def collection_name
18
- origin == :default ? super : CrossOrigin[origin].collection_name_for(self.class)
19
- end
17
+ def collection_name
18
+ origin == :default ? super : CrossOrigin[origin].collection_name_for(self.class)
19
+ end
20
20
 
21
- def client_name
22
- origin == :default ? super : CrossOrigin[origin].name
23
- end
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
@@ -1,3 +1,3 @@
1
1
  module CrossOrigin
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
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.1
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-07 00:00:00.000000000 Z
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