leanmodel 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,8 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  gem "activesupport"
5
5
  gem "activemodel"
6
+ gem "uuidtools"
7
+
6
8
 
7
9
  # Add dependencies to develop your gem here.
8
10
  # Include everything needed to run rake, tests, features, etc.
@@ -26,6 +26,7 @@ GEM
26
26
  shoulda-matchers (~> 1.0.0)
27
27
  shoulda-context (1.0.0)
28
28
  shoulda-matchers (1.0.0)
29
+ uuidtools (2.1.2)
29
30
 
30
31
  PLATFORMS
31
32
  ruby
@@ -38,3 +39,4 @@ DEPENDENCIES
38
39
  rcov
39
40
  rdoc (~> 3.12)
40
41
  shoulda
42
+ uuidtools
@@ -1,16 +1,73 @@
1
- = leanmodel
1
+ = Leanmodel
2
2
 
3
- Description goes here.
3
+ Simple ORM for CouchDB. This a work in progress...
4
+
5
+ ==Installation
6
+ gem install leanmodel
7
+
8
+ ==Usage
9
+
10
+ require 'leanback'
11
+ require 'leanmodel'
12
+
13
+ Name of the class must be singular to a Couchdb database name .
14
+ The class below represents a document in the database called movies
15
+ class Movie < LeanModel::Base
16
+ attributes :title, :year, :story
17
+ end
18
+
19
+ movie = Movie.new({:title => 'Jurassic Park',:year => '1993', :story => 'Raptors brought back in time'})
20
+
21
+ movie.attributes
22
+ #=> {"title"=>"Jurassic Park", "year"=>"1993", "story"=>"Raptors brought back in time"}
23
+
24
+ Configure couchDB
25
+ @couchdb_username = "obi"
26
+ @couchdb_password = "trusted"
27
+
28
+ token = lambda {
29
+ hash = Couchdb.login(@couchdb_username,@couchdb_password)
30
+ hash["AuthSession"]
31
+ }
32
+ movie.config(token)
33
+
34
+ CouchDB Authsession token
35
+ movie.token
36
+ #=> b2JpOjRGQTIxODA4Oq42xHwvvoK9ASvtB55ODSdEpEgB
37
+
38
+ Database name
39
+ movie.database
40
+ #=> movies
41
+
42
+ Write this model to the database
43
+ movie.save
44
+ #=> true
45
+ Returns true if the model was written successfully.
46
+ This method creates a new couchDB document with the model's attribute values
47
+
48
+ Delete this model from database
49
+ movie.destroy
50
+ #=>{"ok"=>true, "id"=>"eb9ecefa-afdd-426a-a21d-f8307fa2b156", "rev"=>"2-98bc85346eff4a663e39395163aaa194"}
51
+ This deletes the document that represents this model from the database
52
+
53
+ Find a movie using it's document id
54
+ movie.service.find('9da82f5a-0ce4-46a0-863c-7d8f2ed84439')
55
+ #=> {"_id"=>"9da82f5a-0ce4-46a0-863c-7d8f2ed84439", "_rev"=>"6-6367dc24342f1590935a99087a57d8c5",
56
+ "title"=>"Jurassic Park", "year"=>"1996", "story"=>"Raptors brought back in time"}
4
57
 
5
- == Contributing to leanmodel
6
58
 
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
59
+ Update a model on the database,
60
+ data = {:id => '9da82f5a-0ce4-46a0-863c-7d8f2ed84439', :year => '1993' }
61
+ movie.service.update_attributes(data)
62
+ #=> true
63
+ Returns true if successful, requires that the document ID is included in the data
64
+
65
+ Retrieve all movies from database
66
+ movie.service.all
67
+ #=> [{"_id"=>"0fb32571-44c4-48c4-9423-55ea7c95c192", "_rev"=>"1-f76f5d4f431cafbdb3f4d43388a03c89",
68
+ "title"=>"Hugo", "year"=>"2011", "story"=>"Film history"},
69
+ {"_id"=>"9da82f5a-0ce4-46a0-863c-7d8f2ed84439", "_rev"=>"10-60e258f605801a31e64b928bb81f7ea6",
70
+ "title"=>"Jurassic Park", "year"=>"1993", "story"=>"Raptors brought back in time"}]
14
71
 
15
72
  == Copyright
16
73
 
data/Rakefile CHANGED
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
17
17
  gem.name = "leanmodel"
18
18
  gem.homepage = "http://github.com/obi-a/leanmodel"
19
19
  gem.license = "MIT"
20
- gem.summary = %Q{Basic ActiveModel, does nothing}
21
- gem.description = %Q{Basic ActiveModel, does nothing}
20
+ gem.summary = %Q{Simple ORM for couchDB}
21
+ gem.description = %Q{Simple ORM for couchDB}
22
22
  gem.email = "obioraakubue@yahoo.com"
23
23
  gem.authors = ["obi-a"]
24
24
  # dependencies defined in Gemfile
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "leanmodel"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["obi-a"]
12
- s.date = "2012-04-15"
13
- s.description = "Basic ActiveModel, does nothing"
12
+ s.date = "2012-05-05"
13
+ s.description = "Simple ORM for couchDB"
14
14
  s.email = "obioraakubue@yahoo.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
35
35
  s.licenses = ["MIT"]
36
36
  s.require_paths = ["lib"]
37
37
  s.rubygems_version = "1.8.21"
