query_dam 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a4fd20c5b7ba8e610fd9757b7d1df78eed85b76
4
+ data.tar.gz: 7ed9a41bc2ffcb516c11753882d0a5de6032aab2
5
+ SHA512:
6
+ metadata.gz: 7129aa97a1b6618a0c394f26a04b1c1868722e0a118138531ad9fa4607ec01774c81d40003610d25840ebe2dd832faf1c35958698ce60b5d00ca797e429779a5
7
+ data.tar.gz: b97ac6cc7920020935cee268af83f1b1e37e047754ae29355619831412ea3673ab065a0c24fac7aeb9d1c81dc931fdbcf00df61ff4ab49640646799929a824df
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in query_dam.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Roman Kuznietsov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # QueryDam
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'query_dam'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install query_dam
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/query_dam/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,34 @@
1
+ module QueryDam
2
+ module Cacheable
3
+ extend ActiveSupport::Concern
4
+
5
+ def cache_query(relation)
6
+ query_id = SecureRandom.uuid
7
+ sql = relation.where("`#{relation.model.table_name}`.`id` = #{query_id}").to_sql
8
+ Redis::HashKey.new(query_id).bulk_set(sql: sql, model: relation.model.name)
9
+ Redis::Set.new(relation.model.name) << query_id
10
+ query_id
11
+ end
12
+
13
+ def query_updates(query_ids)
14
+ query_ids.map do |query_id|
15
+ model = Redis::HashKey.new(query_id)[:model].constantize
16
+ updated_set = Redis::Set.new("#{query_id}_updated")
17
+ excluded_set = Redis::Set.new("#{query_id}_excluded")
18
+
19
+ updated_ids = updated_set.to_a
20
+ excluded_ids = excluded_set.to_a
21
+
22
+ updated_set.clear
23
+ excluded_set.clear
24
+
25
+ updated_records = model.where(id: updated_ids)
26
+ updated_records = yield(updated_records) if block_given?
27
+
28
+ {query_id: query_id,
29
+ updated: updated_records,
30
+ excluded: excluded_ids}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ module QueryDam
2
+ module Trackable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ after_save :update_cached_queries
7
+ after_destroy :update_cached_queries
8
+ end
9
+
10
+ def update_cached_queries
11
+ Redis::Set.new(self.class.name).members.each do |query_id|
12
+ sql = Redis::HashKey.new(query_id)[:sql].gsub(query_id, id.to_s)
13
+
14
+ updated_set = Redis::Set.new("#{query_id}_updated")
15
+ excluded_set = Redis::Set.new("#{query_id}_excluded")
16
+
17
+ if self.class.find_by_sql(sql).any?
18
+ updated_set << id
19
+ excluded_set.delete id
20
+ else
21
+ updated_set.delete id
22
+ excluded_set << id
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module QueryDam
2
+ VERSION = '0.0.1'
3
+ end
data/lib/query_dam.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'securerandom'
2
+ require 'active_support/dependencies'
3
+
4
+ require 'query_dam/version'
5
+
6
+ module QueryDam
7
+ autoload :Trackable, 'query_dam/concerns/trackable'
8
+ autoload :Cacheable, 'query_dam/concerns/cacheable'
9
+ end
data/query_dam.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'query_dam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'query_dam'
8
+ spec.version = QueryDam::VERSION
9
+ spec.authors = ['Roman Kuznietsov']
10
+ spec.email = ['roman.kuznietsov@gmail.com']
11
+ spec.summary = %q{Detecting query result changes}
12
+ spec.description = %q{}
13
+ spec.homepage = 'https://github.com/romankuznietsov/query_dam'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'redis', '>= 3.1.0'
22
+ spec.add_dependency 'redis-objects', '>= 0.8.0'
23
+ spec.add_dependency 'activemodel', '>= 4.0.0'
24
+
25
+ spec.add_development_dependency 'bundler', "~> 1.6"
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
28
+ spec.add_development_dependency 'sqlite3'
29
+ end
@@ -0,0 +1,12 @@
1
+ RSpec.configure do |config|
2
+ config.order = :random
3
+
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.syntax = :expect
6
+ end
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.syntax = :expect
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_dam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roman Kuznietsov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis-objects
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 4.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 4.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - roman.kuznietsov@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/query_dam.rb
125
+ - lib/query_dam/concerns/cacheable.rb
126
+ - lib/query_dam/concerns/trackable.rb
127
+ - lib/query_dam/version.rb
128
+ - query_dam.gemspec
129
+ - spec/spec_helper.rb
130
+ homepage: https://github.com/romankuznietsov/query_dam
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.0.3
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Detecting query result changes
154
+ test_files:
155
+ - spec/spec_helper.rb