Class: LeanModel::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/lean_model/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_tokenObject

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_idObject

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

#allObject



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

#attributesObject



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

#databaseObject



55
56
57
# File 'lib/lean_model/base.rb', line 55

def database
  self.class.name.split('::').last.downcase + 's'
end

#destroyObject



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

Returns:

  • (Boolean)


40
41
42
# File 'lib/lean_model/base.rb', line 40

def persisted?
  false
end

#saveObject



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

#serviceObject



82
83
84
# File 'lib/lean_model/base.rb', line 82

def service
 self
end

#tokenObject



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