Class: CouchDB::Model
- Inherits:
-
Object
- Object
- CouchDB::Model
- Defined in:
- lib/couchdb/model.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Class Method Summary collapse
- .connection ⇒ Object
- .db ⇒ Object
- .db_name ⇒ Object
- .doc_class ⇒ Object
-
.establish_connection(options_or_client) ⇒ Object
call-seq establish_connection options.
- .find(id) ⇒ Object
- .set_db_name(name) ⇒ Object
- .set_doc_class(doc_class) ⇒ Object
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, value) ⇒ Object
- #_id ⇒ Object (also: #id)
- #_rev ⇒ Object (also: #rev)
- #delete ⇒ Object
- #errors ⇒ Object
-
#initialize(attributes = nil) ⇒ Model
constructor
A new instance of Model.
- #new_record? ⇒ Boolean
- #read(chained_keys, splitter = '.') ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #update(attributes) ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Model
Returns a new instance of Model.
47 48 49 |
# File 'lib/couchdb/model.rb', line 47 def initialize(attributes = nil) @doc = self.class.db.new_doc attributes end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
45 46 47 |
# File 'lib/couchdb/model.rb', line 45 def doc @doc end |
Class Method Details
.connection ⇒ Object
13 14 15 |
# File 'lib/couchdb/model.rb', line 13 def self.connection @connection || superclass <= Model && superclass.connection || nil end |
.db ⇒ Object
17 18 19 |
# File 'lib/couchdb/model.rb', line 17 def self.db @db ||= connection && connection[db_name, doc_class] end |
.db_name ⇒ Object
25 26 27 |
# File 'lib/couchdb/model.rb', line 25 def self.db_name @db_name || superclass <= Model && superclass.db_name || nil end |
.doc_class ⇒ Object
36 37 38 |
# File 'lib/couchdb/model.rb', line 36 def self.doc_class @doc_class || superclass <= Model && superclass.doc_class || Document end |
.establish_connection(options_or_client) ⇒ Object
call-seq
establish_connection
call-seq
establish_connection client
8 9 10 11 |
# File 'lib/couchdb/model.rb', line 8 def self.establish_connection() @connection = @connection = Client.new connection unless connection.is_a?(Client) end |
.find(id) ⇒ Object
40 41 42 43 |
# File 'lib/couchdb/model.rb', line 40 def self.find(id) doc = db.find(id) new doc if doc end |
.set_db_name(name) ⇒ Object
21 22 23 |
# File 'lib/couchdb/model.rb', line 21 def self.set_db_name(name) @db_name = name end |
.set_doc_class(doc_class) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/couchdb/model.rb', line 29 def self.set_doc_class(doc_class) raise ArgumentError, "Not a Document." unless doc_class <= Document return if @doc_class == doc_class @doc_class = doc_class @db = nil end |
Instance Method Details
#[](attribute) ⇒ Object
51 52 53 |
# File 'lib/couchdb/model.rb', line 51 def [](attribute) @doc[attribute] end |
#[]=(attribute, value) ⇒ Object
55 56 57 |
# File 'lib/couchdb/model.rb', line 55 def []=(attribute, value) @doc[attribute] = value end |
#_id ⇒ Object Also known as: id
67 68 69 |
# File 'lib/couchdb/model.rb', line 67 def _id @doc._id end |
#_rev ⇒ Object Also known as: rev
73 74 75 |
# File 'lib/couchdb/model.rb', line 73 def _rev @doc._rev end |
#delete ⇒ Object
91 92 93 94 |
# File 'lib/couchdb/model.rb', line 91 def delete @doc.delete! freeze end |
#errors ⇒ Object
96 97 98 |
# File 'lib/couchdb/model.rb', line 96 def errors @doc.errors end |
#new_record? ⇒ Boolean
79 80 81 |
# File 'lib/couchdb/model.rb', line 79 def new_record? @doc.new_record? end |
#read(chained_keys, splitter = '.') ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/couchdb/model.rb', line 59 def read(chained_keys, splitter = '.') chained_keys.split(splitter).inject(@doc) { |value, key| value = value.respond_to?(key) ? value.send(key) : value[key] break if value.nil? value } end |
#save ⇒ Object
87 88 89 |
# File 'lib/couchdb/model.rb', line 87 def save @doc.save end |
#to_hash ⇒ Object
100 101 102 |
# File 'lib/couchdb/model.rb', line 100 def to_hash @doc.to_hash end |
#to_json ⇒ Object
104 105 106 |
# File 'lib/couchdb/model.rb', line 104 def to_json JSON.fast_generate @doc end |
#update(attributes) ⇒ Object
83 84 85 |
# File 'lib/couchdb/model.rb', line 83 def update(attributes) @doc.update! attributes end |