Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Dem Bones

  • Loading branch information...
commit 1c754c0fb716b5729332f719fcc155798d2dbad9 0 parents
@PlasticLizard authored
32 .gitignore
@@ -0,0 +1,32 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
+
+idea/*
+.idea/*
+
+Gemfile.lock
+
+log/*
+
+*.log
+
+*.gem
6 Gemfile
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+gem 'bson_ext'
+gem 'mongo', '~>1.1.1'
+
+gem 'mongo_mapper', :git=>"http://github.com/jnunemaker/mongomapper.git", :branch=>"rails3"
+gemspec
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Nathan Stults
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
98 README.rdoc
@@ -0,0 +1,98 @@
+= mm-dirtier
+
+mm-dirtier is a MongoMapper plugin that extends dirty tracking to the entire object graph. The default dirty tracking implementation in MongoMapper will only track dirty attributes for top level documents, and does not detect changes to hash or array keys. mm-dirtier, which currently only works with the Rails3 branch of MongoMapper, will record changes throughout your entire document hierarchy, including in place modifications to embedded array and hash keys.
+
+== Installation
+
+mm-dirtier is available as a RubyGem:
+
+ gem install mm_dirtier
+
+To activate the plugin, add 'mm_dirtier' to your gemfile and
+
+ include 'mm_dirtier'
+
+when your app is initialized. mm-dirtier will take care of plugging itself into MongoMapper::Document and MongoMapper::EmbeddedDocument.
+
+== Usage
+
+mm-dirtier was created to serve a single purpose: to enable MongoMapper to automatically persist changes to documents via MongoDB's atomic opererators.
+Whether or not the deep dirty tracking mm-dirtier provides has any use beyond that, I have no idea. For that reason I will simply provide a basic sample of plugin usage in this readme. If anyone conceives of a use for this plugin beyond document persistence and wants to know more, add an issue to the tracker stating as much and I'll expand the documentation.
+
+=== Example:
+
+ class Procedure
+ include MongoMapper::EmbeddedDocument
+
+ key :name, String
+ end
+
+ class Diagnosis
+ include MongoMapper::Document
+
+ key :name, String
+ end
+
+ class Patient
+ include MongoMapper::Document
+
+ key :name, String
+ key :nicknames, Array
+ key :address, Hash
+
+ one :procedure
+ many :procedures
+ many :diagnosis_ids, Array
+ many :diagnoses, :in => :diagnosis_ids
+ end
+
+ p = Patient.create :name=>"Nathan Stults"
+ p.changed? => false
+ p.nicknames << "Col. Easy Andy"
+ p.changed? => true
+ p.nicknames_changed? => true
+ p.nicknames_change => [[],["Col. Easy Andy"]]
+
+ p.address["city"] = "Santa Rosa"
+ p.address_changed? => true
+ p.address_change => [{},{"city"=>"Santa Rosa"}]
+
+ p.procedure.build :name => "Labotomy"
+ p.procedure_changed? => true
+ p.procedure_change => [nil,<Class ...>]
+ p.procedure.changed? => true
+ p.procedure.name_changed? => true
+
+ p.procedures.build(:name=>"Labotomy")
+ p.procedures_changed? => true
+ p.procedures_change? => [[],[<Class...]]
+ p.procedures[0].changed? => true
+ p.procedures[0].name_changed? => true
+ p.procedures[0].name_change => [nil,"Labotomy"]
+
+ #in_array associations are a little different than embedded associations
+ #because what is actually stored on the document is only the array of ids
+ #then that is what is tracked for changed.
+ p.diagnoses << Diagnosis.create(:name=>"Aspergers Syndrom")
+ p.diagnosis_ids_changed? => true
+ p.diagnosis_ids_change => [[],[BSON::ObjectId('...')]]
+
+ p.save
+
+ p.changed? => false
+ p.procedure.changed? => false
+ p.procedures[0].changed? => false
+
+
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a future version unintentionally.
+* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 1776 Nathan Stults. See LICENSE for details.
31 Rakefile
@@ -0,0 +1,31 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'rake'
+require 'rake/testtask'
+require File.expand_path('../lib/mm_partial_update/version', __FILE__)
+
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+end
+
+task :default => :test
+
+desc 'Builds the gem'
+task :build do
+ sh "gem build mm_partial_update.gemspec"
+end
+
+desc 'Builds and installs the gem'
+task :install => :build do
+ sh "gem install mm_partial_update-#{MmPartialUpdate::Version}"
+end
+
+desc 'Tags version, pushes to remote, and pushes gem'
+task :release => :build do
+ sh "git tag v#{MmPartialUpdate::Version}"
+ sh "git push origin master"
+ sh "git push origin v#{MmPartialUpdate::Version}"
+ sh "gem push mm_dirtier-#{MmPartialUpdate::Version}.gem"
+end
+
15 lib/mm_partial_update.rb
@@ -0,0 +1,15 @@
+require 'rubygems'
+require "mongo_mapper"
+require "observables"
+require "mm_dirtier"
+
+base_dir = File.dirname(__FILE__)
+[
+ 'version',
+ 'extensions',
+ 'plugins/partial_update'
+].each {|req| require File.join(base_dir,'mm_partial_update',req)}
+
+
+MongoMapper::Document.append_inclusions(MmPartialUpdate::Plugins::PartialUpdate)
+MongoMapper::EmbeddedDocument.append_inclusions(MmPartialUpdate::Plugins::PartialUpdate)
6 lib/mm_partial_update/extensions.rb
@@ -0,0 +1,6 @@
+module MongoMapper
+ module Plugins
+
+
+ end
+end
24 lib/mm_partial_update/plugins/partial_update.rb
@@ -0,0 +1,24 @@
+# encoding: UTF-8
+module MmPartialUpdate
+ module Plugins
+ module PartialUpdate
+
+ def self.included(model)
+ model.plugin MmPartialUpdate::Plugins::PartialUpdate
+ end
+
+ def self.configure(model)
+ model.plugin MmDirtier::Plugins::Dirtier unless
+ model.plugins.include?(MmDirtier::Plugins::Dirtier)
+ end
+
+ module InstanceMethods
+
+ protected
+
+ private
+
+ end
+ end
+ end
+end
4 lib/mm_partial_update/version.rb
@@ -0,0 +1,4 @@
+# encoding: UTF-8
+module MmPartialUpdate
+ Version = '0.1.0'
+end
23 mm_partial_update.gemspec
@@ -0,0 +1,23 @@
+# encoding: UTF-8
+require File.expand_path('../lib/mm_partial_update/version', __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = 'mm_partial_update'
+ s.homepage = 'http://github.com/PlasticLizard/mm_partial_update'
+ s.summary = 'Partial updates for MongoMapper'
+ s.require_path = 'lib'
+ s.authors = ['Nathan Stults']
+ s.email = ['hereiam@sonic.net']
+ s.version = MmPartialUpdate::Version
+ s.platform = Gem::Platform::RUBY
+ s.files = Dir.glob("{lib,test}/**/*") + %w[LICENSE README.rdoc]
+
+ s.add_dependency 'observables', '~> 0.1.2'
+ s.add_dependency 'mm_dirtier', '~> 0.1.0'
+
+ s.add_development_dependency 'rake'
+ s.add_development_dependency 'log_buddy'
+ s.add_development_dependency 'jnunemaker-matchy', '~> 0.4.0'
+ s.add_development_dependency 'shoulda', '~> 2.11'
+end
+
95 test/test_helper.rb
@@ -0,0 +1,95 @@
+require 'rubygems'
+require 'bundler/setup'
+
+$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'mm_partial_update'
+require 'fileutils'
+require 'ostruct'
+
+require 'log_buddy'
+require 'matchy'
+require 'shoulda'
+
+class Test::Unit::TestCase
+ def Doc(name='Class', &block)
+ klass = Class.new
+ klass.class_eval do
+ include MongoMapper::Document
+ set_collection_name :test
+
+ if name
+ class_eval "def self.name; '#{name}' end"
+ class_eval "def self.to_s; '#{name}' end"
+ end
+ end
+
+ klass.class_eval(&block) if block_given?
+ klass.collection.remove
+ klass
+ end
+
+ def EDoc(name='Class', &block)
+ klass = Class.new do
+ include MongoMapper::EmbeddedDocument
+
+ if name
+ class_eval "def self.name; '#{name}' end"
+ class_eval "def self.to_s; '#{name}' end"
+ end
+ end
+
+ klass.class_eval(&block) if block_given?
+ klass
+ end
+
+ def drop_indexes(klass)
+ if klass.database.collection_names.include?(klass.collection.name)
+ klass.collection.drop_indexes
+ end
+ end
+
+ custom_matcher :be_true do |receiver, matcher, args|
+ matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
+ matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
+ receiver.eql?(true)
+ end
+
+ custom_matcher :be_false do |receiver, matcher, args|
+ matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
+ matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
+ receiver.eql?(false)
+ end
+
+ custom_matcher :have_error_on do |receiver, matcher, args|
+ receiver.valid?
+ attribute = args[0]
+ expected_message = args[1]
+
+ if expected_message.nil?
+ matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
+ matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
+ !receiver.errors[attribute].blank?
+ else
+ actual = receiver.errors[attribute]
+ matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
+ matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
+ actual.include? expected_message
+ end
+ end
+
+ custom_matcher :have_index do |receiver, matcher, args|
+ index_name = args[0]
+ matcher.positive_failure_message = "#{receiver} does not have index named #{index_name}, but should"
+ matcher.negative_failure_message = "#{receiver} does have index named #{index_name}, but should not"
+ !receiver.collection.index_information.detect { |index| index[0] == index_name }.nil?
+ end
+end
+
+log_dir = File.expand_path('../../log', __FILE__)
+FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
+logger = Logger.new(log_dir + '/test.log')
+
+LogBuddy.init(:logger => logger)
+MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
+MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
+MongoMapper.database.collections.each { |c| c.drop_indexes }
9 test/test_mm_partial_update.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class TestMmPartialUpdate < Test::Unit::TestCase
+ context "including mm_partial_update" do
+ should "include something or other" do
+ assert_equal defined?(MmPartialUpdate::Plugins::PartialUpdate), "constant"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.