<commit_before># Patchstream

Emits json patches when active records are updated


## Installation

Add this line to your application's Gemfile:

    gem 'patchstream'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install patchstream

## Usage


### Standard

    class Product < ActiveRecord::Base
        include Patchstream
    end

    stream = [] # anything that responds to <<
    Product.patch_streams.add(stream)

    product = Product.create name: "Bike", price: 20
    stream == [
    	{op: "add", path: "/products/1", value: { 
    		id: 1, 
    		name: "Bike", 
    		price: 20, 
    		created_at: "2014-06-24T03:19:15.000Z",
    		updated_at: "2014-06-24T03:19:15.000Z"
    	}}
    ]
    stream.clear

    product.update_attributes name: "Bicycle", price: 30
    stream == [
    	{op: "replace", path: "/products/1/name", value: "Bike"},
    	{op: "replace", path: "/products/1/price", value: 20}
    ]
    stream.clear

    product.destroy
	stream == [
		{op: "remove", path: "/products/1/name"}
	]

### With a stream policy class

	class StreamPolicy < Struct.new(:user)
	    def permits?(operation, record) # operation is one of :create/:update/:destroy
	    	user.has_access_to?(record)
	    end
	end

	stream1 = []
	Product.patch_streams.add(stream1, StreamPolicy.new(user1))

	stream2 = []
	Product.patch_streams.add(stream2, StreamPolicy.new(user2))

	product = Product.create name: "Bike", price: 20, permitted_users: [user1]

	stream1.length == 0<commit_msg>Correct destroy example in readme<commit_after># Patchstream

Emits json patches when active records are updated


## Installation

Add this line to your application's Gemfile:

    gem 'patchstream'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install patchstream

## Usage


### Standard

    class Product < ActiveRecord::Base
        include Patchstream
    end

    stream = [] # anything that responds to <<
    Product.patch_streams.add(stream)

    product = Product.create name: "Bike", price: 20
    stream == [
    	{op: "add", path: "/products/1", value: { 
    		id: 1, 
    		name: "Bike", 
    		price: 20, 
    		created_at: "2014-06-24T03:19:15.000Z",
    		updated_at: "2014-06-24T03:19:15.000Z"
    	}}
    ]
    stream.clear

    product.update_attributes name: "Bicycle", price: 30
    stream == [
    	{op: "replace", path: "/products/1/name", value: "Bike"},
    	{op: "replace", path: "/products/1/price", value: 20}
    ]
    stream.clear

    product.destroy
	stream == [
		{op: "remove", path: "/products/1"}
	]

### With a stream policy class

	class StreamPolicy < Struct.new(:user)
	    def permits?(operation, record) # operation is one of :create/:update/:destroy
	    	user.has_access_to?(record)
	    end
	end

	stream1 = []
	Product.patch_streams.add(stream1, StreamPolicy.new(user1))

	stream2 = []
	Product.patch_streams.add(stream2, StreamPolicy.new(user2))

	product = Product.create name: "Bike", price: 20, permitted_users: [user1]

	stream1.length == 0
{'fraction_non_alphanumeric': 0.13247439709283118, 'fraction_numerical': 0.0383217707300958, 'mean_word_length': 3.0481283422459895, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2905644', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1011, 'n_words': 261}