Module: Recliner::Document::ClassMethods
- Defined in:
- lib/recliner/document.rb
Instance Method Summary collapse
-
#create(attributes = {}) ⇒ Object
Creates an object and saves it to the database, if validations pass.
-
#create!(attributes = {}) ⇒ Object
Creates an object just like create but calls save! instead of save so an exception is raised if the document is invalid.
-
#database ⇒ Object
The Recliner::Database object to use for this class.
-
#default_database ⇒ Object
The default database to use for this class, based on the URI given to use_database!.
-
#human_attribute_name(attribute_key_name, options = {}) ⇒ Object
Transforms attribute key names into a more humane format, such as “First name” instead of “first_name”.
-
#human_name(options = {}) ⇒ Object
Transform the modelname into a more humane format, using I18n.
- #instantiate_from_database(attrs) ⇒ Object
-
#load(*ids) ⇒ Object
Loads one or more documents from the database given their document IDs.
-
#load!(*ids) ⇒ Object
Loads one or more documents from the database given their document IDs.
-
#self_and_descendants_from_recliner ⇒ Object
:nodoc:.
-
#use_database!(uri) ⇒ Object
Set a new database URI to use for this class and subclasses.
-
#with_database(db) ⇒ Object
Sets a database for a block, that all objects of this type created inside the block should use.
Instance Method Details
#create(attributes = {}) ⇒ Object
Creates an object and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.
The attributes
parameter can be either be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.
Examples
# Create a single new object
User.create(:first_name => 'Jamie')
# Create a single object and pass it into a block to set other attributes.
User.create(:first_name => 'Jamie') do |u|
u.is_admin = false
end
182 183 184 185 186 187 |
# File 'lib/recliner/document.rb', line 182 def create(attributes={}) returning new(attributes) do |doc| yield doc if block_given? doc.save end end |
#create!(attributes = {}) ⇒ Object
Creates an object just like create but calls save! instead of save so an exception is raised if the document is invalid.
191 192 193 194 195 196 |
# File 'lib/recliner/document.rb', line 191 def create!(attributes={}) returning new(attributes) do |doc| yield doc if block_given? doc.save! end end |
#database ⇒ Object
The Recliner::Database object to use for this class.
224 225 226 |
# File 'lib/recliner/document.rb', line 224 def database Thread.current["#{name}_database"] || default_database end |
#default_database ⇒ Object
The default database to use for this class, based on the URI given to use_database!.
229 230 231 |
# File 'lib/recliner/document.rb', line 229 def default_database @default_database ||= Database.new(read_inheritable_attribute(:database_uri)) end |
#human_attribute_name(attribute_key_name, options = {}) ⇒ Object
Transforms attribute key names into a more humane format, such as “First name” instead of “first_name”. Example:
Person.human_attribute_name("first_name") # => "First name"
This used to be deprecated in favor of humanize, but is now preferred, because it automatically uses the I18n module now. Specify options
with additional translating options.
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/recliner/document.rb', line 278 def human_attribute_name(attribute_key_name, = {}) defaults = self_and_descendants_from_recliner.map do |klass| :"#{klass.name.underscore}.#{attribute_key_name}" end defaults << [:default] if [:default] defaults.flatten! defaults << attribute_key_name.humanize [:count] ||= 1 I18n.translate(defaults.shift, .merge(:default => defaults, :scope => [:recliner, :attributes])) end |
#human_name(options = {}) ⇒ Object
Transform the modelname into a more humane format, using I18n. By default, it will underscore then humanize the class name (BlogPost.human_name #=> “Blog post”). Default scope of the translation is recliner.models Specify options
with additional translating options.
293 294 295 296 297 298 299 |
# File 'lib/recliner/document.rb', line 293 def human_name( = {}) defaults = self_and_descendants_from_recliner.map do |klass| :"#{klass.name.underscore}" end defaults << self.name.underscore.humanize I18n.translate(defaults.shift, {:scope => [:recliner, :models], :count => 1, :default => defaults}.merge()) end |
#instantiate_from_database(attrs) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/recliner/document.rb', line 243 def instantiate_from_database(attrs) unless attrs['class'] && (self == Document || attrs['class'] == name) raise DocumentNotFound end klass = attrs['class'].constantize returning(klass.new) do |doc| klass.properties.each do |name, property| doc.write_attribute(property.name, property.type.from_couch(attrs[property.as])) end doc.instance_variable_set("@new_record", false) doc.send(:changed_attributes).clear doc.send(:_run_load_callbacks) end end |
#load(*ids) ⇒ Object
Loads one or more documents from the database given their document IDs. If a document does not exist, nil will be returned.
Examples
>> TestDocument.load('some-document-id') # returns the object with id 'some-document-id'
>> TestDocument.load('missing-document') # returns nil for missing documents
>> TestDocument.load('document-1', 'document-2') # returns an array of objects with ids 'document-1', 'document-2'
151 152 153 |
# File 'lib/recliner/document.rb', line 151 def load(*ids) load_ids(ids, false) end |
#load!(*ids) ⇒ Object
Loads one or more documents from the database given their document IDs. If a document does not exist, a Recliner::DocumentNotFound exception will be raised.
Examples
>> TestDocument.load!('some-document-id') # returns the object with id 'some-document-id'
>> TestDocument.load!('missing-document') # raises Recliner::DocumentNotFound exception for missing documents
>> TestDocument.load!('document-1', 'document-2') # returns an array of objects with ids 'document-1', 'document-2'
163 164 165 |
# File 'lib/recliner/document.rb', line 163 def load!(*ids) load_ids(ids, true) end |
#self_and_descendants_from_recliner ⇒ Object
:nodoc:
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/recliner/document.rb', line 262 def self_and_descendants_from_recliner#:nodoc: klass = self classes = [klass] while klass.superclass != Document classes << klass = klass.superclass end classes rescue [self] end |
#use_database!(uri) ⇒ Object
Set a new database URI to use for this class and subclasses.
218 219 220 221 |
# File 'lib/recliner/document.rb', line 218 def use_database!(uri) @default_database = nil write_inheritable_attribute(:database_uri, uri) end |
#with_database(db) ⇒ Object
Sets a database for a block, that all objects of this type created inside the block should use.
234 235 236 237 238 239 240 |
# File 'lib/recliner/document.rb', line 234 def with_database(db) Thread.current["#{name}_database"] = db yield ensure Thread.current["#{name}_database"] = nil end |