Merge branch 'master' of https://github.com/lucasrenan/couchrest_model into bundler
This commit is contained in:
commit
93cce72a2f
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -6,3 +6,5 @@ Gemfile*
|
||||||
.rvmrc
|
.rvmrc
|
||||||
.bundle
|
.bundle
|
||||||
couchdb.std*
|
couchdb.std*
|
||||||
|
*.*~
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,12 @@ module CouchRest
|
||||||
doc
|
doc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def last(opts = {})
|
||||||
|
doc = @klass.last({:database => @database}.merge(opts))
|
||||||
|
doc.database = @database if doc && doc.respond_to?(:database)
|
||||||
|
doc
|
||||||
|
end
|
||||||
|
|
||||||
def get(id)
|
def get(id)
|
||||||
doc = @klass.get(id, @database)
|
doc = @klass.get(id, @database)
|
||||||
doc.database = @database if doc && doc.respond_to?(:database)
|
doc.database = @database if doc && doc.respond_to?(:database)
|
||||||
|
|
|
@ -38,6 +38,22 @@ module CouchRest
|
||||||
first_instance.empty? ? nil : first_instance.first
|
first_instance.empty? ? nil : first_instance.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Load the last document that have the model_type_key's field equal to
|
||||||
|
# the name of the current class.
|
||||||
|
# It's similar to method first, just adds :descending => true
|
||||||
|
#
|
||||||
|
# ==== Returns
|
||||||
|
# Object:: The last object instance available
|
||||||
|
# or
|
||||||
|
# Nil:: if no instances available
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# opts<Hash>::
|
||||||
|
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
|
||||||
|
def last(opts = {})
|
||||||
|
first(opts.merge!(:descending => true))
|
||||||
|
end
|
||||||
|
|
||||||
# Load a document from the database by id
|
# Load a document from the database by id
|
||||||
# No exceptions will be raised if the document isn't found
|
# No exceptions will be raised if the document isn't found
|
||||||
#
|
#
|
||||||
|
|
|
@ -18,6 +18,16 @@ module CouchRest
|
||||||
self.class.properties
|
self.class.properties
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the Class properties with their values
|
||||||
|
#
|
||||||
|
# ==== Returns
|
||||||
|
# Array:: the list of properties with their values
|
||||||
|
def properties_with_values
|
||||||
|
props = {}
|
||||||
|
properties.each { |property| props[property.name] = read_attribute(property.name) }
|
||||||
|
props
|
||||||
|
end
|
||||||
|
|
||||||
# Read the casted value of an attribute defined with a property.
|
# Read the casted value of an attribute defined with a property.
|
||||||
#
|
#
|
||||||
# ==== Returns
|
# ==== Returns
|
||||||
|
@ -47,7 +57,6 @@ module CouchRest
|
||||||
|
|
||||||
private
|
private
|
||||||
# The following methods should be accessable by the Model::Base Class, but not by anything else!
|
# The following methods should be accessable by the Model::Base Class, but not by anything else!
|
||||||
|
|
||||||
def apply_all_property_defaults
|
def apply_all_property_defaults
|
||||||
return if self.respond_to?(:new?) && (new? == false)
|
return if self.respond_to?(:new?) && (new? == false)
|
||||||
# TODO: cache the default object
|
# TODO: cache the default object
|
||||||
|
@ -206,3 +215,4 @@ module CouchRest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -87,6 +87,12 @@ describe "Proxy Class" do
|
||||||
u = @us.first
|
u = @us.first
|
||||||
u.title.should =~ /\A...\z/
|
u.title.should =~ /\A...\z/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should get last" do
|
||||||
|
u = @us.last
|
||||||
|
u.title.should == "aaa"
|
||||||
|
end
|
||||||
|
|
||||||
it "should set database on first retreived document" do
|
it "should set database on first retreived document" do
|
||||||
u = @us.first
|
u = @us.first
|
||||||
u.database.should === DB
|
u.database.should === DB
|
||||||
|
|
|
@ -22,6 +22,11 @@ describe "Model properties" do
|
||||||
@card.properties.map{|p| p.name}.should include("first_name")
|
@card.properties.map{|p| p.name}.should include("first_name")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should list object properties with values" do
|
||||||
|
@card.properties_with_values.should be_an_instance_of(Hash)
|
||||||
|
@card.properties_with_values["first_name"].should == "matt"
|
||||||
|
end
|
||||||
|
|
||||||
it "should let you access a property value (getter)" do
|
it "should let you access a property value (getter)" do
|
||||||
@card.first_name.should == "matt"
|
@card.first_name.should == "matt"
|
||||||
end
|
end
|
||||||
|
@ -869,3 +874,4 @@ describe "Property Class" do
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -273,6 +273,12 @@ describe "Model views" do
|
||||||
u = Unattached.first :database=>@db
|
u = Unattached.first :database=>@db
|
||||||
u.title.should =~ /\A...\z/
|
u.title.should =~ /\A...\z/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should get last" do
|
||||||
|
u = Unattached.last :database=>@db
|
||||||
|
u.title.should == "aaa"
|
||||||
|
end
|
||||||
|
|
||||||
it "should barf on all_design_doc_versions if no database given" do
|
it "should barf on all_design_doc_versions if no database given" do
|
||||||
lambda{Unattached.all_design_doc_versions}.should raise_error
|
lambda{Unattached.all_design_doc_versions}.should raise_error
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue