diff --git a/Rakefile b/Rakefile index 8cf05ec..f624098 100644 --- a/Rakefile +++ b/Rakefile @@ -1,23 +1,20 @@ require 'rubygems' require 'bundler' -Bundler::GemHelper.install_tasks - -require 'rake' +require 'rspec/core/rake_task' require "rake/rdoctask" -require 'rspec' -require 'rspec/core/rake_task' +Bundler::GemHelper.install_tasks desc "Run all specs" -Rspec::Core::RakeTask.new(:spec) do |spec| - spec.rspec_opts = ["--color"] - spec.pattern = 'spec/**/*_spec.rb' +RSpec::Core::RakeTask.new(:spec) do |spec| + spec.rspec_opts = ["--color"] + spec.pattern = 'spec/**/*_spec.rb' end desc "Print specdocs" -Rspec::Core::RakeTask.new(:doc) do |spec| - spec.rspec_opts = ["--format", "specdoc"] - spec.pattern = 'spec/*_spec.rb' +RSpec::Core::RakeTask.new(:doc) do |spec| + spec.rspec_opts = ["--format", "specdoc"] + spec.pattern = 'spec/*_spec.rb' end desc "Generate the rdoc" @@ -38,4 +35,4 @@ module Rake end Rake.remove_task("github:release") -Rake.remove_task("release") +Rake.remove_task("release") \ No newline at end of file diff --git a/couchrest_model.gemspec b/couchrest_model.gemspec index 6a8947f..ce7f2ab 100644 --- a/couchrest_model.gemspec +++ b/couchrest_model.gemspec @@ -17,17 +17,16 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/couchrest/couchrest_model} s.rubygems_version = %q{1.3.7} s.summary = %q{Extends the CouchRest Document for advanced modelling.} - + s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - + s.add_dependency(%q, "1.1.0.pre2") s.add_dependency(%q, "~> 1.15") - s.add_dependency(%q, "~> 3.0.0") + s.add_dependency(%q, "~> 3.0") s.add_dependency(%q, "~> 0.3.22") - s.add_dependency(%q, "~> 3.0.0") s.add_development_dependency(%q, ">= 2.0.0") s.add_development_dependency(%q, ">= 0.5.7") # s.add_development_dependency("jruby-openssl", ">= 0.7.3") diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index 0f6d26c..5f3a6cd 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -49,6 +49,8 @@ module CouchRest # * :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(doc = {}, options = {}) doc = prepare_all_attributes(doc, options) # set the instances database, if provided @@ -57,6 +59,8 @@ module CouchRest unless self['_id'] && self['_rev'] self[self.model_type_key] = self.class.to_s end + yield self if block_given? + after_initialize if respond_to?(:after_initialize) end diff --git a/lib/couchrest/model/properties.rb b/lib/couchrest/model/properties.rb index f421adf..5d815ab 100644 --- a/lib/couchrest/model/properties.rb +++ b/lib/couchrest/model/properties.rb @@ -5,8 +5,8 @@ module CouchRest extend ActiveSupport::Concern included do - extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties) - extlib_inheritable_accessor(:properties_by_name) unless self.respond_to?(:properties_by_name) + class_attribute(:properties) unless self.respond_to?(:properties) + class_attribute(:properties_by_name) unless self.respond_to?(:properties_by_name) self.properties ||= [] self.properties_by_name ||= {} raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=)) @@ -16,20 +16,6 @@ module CouchRest Hash[self].reject{|k,v| v.nil?}.as_json(options) end - # Returns the Class properties - # - # ==== Returns - # Array:: the list of properties for model's class - def properties - self.class.properties - end - - # Returns all the class's properties as a Hash where the key is the name - # of the property. - def properties_by_name - self.class.properties_by_name - end - # Returns the Class properties with their values # # ==== Returns diff --git a/lib/couchrest_model.rb b/lib/couchrest_model.rb index 7de6b41..8892ea4 100644 --- a/lib/couchrest_model.rb +++ b/lib/couchrest_model.rb @@ -1,7 +1,6 @@ require 'active_model' require "active_model/callbacks" require "active_model/conversion" -require "active_model/deprecated_error_methods" require "active_model/errors" require "active_model/naming" require "active_model/serialization" diff --git a/spec/couchrest/base_spec.rb b/spec/couchrest/base_spec.rb index 21c9a40..208d6e7 100644 --- a/spec/couchrest/base_spec.rb +++ b/spec/couchrest/base_spec.rb @@ -44,6 +44,11 @@ describe "Model Base" do @obj.database.should eql('database') end + it "should support initialization block" do + @obj = Basic.new {|b| b.database = 'database'} + @obj.database.should eql('database') + end + end describe "ActiveModel compatability Basic" do