Class: LeanModel::Base
- Inherits:
-
Object
- Object
- LeanModel::Base
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/lean_model/base.rb
Instance Attribute Summary collapse
-
#database_token ⇒ Object
Returns the value of attribute database_token.
-
#doc_id ⇒ Object
Returns the value of attribute doc_id.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #attributes ⇒ Object
-
#config(token) ⇒ Object
database functions.
- #database ⇒ Object
- #destroy ⇒ Object
- #find(id) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #service ⇒ Object
- #token ⇒ Object
- #update_attributes(hash) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
20 21 22 23 24 |
# File 'lib/lean_model/base.rb', line 20 def initialize(attributes = {}) attributes.each do |attr, value| self.send("#{attr}=",value) end unless attributes.blank? end |
Instance Attribute Details
#database_token ⇒ Object
Returns the value of attribute database_token.
9 10 11 |
# File 'lib/lean_model/base.rb', line 9 def database_token @database_token end |
#doc_id ⇒ Object
Returns the value of attribute doc_id.
10 11 12 |
# File 'lib/lean_model/base.rb', line 10 def doc_id @doc_id end |
Class Method Details
.attributes(*names) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/lean_model/base.rb', line 26 def self.attributes(*names) attr_accessor *names define_attribute_methods names self._attributes += names end |
Instance Method Details
#all ⇒ Object
59 60 61 62 |
# File 'lib/lean_model/base.rb', line 59 def all #returns hash of all key/values in the database docs = Couchdb.docs_from database,token end |
#attributes ⇒ Object
33 34 35 36 37 38 |
# File 'lib/lean_model/base.rb', line 33 def attributes self._attributes.inject({}) do |hash,attr| hash[attr.to_s] = send(attr) hash end end |
#config(token) ⇒ Object
database functions
45 46 47 |
# File 'lib/lean_model/base.rb', line 45 def config(token) @database_token = token end |
#database ⇒ Object
55 56 57 |
# File 'lib/lean_model/base.rb', line 55 def database self.class.name.split('::').last.downcase + 's' end |
#destroy ⇒ Object
99 100 101 102 103 |
# File 'lib/lean_model/base.rb', line 99 def destroy #delete this model from database doc = {:database => database, :doc_id => @doc_id} Couchdb.delete_doc doc,token end |
#find(id) ⇒ Object
64 65 66 67 68 |
# File 'lib/lean_model/base.rb', line 64 def find(id) #returns a model object with the given ID doc = {:database => database, :doc_id => id} hash = Couchdb.view doc,token end |
#persisted? ⇒ Boolean
40 41 42 |
# File 'lib/lean_model/base.rb', line 40 def persisted? false end |
#save ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lean_model/base.rb', line 70 def save #save this model object to database if self.valid? @doc_id = UUIDTools::UUID.random_create.to_s doc = { :database => database, :doc_id => @doc_id, :data => attributes} Couchdb.create_doc doc,token true else false end end |
#service ⇒ Object
82 83 84 |
# File 'lib/lean_model/base.rb', line 82 def service self end |
#token ⇒ Object
49 50 51 52 53 |
# File 'lib/lean_model/base.rb', line 49 def token if @database_token.lambda? @database_token.call end end |
#update_attributes(hash) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lean_model/base.rb', line 86 def update_attributes(hash) #update this model on database if self.valid? id = hash[:id] hash.delete(:id) doc = { :database => database, :doc_id => id, :data => hash} Couchdb.update_doc doc,token true else false end end |