Class: CouchObject::Document
- Inherits:
-
Object
- Object
- CouchObject::Document
- Includes:
- Enumerable
- Defined in:
- lib/couch_object/document.rb
Overview
Represents a CouchDb document
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#revision ⇒ Object
Returns the value of attribute revision.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Look up an attribute by
key
. -
#[]=(key, value) ⇒ Object
Set an attributes by
key
tovalue
. -
#each(&blk) ⇒ Object
yields each document attribute.
-
#has_key?(key) ⇒ Boolean
is the attribute
key
in this document?. -
#initialize(attributes = {}) ⇒ Document
constructor
initializes a new document object with
attributes
as the document values. -
#new? ⇒ Boolean
is this a new document?.
-
#respond_to?(meth) ⇒ Boolean
is the attribute
key
in this document?. -
#save(database) ⇒ Object
Saves this document to the
database
. -
#to_json(extra = {}) ⇒ Object
Converts the Document to a JSON representation of its attributes.
Constructor Details
#initialize(attributes = {}) ⇒ Document
initializes a new document object with attributes
as the document values
8 9 10 11 12 |
# File 'lib/couch_object/document.rb', line 8 def initialize(attributes={}) @attributes = attributes.dup @id = @attributes.delete("_id") || @attributes.delete("id") @revision = @attributes.delete("_rev") || @attributes.delete("rev") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object (private)
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/couch_object/document.rb', line 93 def method_missing(method_symbol, *arguments) method_name = method_symbol.to_s case method_name[-1..-1] when "=" self[method_name[0..-2]] = arguments.first when "?" self[method_name[0..-2]] == true else has_key?(method_name) ? self[method_name] : super end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
13 14 15 |
# File 'lib/couch_object/document.rb', line 13 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
13 14 15 |
# File 'lib/couch_object/document.rb', line 13 def id @id end |
#revision ⇒ Object
Returns the value of attribute revision.
13 14 15 |
# File 'lib/couch_object/document.rb', line 13 def revision @revision end |
Instance Method Details
#[](key) ⇒ Object
Look up an attribute by key
41 42 43 |
# File 'lib/couch_object/document.rb', line 41 def [](key) attributes[key] end |
#[]=(key, value) ⇒ Object
Set an attributes by key
to value
46 47 48 |
# File 'lib/couch_object/document.rb', line 46 def []=(key, value) attributes[key] = value end |
#each(&blk) ⇒ Object
yields each document attribute
31 32 33 |
# File 'lib/couch_object/document.rb', line 31 def each(&blk) @attributes.each(&blk) end |
#has_key?(key) ⇒ Boolean
is the attribute key
in this document?
51 52 53 |
# File 'lib/couch_object/document.rb', line 51 def has_key?(key) attributes.has_key?(key) end |
#new? ⇒ Boolean
is this a new document?
26 27 28 |
# File 'lib/couch_object/document.rb', line 26 def new? @id.nil? && @revision.nil? end |
#respond_to?(meth) ⇒ Boolean
is the attribute key
in this document?
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/couch_object/document.rb', line 56 def respond_to?(meth) method_name = meth.to_s if has_key?(method_name) return true elsif %w[ ? = ].include?(method_name[-1..-1]) && has_key?(method_name[0..-2]) return true end super end |
#save(database) ⇒ Object
Saves this document to the database
36 37 38 |
# File 'lib/couch_object/document.rb', line 36 def save(database) new? ? create(database) : update(database) end |
#to_json(extra = {}) ⇒ Object
Converts the Document to a JSON representation of its attributes
69 70 71 72 73 74 75 76 |
# File 'lib/couch_object/document.rb', line 69 def to_json(extra={}) if id.nil? opts = {}.merge(extra) else opts = {"_id" => id}.merge(extra) end attributes.merge(opts).to_json end |