lib/couchrest/model/base.rb in couchrest_model-1.1.0.rc1 vs lib/couchrest/model/base.rb in couchrest_model-1.1.0

- old
+ new

@@ -5,24 +5,25 @@ extend ActiveModel::Naming include CouchRest::Model::Configuration include CouchRest::Model::Connection include CouchRest::Model::Persistence - include CouchRest::Model::Callbacks include CouchRest::Model::DocumentQueries include CouchRest::Model::Views include CouchRest::Model::DesignDoc include CouchRest::Model::ExtendedAttachments include CouchRest::Model::ClassProxy include CouchRest::Model::Proxyable include CouchRest::Model::Collection include CouchRest::Model::PropertyProtection include CouchRest::Model::Associations include CouchRest::Model::Validations + include CouchRest::Model::Callbacks include CouchRest::Model::Designs include CouchRest::Model::CastedBy include CouchRest::Model::Dirty + include CouchRest::Model::Callbacks def self.subclasses @subclasses ||= [] end @@ -44,12 +45,12 @@ # Instantiate a new CouchRest::Model::Base by preparing all properties # using the provided document hash. # # Options supported: # - # * :directly_set_attributes: true when data comes directly from database - # * :database: provide an alternative database + # * :directly_set_attributes, true when data comes directly from database + # * :database, provide an alternative database # # If a block is provided the new model will be passed into the # block so that it can be populated. def initialize(attributes = {}, options = {}) super() @@ -61,10 +62,11 @@ end yield self if block_given? after_initialize if respond_to?(:after_initialize) + run_callbacks(:initialize) { self } end # Temp solution to make the view_by methods available def self.method_missing(m, *args, &block) @@ -78,19 +80,35 @@ end end super end - def persisted? - !new? - end - def to_key new? ? nil : [id] end alias :to_param :id alias :new_record? :new? alias :new_document? :new? + + # Compare this model with another by confirming to see + # if the IDs and their databases match! + # + # Camparison of the database is required in case the + # model has been proxied or loaded elsewhere. + # + # A Basic CouchRest document will only ever compare using + # a Hash comparison on the attributes. + def == other + return false unless other.is_a?(Base) + if id.nil? && other.id.nil? + # no ids? assume comparing nested and revert to hash comparison + to_hash == other.to_hash + else + database == other.database && id == other.id + end + end + alias :eql? :== + end end end