patchstream 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +17 -0
  4. data/Guardfile +9 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +59 -0
  7. data/Rakefile +1 -0
  8. data/lib/patchstream.rb +92 -0
  9. data/lib/patchstream/version.rb +3 -0
  10. data/patchstream.gemspec +23 -0
  11. data/spec/dummy/.gitignore +16 -0
  12. data/spec/dummy/Gemfile +47 -0
  13. data/spec/dummy/README.rdoc +28 -0
  14. data/spec/dummy/Rakefile +6 -0
  15. data/spec/dummy/app/assets/images/.keep +0 -0
  16. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  17. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  19. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  20. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  21. data/spec/dummy/app/mailers/.keep +0 -0
  22. data/spec/dummy/app/models/.keep +0 -0
  23. data/spec/dummy/app/models/concerns/.keep +0 -0
  24. data/spec/dummy/app/models/product.rb +4 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/spec/dummy/bin/bundle +3 -0
  27. data/spec/dummy/bin/rails +4 -0
  28. data/spec/dummy/bin/rake +4 -0
  29. data/spec/dummy/config.ru +4 -0
  30. data/spec/dummy/config/application.rb +23 -0
  31. data/spec/dummy/config/boot.rb +4 -0
  32. data/spec/dummy/config/database.yml +25 -0
  33. data/spec/dummy/config/environment.rb +5 -0
  34. data/spec/dummy/config/environments/development.rb +29 -0
  35. data/spec/dummy/config/environments/production.rb +80 -0
  36. data/spec/dummy/config/environments/test.rb +36 -0
  37. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/spec/dummy/config/initializers/inflections.rb +16 -0
  40. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  41. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  42. data/spec/dummy/config/initializers/session_store.rb +3 -0
  43. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  44. data/spec/dummy/config/locales/en.yml +23 -0
  45. data/spec/dummy/config/routes.rb +56 -0
  46. data/spec/dummy/db/migrate/20140624014830_create_products.rb +11 -0
  47. data/spec/dummy/db/schema.rb +25 -0
  48. data/spec/dummy/db/seeds.rb +7 -0
  49. data/spec/dummy/lib/assets/.keep +0 -0
  50. data/spec/dummy/lib/tasks/.keep +0 -0
  51. data/spec/dummy/log/.keep +0 -0
  52. data/spec/dummy/public/404.html +58 -0
  53. data/spec/dummy/public/422.html +58 -0
  54. data/spec/dummy/public/500.html +57 -0
  55. data/spec/dummy/public/favicon.ico +0 -0
  56. data/spec/dummy/public/robots.txt +5 -0
  57. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  58. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  59. data/spec/patchstream_spec.rb +60 -0
  60. data/spec/spec_helper.rb +26 -0
  61. metadata +182 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4900b6ac510f622cfeb8ca3433e90dcd84198a3e
4
+ data.tar.gz: 6d1988cf56a923a0de5166acbe12244bcc978a6f
5
+ SHA512:
6
+ metadata.gz: 0c5efc756f128186c2898a868c456f5800fa302c188f43deedeb5ca5a07f3279c4b1b3d3e94935c79a2ccaa7c98cf856e68d36723474e022253792c39a1231f2
7
+ data.tar.gz: e3bbf12c5d8670868f860f49f96f55e3bdfddd1873cf910acb5224690e106a9405d1ff3e94552fbd92e0998d34e7d9d85a6ff81ddeef037fa90e8f74ef3c15b0
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in patchstream.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'guard', '>= 2.2.4'
9
+ gem 'guard-rspec'
10
+ gem 'mocha'
11
+ gem 'jsondiff'
12
+ gem 'rails'
13
+ gem 'rspec-rails'
14
+ gem 'sqlite3'
15
+ gem 'timecop'
16
+ gem 'activeuuid'
17
+ end
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 opsb
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.
@@ -0,0 +1,59 @@
1
+ # Patchstream
2
+
3
+ Emits json patches when active records are updated
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'patchstream'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install patchstream
19
+
20
+ ## Usage
21
+
22
+ class Product
23
+ include Patchstream
24
+ end
25
+
26
+ stream = [] # anything that responds to <<
27
+ Product.patch_streams.add(stream)
28
+
29
+ product = Product.create name: "Bike", price: 20
30
+ stream == [
31
+ {op: "add", path: "/products/1", value: {
32
+ id: 1,
33
+ name: "Bike",
34
+ price: 20,
35
+ created_at: "2014-06-24T03:19:15.000Z",
36
+ updated_at: "2014-06-24T03:19:15.000Z"
37
+ }}
38
+ ]
39
+ stream.clear
40
+
41
+ product.update_attributes name: "Bicycle", price: 30
42
+ stream == [
43
+ {op: "replace", path: "/products/1/name", value: "Bike"},
44
+ {op: "replace", path: "/products/1/price", value: 20}
45
+ ]
46
+ stream.clear
47
+
48
+ product.destroy
49
+ stream == [
50
+ {op: "remove", path: "/products/1/name"}
51
+ ]
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,92 @@
1
+ require "patchstream/version"
2
+
3
+ module Patchstream
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ around_create PatchStreamCallbacks.new
8
+ around_update PatchStreamCallbacks.new
9
+ around_destroy PatchStreamCallbacks.new
10
+ end
11
+
12
+ class PatchStreamCallbacks
13
+ def around_create(record, &block)
14
+ record.class.patch_streams.emit_create(record, &block)
15
+ end
16
+
17
+ def around_update(record, &block)
18
+ record.class.patch_streams.emit_update(record, &block)
19
+ end
20
+
21
+ def around_destroy(record, &block)
22
+ record.class.patch_streams.emit_destroy(record, &block)
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def patch_streams
28
+ @patch_streams ||= PatchStreams.new
29
+ end
30
+
31
+ class PatchStreams
32
+ def add(stream)
33
+ streams << stream
34
+ end
35
+
36
+ def emit_create(record, &block)
37
+ emit(build_create_patch(record), &block)
38
+ end
39
+
40
+ def emit_update(record, &block)
41
+ emit_all(build_update_patches(record), &block)
42
+ end
43
+
44
+ def emit_destroy(record, &block)
45
+ emit(build_destroy_patch(record), &block)
46
+ end
47
+
48
+ private
49
+ def build_create_patch(record)
50
+ {
51
+ :op => :add,
52
+ :path => "/#{record.class.name.tableize}/#{record.id}",
53
+ :value => record.changes.inject({}) do |additions, (key, (_, new_value))|
54
+ additions[key] = new_value.as_json
55
+ additions
56
+ end
57
+ }
58
+ end
59
+
60
+ def build_update_patches(record)
61
+ record.changes.inject([]) do |changes, (key, (_, new_value))|
62
+ changes << {
63
+ :op => :replace,
64
+ :path => "/#{record.class.name.tableize}/#{record.id}/#{key}",
65
+ :value => new_value
66
+ }
67
+ changes
68
+ end
69
+ end
70
+
71
+ def build_destroy_patch(record)
72
+ {
73
+ :op => :remove,
74
+ :path => "/#{record.class.name.tableize}/#{record.id}"
75
+ }
76
+ end
77
+
78
+ def streams
79
+ @streams ||= []
80
+ end
81
+
82
+ def emit(patch)
83
+ yield if block_given?
84
+ @streams.each{ |s| s << patch}
85
+ end
86
+
87
+ def emit_all(patches)
88
+ patches.each{ |patch| emit(patch) }
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module Patchstream
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'patchstream/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "patchstream"
8
+ spec.version = Patchstream::VERSION
9
+ spec.authors = ["opsb"]
10
+ spec.email = ["oliver@opsb.co.uk"]
11
+ spec.description = %q{Emits json patches when active records are updated}
12
+ spec.summary = %q{Emits json patches when active records are updated}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,47 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4
+ gem 'rails', '4.0.4'
5
+
6
+ # Use sqlite3 as the database for Active Record
7
+ gem 'sqlite3'
8
+
9
+ # Use SCSS for stylesheets
10
+ gem 'sass-rails', '~> 4.0.2'
11
+
12
+ # Use Uglifier as compressor for JavaScript assets
13
+ gem 'uglifier', '>= 1.3.0'
14
+
15
+ # Use CoffeeScript for .js.coffee assets and views
16
+ gem 'coffee-rails', '~> 4.0.0'
17
+
18
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
19
+ # gem 'therubyracer', platforms: :ruby
20
+
21
+ # Use jquery as the JavaScript library
22
+ gem 'jquery-rails'
23
+
24
+ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
25
+ gem 'turbolinks'
26
+
27
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
28
+ gem 'jbuilder', '~> 1.2'
29
+ gem 'activeuuid'
30
+ gem 'patchstream', path: "../../"
31
+
32
+ group :doc do
33
+ # bundle exec rake doc:rails generates the API under doc/api.
34
+ gem 'sdoc', require: false
35
+ end
36
+
37
+ # Use ActiveModel has_secure_password
38
+ # gem 'bcrypt', '~> 3.1.7'
39
+
40
+ # Use unicorn as the app server
41
+ # gem 'unicorn'
42
+
43
+ # Use Capistrano for deployment
44
+ # gem 'capistrano', group: :development
45
+
46
+ # Use debugger
47
+ # gem 'debugger', group: [:development, :test]
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ class Product < ActiveRecord::Base
2
+ include ActiveUUID::UUID
3
+ include Patchstream
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')