38
- s.summary = "Basic ActiveModel, does nothing"
38
+ s.summary = "Simple ORM for couchDB"
39
39
 
40
40
  if s.respond_to? :specification_version then
41
41
  s.specification_version = 3
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
44
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
45
45
  s.add_runtime_dependency(%q<activemodel>, [">= 0"])
46
+ s.add_runtime_dependency(%q<uuidtools>, [">= 0"])
46
47
  s.add_development_dependency(%q<shoulda>, [">= 0"])
47
48
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
49
  s.add_development_dependency(%q<bundler>, [">= 0"])
@@ -51,6 +52,7 @@ Gem::Specification.new do |s|
51
52
  else
52
53
  s.add_dependency(%q<activesupport>, [">= 0"])
53
54
  s.add_dependency(%q<activemodel>, [">= 0"])
55
+ s.add_dependency(%q<uuidtools>, [">= 0"])
54
56
  s.add_dependency(%q<shoulda>, [">= 0"])
55
57
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
58
  s.add_dependency(%q<bundler>, [">= 0"])
@@ -60,6 +62,7 @@ Gem::Specification.new do |s|
60
62
  else
61
63
  s.add_dependency(%q<activesupport>, [">= 0"])
62
64
  s.add_dependency(%q<activemodel>, [">= 0"])
65
+ s.add_dependency(%q<uuidtools>, [">= 0"])
63
66
  s.add_dependency(%q<shoulda>, [">= 0"])
64
67
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
65
68
  s.add_dependency(%q<bundler>, [">= 0"])
@@ -5,6 +5,9 @@ module LeanModel
5
5
  extend ActiveModel::Translation
6
6
  include ActiveModel::Validations
7
7
  include ActiveModel::AttributeMethods
8
+
9
+ attr_accessor :database_token
10
+ attr_accessor :doc_id
8
11
 
9
12
  class_attribute :_attributes
10
13
  self._attributes = []
@@ -14,6 +17,12 @@ module LeanModel
14
17
 
15
18
  attribute_method_suffix '?'
16
19
 
20
+ def initialize(attributes = {})
21
+ attributes.each do |attr, value|
22
+ self.send("#{attr}=",value)
23
+ end unless attributes.blank?
24
+ end
25
+
17
26
  def self.attributes(*names)
18
27
  attr_accessor *names
19
28
 
@@ -32,6 +41,68 @@ module LeanModel
32
41
  false
33
42
  end
34
43
 
44
+ #database functions
45
+ def config(token)
46
+ @database_token = token
47
+ end
48
+
49
+ def token
50
+ if @database_token.lambda?
51
+ @database_token.call
52
+ end
53
+ end
54
+
55
+ def database
56
+ self.class.name.split('::').last.downcase + 's'
57
+ end
58
+
59
+ def all
60
+ #returns hash of all key/values in the database
61
+ docs = Couchdb.docs_from database,token
62
+ end
63
+
64
+ def find(id)
65
+ #returns a model object with the given ID
66
+ doc = {:database => database, :doc_id => id}
67
+ hash = Couchdb.view doc,token
68
+ end
69
+
70
+ def save
71
+ #save this model object to database
72
+ if self.valid?
73
+ @doc_id = UUIDTools::UUID.random_create.to_s
74
+ doc = { :database => database, :doc_id => @doc_id, :data => attributes}
75
+ Couchdb.create_doc doc,token
76
+ true
77
+ else
78
+ false
79
+ end
80
+ end
81
+
82
+ def service
83
+ self
84
+ end
85
+
86
+ def update_attributes(hash)
87
+ #update this model on database
88
+ if self.valid?
89
+ id = hash[:id]
90
+ hash.delete(:id)
91
+ doc = { :database => database, :doc_id => id, :data => hash}
92
+ Couchdb.update_doc doc,token
93
+ true
94
+ else
95
+ false
96
+ end
97
+ end
98
+
99
+ def destroy
100
+ #delete this model from database
101
+ doc = {:database => database, :doc_id => @doc_id}
102
+ Couchdb.delete_doc doc,token
103
+ end
104
+
105
+ #end of database functions
35
106
  protected
36
107
  def clear_attribute(attribute)
37
108
  send("#{attribute}=",nil)
@@ -1,4 +1,5 @@
1
1
  require 'active_support'
2
2
  require 'active_model'
3
+ require 'uuidtools'
3
4
 
4
5
  require 'lean_model/base'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leanmodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-15 00:00:00.000000000 Z
12
+ date: 2012-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: uuidtools
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: shoulda
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +139,7 @@ dependencies:
123
139
  - - ! '>='
124
140
  - !ruby/object:Gem::Version
125
141
  version: '0'
126
- description: Basic ActiveModel, does nothing
142
+ description: Simple ORM for couchDB
127
143
  email: obioraakubue@yahoo.com
128
144
  executables: []
129
145
  extensions: []
@@ -159,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
175
  version: '0'
160
176
  segments:
161
177
  - 0
162
- hash: -296849791
178
+ hash: 610187215
163
179
  required_rubygems_version: !ruby/object:Gem::Requirement
164
180
  none: false
165
181
  requirements:
@@ -171,5 +187,5 @@ rubyforge_project:
171
187
  rubygems_version: 1.8.21
172
188
  signing_key:
173
189
  specification_version: 3
174
- summary: Basic ActiveModel, does nothing
190
+ summary: Simple ORM for couchDB
175
191
  test_files: []