taupe 0.5.3 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1b548f21b57a427e78515d3ea17ac0974f3a9d0
4
- data.tar.gz: d25d1f8a2dd03b30b583e48f99a353fa09597528
3
+ metadata.gz: 8a2c0a9f56009d2ce7ba093cd9069380c6b071b2
4
+ data.tar.gz: d1401fecf29967d599365dcfece011f69e788e27
5
5
  SHA512:
6
- metadata.gz: ef228a38b35977d1cada2add8000afeacf0d7756b42714d27d3b5806eb134193a0aa5f376aa56582440c8bd9451c7e52cc94c4b24b65593a01c5f764be248581
7
- data.tar.gz: 560d21ddc307e63daa65469bd566b9c8062ec1d99aabf7ade937be744e70fa4bf199047aceef660d2364604a7843ee16e7ae814480639b1462c4b0c02b6bdc91
6
+ metadata.gz: f660d2425a1766b1d829a38576d9bcdda563cbf1468a1569ce37db0a927f670fc620385073c6e7e20e1b37b54adbd6317978dad14fbb80bc46f04c0d0129134d
7
+ data.tar.gz: 5bf686251cfc41a9aef472295337c6306687ed74a1e4b590d2759f642295bb6deaf5e36cbedc752c86d9779f0fb3a14c85dc4cee9a425ab88c7e8c69ad1d693a
data/Gemfile CHANGED
@@ -4,3 +4,7 @@ source 'https://rubygems.org'
4
4
  gem 'rake'
5
5
  gem 'rspec'
6
6
  gem 'yard'
