Created
January 16, 2011 15:54
-
-
Save dcolthorp/781900 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################ | |
| # spec/spec_helper.rb, in RSpec.configure block | |
| config.before do | |
| MongoidCleaner.start | |
| end | |
| config.after do | |
| MongoidCleaner.clean | |
| end | |
| ############################################ | |
| # spec/support/mongoid.rb | |
| class MongoidCleaner | |
| class <<self | |
| def start | |
| begin | |
| DatabaseCleaner.orm = 'mongoid' | |
| DatabaseCleaner.strategy = :truncation | |
| DatabaseCleaner.start | |
| ensure | |
| DatabaseCleaner.orm = 'active_record' | |
| DatabaseCleaner.strategy = :truncation | |
| end | |
| end | |
| def clean | |
| begin | |
| DatabaseCleaner.orm = 'mongoid' | |
| DatabaseCleaner.strategy = :truncation | |
| DatabaseCleaner.clean | |
| ensure | |
| DatabaseCleaner.orm = 'active_record' | |
| DatabaseCleaner.strategy = :truncation | |
| end | |
| end | |
| end | |
| end | |
| ############################################ | |
| # lib/pseudo_attributes.rb | |
| module PseudoAttributes | |
| extend ActiveSupport::Concern | |
| Null = (class NullClass; self; end).new | |
| included do | |
| alias_method_chain :reload, :pseudo_attributes if instance_methods.include?('reload') | |
| class_inheritable_accessor :_pseudos | |
| self._pseudos = {} | |
| end | |
| module InstanceMethods | |
| def reload_with_pseudo_attributes(*args) | |
| self.class.send(:_pseudos).each_key do |key| | |
| instance_variable_set key, Null | |
| end | |
| reload_without_pseudo_attributes(*args) | |
| end | |
| end | |
| module ClassMethods | |
| def pseudo_attribute name, options={}, &block | |
| options.reverse_merge! :writer => false | |
| instance_variable = :"@#{name}" | |
| _pseudos[instance_variable] = block | |
| class_eval <<-RUBY, caller[1] | |
| def #{name} | |
| if !#{name}_populated? | |
| #{instance_variable} = instance_eval(&self.class.send(:_pseudos)[:#{instance_variable}]) | |
| end | |
| #{instance_variable} == PseudoAttributes::Null ? nil : #{instance_variable} | |
| end | |
| def #{name}_populated? | |
| defined?(#{instance_variable}) && #{instance_variable} != PseudoAttributes::Null | |
| end | |
| RUBY | |
| if options[:writer] | |
| class_eval <<-RUBY | |
| def #{name}=(value) | |
| #{instance_variable} = value | |
| end | |
| RUBY | |
| end | |
| end | |
| end | |
| end | |
| ######################################################## | |
| # Example use of PseudoAttributes | |
| class User < ActiveRecord::Base | |
| ... | |
| pseudo_attribute :profiles do | |
| Profile.for_user(self) | |
| end | |
| ... | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment