class Patchstream::ClassMethods::PatchStreams

Public Instance Methods

add(stream) click to toggle source
# File lib/patchstream.rb, line 32
def add(stream)
        streams << stream
end
emit_create(record, &block) click to toggle source
# File lib/patchstream.rb, line 36
def emit_create(record, &block)
        emit(build_create_patch(record), &block)
end
emit_destroy(record, &block) click to toggle source
# File lib/patchstream.rb, line 44
def emit_destroy(record, &block)
        emit(build_destroy_patch(record), &block)
end
emit_update(record, &block) click to toggle source
# File lib/patchstream.rb, line 40
def emit_update(record, &block)
        emit_all(build_update_patches(record), &block)
end

Private Instance Methods

build_create_patch(record) click to toggle source
# File lib/patchstream.rb, line 49
def build_create_patch(record)
        {
                :op => :add, 
                :path => "/#{record.class.name.tableize}/#{record.id}", 
                :value => record.changes.inject({}) do |additions, (key, (_, new_value))|
                        additions[key] = new_value.as_json
                        additions
                end
        }
end
build_destroy_patch(record) click to toggle source
# File lib/patchstream.rb, line 71
def build_destroy_patch(record)
        {
                :op => :remove, 
                :path => "/#{record.class.name.tableize}/#{record.id}"
        }
end
build_update_patches(record) click to toggle source
# File lib/patchstream.rb, line 60
def build_update_patches(record)
        record.changes.inject([]) do |changes, (key, (_, new_value))|
                changes << {
                        :op => :replace,
                        :path => "/#{record.class.name.tableize}/#{record.id}/#{key}",
                        :value => new_value
                }
                changes
        end
end
emit(patch) { || ... } click to toggle source
# File lib/patchstream.rb, line 82
def emit(patch)
        yield if block_given?
        @streams.each{ |s| s << patch}
end
emit_all(patches) click to toggle source
# File lib/patchstream.rb, line 87
def emit_all(patches)
        patches.each{ |patch| emit(patch) }
end
streams() click to toggle source
# File lib/patchstream.rb, line 78
def streams
        @streams ||= []
end