7
+
8
+ group :test do
9
+ gem 'sqlite3'
10
+ end
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Taupe
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/taupe.svg)](http://badge.fury.io/rb/taupe)
4
+ [![Build Status](https://travis-ci.org/pierre-lecocq/taupe.svg?branch=master)](https://travis-ci.org/pierre-lecocq/taupe)
5
+
3
6
  ## What is it?
4
7
 
5
8
  **Taupe** is a simple and elegant database model manager.
@@ -37,7 +40,7 @@ Here are the main reasons:
37
40
 
38
41
  ## How to use it?
39
42
 
40
- ### Prerequisites
43
+ ### Install
41
44
 
42
45
  In order to install the Taupe library, simply install the corresponding gem with
43
46
 
@@ -100,7 +103,7 @@ After that, you must have a few gems install on your system, depending on which
100
103
  Taupe::Cache.setup do
101
104
  type :memcached
102
105
  host :localhost
103
- port :11211
106
+ port 11211 # A symbol can not start with an integer, so make it an integer
104
107
  end
105
108
 
106
109
  #### Redis
@@ -109,7 +112,7 @@ After that, you must have a few gems install on your system, depending on which
109
112
  Taupe::Cache.setup do
110
113
  type :redis
111
114
  host :localhost
112
- port :6379
115
+ port 6379 # A symbol can not start with an integer, so make it an integer
113
116
  end
114
117
 
115
118
  ### Define some models
@@ -149,27 +152,98 @@ After that, you must have a few gems install on your system, depending on which
149
152
  # Reflect changes in database/cache (insert or update)
150
153
  article.save
151
154
 
152
- #### Query, query, query. But by yourself.
155
+ #### Execute raw queries
156
+
157
+ In order to execute raw queries like INERT, UPDATE or DELETE, the easiest method is to execute them directly on the database object. There is no specific need to execute them from a specific model's class.
153
158
 
154
- # Execute a single query
159
+ # Execute raw queries
155
160
  Taupe::Database.exec "INSERT INTO article (title, state) VALUES ('Another article', 1)"
161
+ Taupe::Database.exec "UPDATE article SET title = 'Yet another article' WHERE article_id = 1"
162
+ Taupe::Database.exec "DELETE article WHERE article_id = 1"
163
+
164
+ #### Fetching objects
165
+
166
+ There are two ways of fetching objects that depend on the expecting results
167
+
168
+ ##### Fetch objects and get an Array of Hashes
169
+
170
+ By executing a SELECT query on the database object, Taupe returns an Array of the Hash values of the object.
171
+
172
+ For example:
156
173
 
157
174
  # Fetch results in an array
158
175
  articles = Taupe::Database.fetch "SELECT * FROM article"
159
176
 
160
- # Fetch a single result
161
- article = Taupe::Database.fetch "SELECT * FROM article WHERE article_id = 3", true
177
+ gives:
178
+
179
+ [
180
+ { :article_id => 1, :title => 'An article'},
181
+ { :article_id => 2, :title => 'Another article'}
182
+ ]
162
183
 
163
- For more clarity, you can also exec or fetch from any model class. It is exactly the same.
184
+ This is great if you need either raw or flexible data to deal with afterwards.
185
+ Of course, you deal with this data like any ruby array of hashes.
164
186
 
165
- # Execute an insert query
166
- Taupe::Model::Article.exec "INSERT INTO article (title, state) VALUES ('Another article', 1)"
187
+ ##### Fetch objects and get an Array of Model Objects
188
+
189
+ By executing a SELECT query on a specific model object, Taupe returns an Array of Objects.
190
+
191
+ For example:
167
192
 
168
193
  # Fetch
169
194
  articles = Taupe::Model::Article.fetch "SELECT * FROM article"
170
195
 
171
- # Fetch single
172
- article = Taupe::Model::Article.fetch "SELECT * FROM article WHERE article_id = 3", true
196
+ gives (these are dummy data):
197
+
198
+ [
199
+ <Taupe::Model::Article:0x000000014e3c88 @_table=:article, @_columns={:article_id=>{:type=>Integer, :primary_key=>true}, # skip...
200
+ <Taupe::Model::Article:0x000000014e38b8 @_table=:article, @_columns={:article_id=>{:type=>Integer, :primary_key=>true} # skip...
201
+ ]
202
+
203
+ Therefore, it allows you to deal with all the objects contained in the result array like (for example):
204
+
205
+ articles.each do |article|
206
+ # Display some properties
207
+ puts article.article_id
208
+ puts "The title of this article is: #{article.title}"
209
+
210
+ # Modify some properties
211
+ article.title = '[Updated] ' + article.title
212
+
213
+ # And save the model
214
+ article.save
215
+ end
216
+
217
+ And this methods allows you to play with extended models like described just below.
218
+
219
+ #### Extend models
220
+
221
+ Models can be extended to add some properties that are not stored (and will not be stored) in the database.
222
+
223
+ Let's take an example to illustrate this, instead of talking:
224
+
225
+ module Taupe
226
+ class Model
227
+ class Article
228
+
229
+ def full_title
230
+ add_property :full_title, "Article #{article_id} - #{title}"
231
+ end
232
+
233
+ def tag_ids
234
+ add_property :tag_ids, [1, 2, 5]
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ This adds two new dummy properties that can be uesd as any other property:
241
+
242
+ articles = Taupe::Model::Article.fetch "SELECT * FROM article"
243
+
244
+ articles.each do |r|
245
+ puts r.full_title
246
+ end
173
247
 
174
248
  ## License
175
249
 
data/lib/taupe.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # File: taupe.rb
2
- # Time-stamp: <2014-09-11 16:27:47 pierre>
2
+ # Time-stamp: <2014-12-10 14:53:34 pierre>
3
3
  # Copyright (C) 2014 Pierre Lecocq
4
4
  # Description: Taupe library main file
5
5
 
@@ -13,7 +13,7 @@ require 'taupe/model'
13
13
  # Main Taupe module
14
14
  module Taupe
15
15
  # Current version constant in the form major.minor.patch
16
- VERSION = [0, 5, 3].join('.')
16
+ VERSION = [0, 6, 1].join('.')
17
17
 
18
18
  # Require a gem
19
19
  # @param gem_name [String] the gem name
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  # File: table.rb
4
- # Time-stamp: <2014-09-11 16:28:23 pierre>
4
+ # Time-stamp: <2014-12-10 14:46:01 pierre>
5
5
  # Copyright (C) 2014 Pierre Lecocq
6
6
  # Description: Taupe library model table class
7
7
 
@@ -62,6 +62,11 @@ module Taupe
62
62
  @_cache_key = options[:cache_key] || nil
63
63
  @_values = options[:values] || {}
64
64
 
65
+ # Clean up values (i.e SQLite duplicates values with numeric keys)
66
+ @_values.each do |k, _v|
67
+ @_values.delete(k) unless k.is_a? Symbol
68
+ end
69
+
65
70
  @_pkey = columns.select { |_k, v| v[:primary_key] == true }.first[0]
66
71
  fail "Primary key undefined for model #{table}" if @_pkey.nil?
67
72
 
@@ -95,8 +100,8 @@ module Taupe
95
100
 
96
101
  return nil if result.nil? || result.empty?
97
102
 
98
- result.map do |k, v|
99
- @_values[k.to_sym] = v unless k.is_a? Numeric
103
+ result.each do |k, v|
104
+ @_values[k.to_sym] = v if k.is_a? Symbol
100
105
  end
101
106
 
102
107
  Taupe::Cache.set @_cache_key, @_values unless @_cache_key.nil?
@@ -107,18 +112,20 @@ module Taupe
107
112
  def save(with_validations = true)
108
113
  Taupe::Validate.check(@_values, @_columns) if with_validations
109
114
 
115
+ real_values = @_values.keep_if { |k, _v| @_columns.key?(k) }
116
+
110
117
  if @_pkey_id.nil?
111
118
  query = "
112
119
  INSERT INTO #{@_table}
113
- (#{@_values.keys.map(&:to_s).join(', ')})
120
+ (#{real_values.keys.map(&:to_s).join(', ')})
114
121
  VALUES
115
- (#{@_values.values.map { |e| "'" + e.to_s + "'" }.join(', ')})
122
+ (#{real_values.values.map { |e| "'" + e.to_s + "'" }.join(', ')})
116
123
  "
117
124
 
118
125
  Taupe::Database.exec query
119
126
  @_pkey_id = Taupe::Database.last_id
120
127
  else
121
- joined_values = @_values.map { |k, v| "#{k} = '#{v}'" }.join(', ')
128
+ joined_values = real_values.map { |k, v| "#{k} = '#{v}'" }.join(', ')
122
129
  query = "
123
130
  UPDATE #{@_table} SET #{joined_values}
124
131
  WHERE #{@_pkey} = #{@_pkey_id}
@@ -155,7 +162,7 @@ module Taupe
155
162
  ms = m.to_s
156
163
  if ms.include? '='
157
164
  ms = ms[0..-2]
158
- if @_columns.include? ms.to_sym
165
+ if property? ms.to_sym
159
166
  @_values[ms.to_sym] = args[0]
160
167
  return true
161
168
  end
@@ -164,6 +171,30 @@ module Taupe
164
171
  super
165
172
  end
166
173
 
174
+ # Check if current model has a given property
175
+ # @param key [Symbol]
176
+ # @return [Boolean]
177
+ def property?(key)
178
+ @_columns.include? key.to_sym
179
+ end
180
+
181
+ # Add a propery to the current model
182
+ # @param key [Symbol]
183
+ # @param value [Mixed]
184
+ # @param column_properties [Hash]
185
+ # @return [Mixed]
186
+ def add_property(key, value = nil, column_properties = {})
187
+ key = key.to_sym
188
+ unless property? key
189
+ @_values[key] = value
190
+
191
+ # Include in database columns definition
192
+ @_columns[key] = {} unless column_properties.empty?
193
+ end
194
+
195
+ @_values[key]
196
+ end
197
+
167
198
  # Execute a single query
168
199
  # @param query [String] The query to execute
169
200
  # @return [Object]
data/spec/taupe_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # File: taupe_spec.rb
2
- # Time-stamp: <2014-08-01 12:00:00 pierre>
2
+ # Time-stamp: <2014-12-10 14:53:22 pierre>
3
3
  # Copyright (C) 2014 Pierre Lecocq
4
4
  # Description: Taupe library main tests file
5
5
 
@@ -7,8 +7,8 @@ require_relative '../lib/taupe'
7
7
 
8
8
  describe Taupe do
9
9
  describe 'VERSION' do
10
- it 'should return 0.5.3' do
11
- expect(Taupe::VERSION).to eql '0.5.3'
10
+ it 'should return 0.6.1' do
11
+ expect(Taupe::VERSION).to eql '0.6.1'
12
12
  end
13
13
  end
14
14
  end
data/taupe.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  # File: taupe.gemspec
2
- # Time-stamp: <2014-09-11 16:31:28 pierre>
2
+ # Time-stamp: <2014-12-10 14:50:35 pierre>
3
3
  # Copyright (C) 2014 Pierre Lecocq
4
4
  # Description: Taupe library gemspec file
5
5
 
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.authors = ['Pierre Lecocq']
17
17
  gem.email = ['pierre.lecocq@gmail.com']
18
18
  gem.summary = 'A model manager with database and cache backends in ruby'
19
- gem.description = 'Keep your database '
19
+ gem.description = 'Access to your database and cache backends easily in ruby'
20
20
  gem.homepage = 'https://github.com/pierre-lecocq/taupe'
21
- gem.date = '2014-08-01'
21
+ gem.date = Date.today.to_s
22
22
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taupe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Lecocq
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'Keep your database '
13
+ description: Access to your database and cache backends easily in ruby
14
14
  email:
15
15
  - pierre.lecocq@gmail.com
16
16
  executables: []