sentiql 0.3.0 → 0.3.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.
- data/README.rdoc +76 -24
- data/db/migrate/20110515_test_tables.rb +1 -1
- data/lib/sentiql/version.rb +1 -1
- data/lib/sentiql.rb +7 -3
- data/spec/sentiql_spec.rb +73 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -15,8 +15,14 @@ This is REALY a work in progress, I wrote this lib during one evening after read
|
|
15
15
|
== Instalation
|
16
16
|
gem install sentiql
|
17
17
|
|
18
|
-
==
|
19
|
-
|
18
|
+
== Features
|
19
|
+
* OM to create, update and find by attribute
|
20
|
+
* Prepared SQL statements, which automatically escapes input
|
21
|
+
* Filters: before_save, before_create, after_save and after_create
|
22
|
+
* Validations expressed in boolean logic
|
23
|
+
* Automatic timestamps
|
24
|
+
|
25
|
+
== Configuration
|
20
26
|
DB = Mysql2::Client.new(
|
21
27
|
:host=>'localhost',
|
22
28
|
:username=>'username',
|
@@ -26,40 +32,86 @@ This is REALY a work in progress, I wrote this lib during one evening after read
|
|
26
32
|
DB.query_options.merge!(:symbolize_keys=>true)
|
27
33
|
SentiQL::Base.connection = DB
|
28
34
|
|
29
|
-
|
35
|
+
== Usage examples
|
36
|
+
1. Create
|
37
|
+
u = User.create :name=>'tdurden', :password=>'project_mayhem'
|
30
38
|
|
31
|
-
|
32
|
-
|
33
|
-
u.
|
34
|
-
|
35
|
-
# updating
|
36
|
-
u[:name] = 'Tyler'
|
39
|
+
2. Update
|
40
|
+
# single attribute update
|
41
|
+
u = User.find_by :name=>'tdurden'
|
42
|
+
u.full_name = 'Tyler Durden'
|
37
43
|
u.save
|
38
44
|
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
|
45
|
+
# multiple update
|
46
|
+
put '/users' do
|
47
|
+
@user = User.find_by :id=>params[:user][:id]
|
48
|
+
@user.updated_attributes params[:user]
|
49
|
+
@user.save
|
50
|
+
end
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
|
52
|
+
3. Select
|
53
|
+
# find by attribute
|
54
|
+
u = User.find_by :name=>'tdurden'
|
55
|
+
|
56
|
+
# select with SQL
|
57
|
+
|
58
|
+
# "?" is replaced with escaped adequate value from params array
|
59
|
+
sql = 'SELECT * FROM users WHERE name=?'
|
60
|
+
u = User.first sql, ['tdurden'] # -> [ {:name=>'tdurden', :password=>'project_mayhem'} ]
|
47
61
|
|
48
|
-
|
62
|
+
users = User.all 'SELECT * FROM users' # => Mysql2::Result
|
63
|
+
users.map {|u| u[:name] } #=> ['username1', 'username2']
|
49
64
|
|
65
|
+
== Model
|
66
|
+
1. Basic
|
50
67
|
class User < SentiQL::Base
|
51
|
-
|
52
|
-
|
53
|
-
|
68
|
+
|
69
|
+
set_table :users
|
70
|
+
set_schema :name, :full_name, :email, :crypted_password, :salt, :created_at, :updated_at
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
2. Using filters
|
75
|
+
|
76
|
+
before_save :create_salt, :encrypt_password,
|
77
|
+
after_create :download_profile_image
|
54
78
|
|
55
|
-
|
79
|
+
protected
|
56
80
|
|
57
|
-
|
81
|
+
def create_salt
|
82
|
+
self[:salt] = 'salt'
|
83
|
+
end
|
84
|
+
|
85
|
+
def encrypt_password
|
86
|
+
self[:crypted_password] = "#{self[:salt]}#{self[:password]}"
|
87
|
+
end
|
88
|
+
|
89
|
+
def download_profile_image
|
90
|
+
# ...
|
91
|
+
end
|
58
92
|
|
59
|
-
|
60
|
-
|
93
|
+
3. Validations
|
94
|
+
...
|
95
|
+
|
96
|
+
def validate
|
97
|
+
name_not_blank? && name_not_longer_then?(20)
|
98
|
+
end
|
99
|
+
|
100
|
+
def name_not_blank?
|
101
|
+
if self[:name] == "" || self[:name].nil?
|
102
|
+
@errors << "Name can't
|
103
|
+
be blank"
|
104
|
+
return false
|
61
105
|
end
|
106
|
+
return true
|
107
|
+
end
|
62
108
|
|
109
|
+
def name_not_longer_then? max
|
110
|
+
if self[:name].length > max
|
111
|
+
@errors << "Name can't be longer then #{max} characters"
|
112
|
+
return false
|
113
|
+
end
|
114
|
+
return true
|
63
115
|
end
|
64
116
|
|
65
117
|
== Licence
|
data/lib/sentiql/version.rb
CHANGED
data/lib/sentiql.rb
CHANGED
@@ -7,7 +7,7 @@ module SentiQL
|
|
7
7
|
|
8
8
|
def initialize attrs={}
|
9
9
|
@attrs = {}
|
10
|
-
@errors =
|
10
|
+
@errors = []
|
11
11
|
attrs.each_pair do |key, value|
|
12
12
|
@attrs[key.to_sym] = value
|
13
13
|
end
|
@@ -31,7 +31,6 @@ module SentiQL
|
|
31
31
|
|
32
32
|
|
33
33
|
def save
|
34
|
-
|
35
34
|
filter_with :before_save_filters
|
36
35
|
|
37
36
|
if @attrs[:id]
|
@@ -84,7 +83,12 @@ module SentiQL
|
|
84
83
|
end
|
85
84
|
end
|
86
85
|
|
87
|
-
def
|
86
|
+
def validate; end
|
87
|
+
def valid?
|
88
|
+
@errors = []
|
89
|
+
validate
|
90
|
+
return @errors == [] ? true : false
|
91
|
+
end
|
88
92
|
|
89
93
|
class << self
|
90
94
|
def connection; @@connection; end
|
data/spec/sentiql_spec.rb
CHANGED
@@ -1,5 +1,42 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
require 'date'
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
class Club < SentiQL::Base
|
6
|
+
|
7
|
+
set_table :clubs
|
8
|
+
set_schema :name, :description, :reqs, :created_at, :updated_at
|
9
|
+
|
10
|
+
def validate
|
11
|
+
name_not_blank?
|
12
|
+
description_not_blank? && description_not_longer_then(400)
|
13
|
+
end
|
14
|
+
|
15
|
+
def name_not_blank?
|
16
|
+
if self[:name].blank?
|
17
|
+
errors << "Name can't be blank"
|
18
|
+
return false
|
19
|
+
end
|
20
|
+
return true
|
21
|
+
end
|
22
|
+
|
23
|
+
def description_not_blank?
|
24
|
+
if self[:description].blank?
|
25
|
+
errors << "Description can't be blank"
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
return true
|
29
|
+
end
|
30
|
+
|
31
|
+
def description_not_longer_then max
|
32
|
+
if self[:description].length > max
|
33
|
+
errors << "Description can't be longer then #{max.to_s}"
|
34
|
+
return false
|
35
|
+
end
|
36
|
+
return true
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
3
40
|
|
4
41
|
class User < SentiQL::Base
|
5
42
|
|
@@ -43,6 +80,42 @@ end
|
|
43
80
|
|
44
81
|
describe SentiQL::Base do
|
45
82
|
|
83
|
+
describe 'validations' do
|
84
|
+
|
85
|
+
describe '.valid?' do
|
86
|
+
|
87
|
+
it 'returns false if there are errors' do
|
88
|
+
c = Club.new :name=>'Fight Club'
|
89
|
+
c.save
|
90
|
+
c.valid?.should be_false
|
91
|
+
c[:description] = 'some description'
|
92
|
+
c.valid?.should be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
it "doesn't save to DB if record is not valid" do
|
98
|
+
c = Club.new :name=>'Fight Club'
|
99
|
+
c.save
|
100
|
+
c[:id].should be_nil
|
101
|
+
end
|
102
|
+
|
103
|
+
it "saves record if its valid" do
|
104
|
+
c = Club.new :name=>'Fight Club'
|
105
|
+
c.save
|
106
|
+
c[:description] = 'some description'
|
107
|
+
c.save
|
108
|
+
c.id.should_not be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns a list of errors if records does not pass validations" do
|
112
|
+
c = Club.new :name=>'Fight Club'
|
113
|
+
c.valid?
|
114
|
+
c.errors.size.should_not == []
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
46
119
|
describe User do
|
47
120
|
|
48
121
|
describe '.create' do
|