baza_models 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +107 -0
- data/LICENSE.txt +20 -0
- data/README.md +23 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/baza_models/errors.rb +30 -0
- data/lib/baza_models/model.rb +228 -0
- data/lib/baza_models/validators/base_validator.rb +8 -0
- data/lib/baza_models/validators/presence_validator.rb +5 -0
- data/lib/baza_models.rb +17 -0
- data/shippable.yml +9 -0
- data/spec/baza_models/model_spec.rb +152 -0
- data/spec/baza_models_spec.rb +4 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/test_classes/user_test.rb +17 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 97e4c8978bcb4c3871711744b67dbeb46587b2b7
|
4
|
+
data.tar.gz: e8233d6be05b457895c5ab6334c09f4460ead3d4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e91e463217a4c3b601adc67d66550f263f4cb528018d7fe67560d7e0a20deb19a7b1c7048598b18cc0f875c9990fd7111eb0560d2dddba297753f52117cfb448
|
7
|
+
data.tar.gz: b1c3e2958d01c720a0b3437793b08ce80e0dfbf8cfafaff46d3848beb16d618c48562173b61aa4b5fdf4c95e408a7397e0673757922443bca1d811a9e801fa46
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "baza", "~> 0.0.15"
|
7
|
+
gem "string-cases"
|
8
|
+
|
9
|
+
# Add dependencies to develop your gem here.
|
10
|
+
# Include everything needed to run rake, tests, features, etc.
|
11
|
+
group :development do
|
12
|
+
gem "rspec", "~> 2.8.0"
|
13
|
+
gem "rdoc", "~> 3.12"
|
14
|
+
gem "bundler", "~> 1.0"
|
15
|
+
gem "jeweler", "~> 2.0.1"
|
16
|
+
gem "sqlite3"
|
17
|
+
end
|
18
|
+
|
19
|
+
gem "codeclimate-test-reporter", group: :test, require: nil
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.3.7)
|
5
|
+
array_enumerator (0.0.7)
|
6
|
+
baza (0.0.15)
|
7
|
+
array_enumerator
|
8
|
+
datet
|
9
|
+
knjrbfw
|
10
|
+
string-cases
|
11
|
+
wref
|
12
|
+
builder (3.2.2)
|
13
|
+
codeclimate-test-reporter (0.4.6)
|
14
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
15
|
+
datet (0.0.25)
|
16
|
+
descendants_tracker (0.0.4)
|
17
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
18
|
+
diff-lcs (1.1.3)
|
19
|
+
docile (1.1.5)
|
20
|
+
faraday (0.9.1)
|
21
|
+
multipart-post (>= 1.2, < 3)
|
22
|
+
git (1.2.9.1)
|
23
|
+
github_api (0.12.3)
|
24
|
+
addressable (~> 2.3)
|
25
|
+
descendants_tracker (~> 0.0.4)
|
26
|
+
faraday (~> 0.8, < 0.10)
|
27
|
+
hashie (>= 3.3)
|
28
|
+
multi_json (>= 1.7.5, < 2.0)
|
29
|
+
nokogiri (~> 1.6.3)
|
30
|
+
oauth2
|
31
|
+
hashie (3.4.0)
|
32
|
+
highline (1.7.1)
|
33
|
+
http2 (0.0.29)
|
34
|
+
string-cases
|
35
|
+
jeweler (2.0.1)
|
36
|
+
builder
|
37
|
+
bundler (>= 1.0)
|
38
|
+
git (>= 1.2.5)
|
39
|
+
github_api
|
40
|
+
highline (>= 1.6.15)
|
41
|
+
nokogiri (>= 1.5.10)
|
42
|
+
rake
|
43
|
+
rdoc
|
44
|
+
json (1.8.2)
|
45
|
+
jwt (1.3.0)
|
46
|
+
knjrbfw (0.0.110)
|
47
|
+
datet
|
48
|
+
http2
|
49
|
+
php4r
|
50
|
+
ruby_process
|
51
|
+
tsafe
|
52
|
+
wref
|
53
|
+
mini_portile (0.6.2)
|
54
|
+
multi_json (1.11.0)
|
55
|
+
multi_xml (0.5.5)
|
56
|
+
multipart-post (2.0.0)
|
57
|
+
nokogiri (1.6.6.2)
|
58
|
+
mini_portile (~> 0.6.0)
|
59
|
+
oauth2 (1.0.0)
|
60
|
+
faraday (>= 0.8, < 0.10)
|
61
|
+
jwt (~> 1.0)
|
62
|
+
multi_json (~> 1.3)
|
63
|
+
multi_xml (~> 0.5)
|
64
|
+
rack (~> 1.2)
|
65
|
+
php4r (0.0.4)
|
66
|
+
datet
|
67
|
+
http2
|
68
|
+
string-strtr
|
69
|
+
rack (1.6.0)
|
70
|
+
rake (10.4.2)
|
71
|
+
rdoc (3.12.2)
|
72
|
+
json (~> 1.4)
|
73
|
+
rspec (2.8.0)
|
74
|
+
rspec-core (~> 2.8.0)
|
75
|
+
rspec-expectations (~> 2.8.0)
|
76
|
+
rspec-mocks (~> 2.8.0)
|
77
|
+
rspec-core (2.8.0)
|
78
|
+
rspec-expectations (2.8.0)
|
79
|
+
diff-lcs (~> 1.1.2)
|
80
|
+
rspec-mocks (2.8.0)
|
81
|
+
ruby_process (0.0.9)
|
82
|
+
tsafe
|
83
|
+
wref
|
84
|
+
simplecov (0.9.2)
|
85
|
+
docile (~> 1.1.0)
|
86
|
+
multi_json (~> 1.0)
|
87
|
+
simplecov-html (~> 0.9.0)
|
88
|
+
simplecov-html (0.9.0)
|
89
|
+
sqlite3 (1.3.10)
|
90
|
+
string-cases (0.0.0)
|
91
|
+
string-strtr (0.0.3)
|
92
|
+
thread_safe (0.3.4)
|
93
|
+
tsafe (0.0.11)
|
94
|
+
wref (0.0.6)
|
95
|
+
|
96
|
+
PLATFORMS
|
97
|
+
ruby
|
98
|
+
|
99
|
+
DEPENDENCIES
|
100
|
+
baza (~> 0.0.15)
|
101
|
+
bundler (~> 1.0)
|
102
|
+
codeclimate-test-reporter
|
103
|
+
jeweler (~> 2.0.1)
|
104
|
+
rdoc (~> 3.12)
|
105
|
+
rspec (~> 2.8.0)
|
106
|
+
sqlite3
|
107
|
+
string-cases
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 kaspernj
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
[![Build Status](https://api.shippable.com/projects/5506810c5ab6cc13529b84bf/badge?branchName=master)](https://app.shippable.com/projects/5506810c5ab6cc13529b84bf/builds/latest)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/kaspernj/baza_models/badges/gpa.svg)](https://codeclimate.com/github/kaspernj/baza_models)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/kaspernj/baza_models/badges/coverage.svg)](https://codeclimate.com/github/kaspernj/baza_models)
|
4
|
+
|
5
|
+
# baza_models
|
6
|
+
|
7
|
+
Description goes here.
|
8
|
+
|
9
|
+
## Contributing to baza_models
|
10
|
+
|
11
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
12
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
13
|
+
* Fork the project.
|
14
|
+
* Start a feature/bugfix branch.
|
15
|
+
* Commit and push until you are happy with your contribution.
|
16
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
17
|
+
* 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.
|
18
|
+
|
19
|
+
## Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2015 kaspernj. See LICENSE.txt for
|
22
|
+
further details.
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "baza_models"
|
18
|
+
gem.homepage = "http://github.com/kaspernj/baza_models"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{ActiveRecord like models for the Baza database framework}
|
21
|
+
gem.description = %Q{ActiveRecord like models for the Baza database framework}
|
22
|
+
gem.email = "k@spernj.org"
|
23
|
+
gem.authors = ["kaspernj"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Code coverage detail"
|
35
|
+
task :simplecov do
|
36
|
+
ENV['COVERAGE'] = "true"
|
37
|
+
Rake::Task['spec'].execute
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rdoc/task'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "baza_models #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class BazaModels::Errors
|
2
|
+
class InvalidRecord < RuntimeError; end
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@errors = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(attribute_name, error)
|
9
|
+
@errors[attribute_name] ||= []
|
10
|
+
@errors[attribute_name] << error
|
11
|
+
end
|
12
|
+
|
13
|
+
def full_messages
|
14
|
+
messages = []
|
15
|
+
|
16
|
+
@errors.each do |attribute_name, errors|
|
17
|
+
messages += errors
|
18
|
+
end
|
19
|
+
|
20
|
+
return messages
|
21
|
+
end
|
22
|
+
|
23
|
+
def empty?
|
24
|
+
full_messages.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def any?
|
28
|
+
full_messages.any?
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
require "string-cases"
|
2
|
+
|
3
|
+
class BazaModels::Model
|
4
|
+
attr_accessor :db
|
5
|
+
attr_reader :changes, :errors
|
6
|
+
|
7
|
+
# Define all callback methods.
|
8
|
+
CALLBACK_TYPES = [:before_create, :after_create, :before_save, :after_save, :before_destroy, :after_destroy,
|
9
|
+
:before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,
|
10
|
+
:before_validation_on_update, :after_validation_on_update]
|
11
|
+
|
12
|
+
CALLBACK_TYPES.each do |callback_type|
|
13
|
+
@@callbacks ||= {}
|
14
|
+
@@callbacks[callback_type] = []
|
15
|
+
callbacks = @@callbacks
|
16
|
+
|
17
|
+
(class << self; self; end).send(:define_method, callback_type) do |method_name, *args, &blk|
|
18
|
+
callbacks[callback_type] << {
|
19
|
+
block: blk,
|
20
|
+
method_name: method_name,
|
21
|
+
args: args
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(data = {})
|
27
|
+
self.class.init_model unless self.class.model_initialized?
|
28
|
+
|
29
|
+
@data = data
|
30
|
+
@changes = {}
|
31
|
+
|
32
|
+
reset_errors
|
33
|
+
|
34
|
+
if @data[:id]
|
35
|
+
@new_record = false
|
36
|
+
else
|
37
|
+
@new_record = true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_record?
|
42
|
+
return @new_record
|
43
|
+
end
|
44
|
+
|
45
|
+
def db
|
46
|
+
return @db if @db
|
47
|
+
return self.class.db
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.db
|
51
|
+
return @db if @db
|
52
|
+
return BazaModels.primary_db
|
53
|
+
end
|
54
|
+
|
55
|
+
def table_name
|
56
|
+
@table_name ||= self.class.table_name
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.table_name
|
60
|
+
@table_name ||= "#{StringCases.camel_to_snake(name)}s"
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.init_model
|
64
|
+
@table = db.tables[table_name]
|
65
|
+
@table.columns.each do |column_name, column|
|
66
|
+
init_attribute_from_column(column)
|
67
|
+
end
|
68
|
+
|
69
|
+
@model_initialized = true
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.init_attribute_from_column(column)
|
73
|
+
column_name = column.name.to_sym
|
74
|
+
|
75
|
+
define_method(column_name) do
|
76
|
+
return @changes.fetch(column_name) if @changes.key?(column_name)
|
77
|
+
return @data.fetch(column_name)
|
78
|
+
end
|
79
|
+
|
80
|
+
define_method("#{column_name}_was") do
|
81
|
+
return @data.fetch(column_name)
|
82
|
+
end
|
83
|
+
|
84
|
+
define_method("#{column_name}=") do |new_value|
|
85
|
+
@changes[column_name] = new_value
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.model_initialized?
|
90
|
+
return @model_initialized
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.validates(attribute_name, args)
|
94
|
+
args.each do |validator_name, validator_args|
|
95
|
+
validator_camel_name = StringCases.snake_to_camel(validator_name)
|
96
|
+
class_name = "#{validator_camel_name}Validator"
|
97
|
+
|
98
|
+
@@validators ||= {}
|
99
|
+
@@validators[attribute_name] ||= []
|
100
|
+
@@validators[attribute_name] << BazaModels::Validators.const_get(class_name).new(attribute_name, args)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def id
|
105
|
+
return @data[:id]
|
106
|
+
end
|
107
|
+
|
108
|
+
def save
|
109
|
+
if valid?
|
110
|
+
new_record = new_record?
|
111
|
+
fire_callbacks(:before_save)
|
112
|
+
|
113
|
+
if new_record
|
114
|
+
fire_callbacks(:before_create)
|
115
|
+
@data[:id] = db.insert(table_name, @data.merge(@changes), return_id: true)
|
116
|
+
else
|
117
|
+
db.update(table_name, @changes, id: id)
|
118
|
+
end
|
119
|
+
|
120
|
+
@changes = {}
|
121
|
+
@new_record = false
|
122
|
+
reload
|
123
|
+
|
124
|
+
fire_callbacks(:after_save)
|
125
|
+
fire_callbacks(:after_create) if new_record
|
126
|
+
|
127
|
+
return true
|
128
|
+
else
|
129
|
+
return false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def save!
|
134
|
+
if save
|
135
|
+
return true
|
136
|
+
else
|
137
|
+
raise BazaModels::Errors::InvalidRecord
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def reload
|
142
|
+
@data = db.select(table_name, {id: id}, limit: 1).fetch
|
143
|
+
@changes = {}
|
144
|
+
return self
|
145
|
+
end
|
146
|
+
|
147
|
+
def destroy
|
148
|
+
if new_record?
|
149
|
+
errors.add(:base, "cannot destroy new record")
|
150
|
+
return false
|
151
|
+
else
|
152
|
+
fire_callbacks(:before_destroy)
|
153
|
+
db.delete(table_name, id: id)
|
154
|
+
fire_callbacks(:after_destroy)
|
155
|
+
return true
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def destroy!
|
160
|
+
raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless destroy
|
161
|
+
end
|
162
|
+
|
163
|
+
def assign_attributes(attributes)
|
164
|
+
@changes.merge!(attributes)
|
165
|
+
end
|
166
|
+
|
167
|
+
def update_attributes(attributes)
|
168
|
+
assign_attributes(attributes)
|
169
|
+
return save
|
170
|
+
end
|
171
|
+
|
172
|
+
def update_attributes!(attributes)
|
173
|
+
unless update_attributes(attributes)
|
174
|
+
raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ")
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def valid?
|
179
|
+
fire_callbacks(:before_validation)
|
180
|
+
|
181
|
+
if new_record?
|
182
|
+
fire_callbacks(:before_validation_on_create)
|
183
|
+
else
|
184
|
+
fire_callbacks(:before_validation_on_update)
|
185
|
+
end
|
186
|
+
|
187
|
+
reset_errors
|
188
|
+
|
189
|
+
merged_data = @data.merge(@changes)
|
190
|
+
merged_data.each do |attribute_name, attribute_value|
|
191
|
+
next unless @@validators.key?(attribute_name)
|
192
|
+
|
193
|
+
@@validators[attribute_name].each do |validator|
|
194
|
+
validator.validate(self, attribute_value)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
fire_callbacks(:after_validation)
|
199
|
+
|
200
|
+
if new_record?
|
201
|
+
fire_callbacks(:after_validation_on_create)
|
202
|
+
else
|
203
|
+
fire_callbacks(:after_validation_on_update)
|
204
|
+
end
|
205
|
+
|
206
|
+
return @errors.empty?
|
207
|
+
end
|
208
|
+
|
209
|
+
protected
|
210
|
+
|
211
|
+
def reset_errors
|
212
|
+
@errors = BazaModels::Errors.new
|
213
|
+
end
|
214
|
+
|
215
|
+
def fire_callbacks(name)
|
216
|
+
if @@callbacks[name]
|
217
|
+
@@callbacks[name].each do |callback_data|
|
218
|
+
if callback_data[:block]
|
219
|
+
callback_data[:block].call(*callback_data[:args])
|
220
|
+
elsif callback_data[:method_name]
|
221
|
+
__send__(callback_data[:method_name], *callback_data[:args])
|
222
|
+
else
|
223
|
+
raise "Didn't know how to perform callbacks for #{name}"
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
data/lib/baza_models.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class BazaModels
|
2
|
+
path = "#{File.realpath(File.dirname(__FILE__))}/baza_models"
|
3
|
+
|
4
|
+
autoload :Errors, "#{path}/errors"
|
5
|
+
autoload :Model, "#{path}/model"
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :primary_db
|
9
|
+
end
|
10
|
+
|
11
|
+
class Validators
|
12
|
+
path = "#{File.realpath(File.dirname(__FILE__))}/baza_models/validators"
|
13
|
+
|
14
|
+
autoload :BaseValidator, "#{path}/base_validator"
|
15
|
+
autoload :PresenceValidator, "#{path}/presence_validator"
|
16
|
+
end
|
17
|
+
end
|
data/shippable.yml
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "BazaModels::Model" do
|
4
|
+
let(:db) { @db }
|
5
|
+
let(:user) { UserTest.new(email: "test@example.com") }
|
6
|
+
|
7
|
+
before do
|
8
|
+
@count ||= 0
|
9
|
+
@count += 1
|
10
|
+
|
11
|
+
require "tempfile"
|
12
|
+
require "baza"
|
13
|
+
require "sqlite3"
|
14
|
+
|
15
|
+
path = Tempfile.new("baza_test").path
|
16
|
+
File.unlink(path) if File.exists?(path)
|
17
|
+
|
18
|
+
@db = Baza::Db.new(type: :sqlite3, path: path)
|
19
|
+
BazaModels.primary_db = @db
|
20
|
+
|
21
|
+
@db.tables.create(:user_tests, {
|
22
|
+
columns: [
|
23
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
24
|
+
{name: :email, type: :varchar}
|
25
|
+
],
|
26
|
+
indexes: [
|
27
|
+
:email
|
28
|
+
]
|
29
|
+
})
|
30
|
+
|
31
|
+
require "test_classes/user_test"
|
32
|
+
end
|
33
|
+
|
34
|
+
after do
|
35
|
+
BazaModels.primary_db = nil
|
36
|
+
|
37
|
+
@db.close
|
38
|
+
path = db.args[:path]
|
39
|
+
File.unlink(path)
|
40
|
+
Thread.current[:baza] = nil
|
41
|
+
@db = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "#email" do
|
45
|
+
user.email.should eq "test@example.com"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "#email=" do
|
49
|
+
user.email = "newemail@example.com"
|
50
|
+
user.email.should eq "newemail@example.com"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "#email_was" do
|
54
|
+
user.email = "newemail@example.com"
|
55
|
+
user.email_was.should eq "test@example.com"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "#changes" do
|
59
|
+
user.email = "newemail@example.com"
|
60
|
+
user.changes.should eq(email: "newemail@example.com")
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#save" do
|
64
|
+
it "inserts a new record when new" do
|
65
|
+
user.new_record?.should eq true
|
66
|
+
user.save
|
67
|
+
user.id.should eq 1
|
68
|
+
user.new_record?.should eq false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#valid?" do
|
73
|
+
it "returns true when valid" do
|
74
|
+
user.valid?.should eq true
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns false when invalid" do
|
78
|
+
user.email = " "
|
79
|
+
user.valid?.should eq false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it "#update_attributes" do
|
84
|
+
user.save!
|
85
|
+
user.update_attributes(email: "newemail@example.com").should eq true
|
86
|
+
user.email.should eq "newemail@example.com"
|
87
|
+
user.update_attributes(email: " ").should eq false
|
88
|
+
end
|
89
|
+
|
90
|
+
it "#before_save, #after_save" do
|
91
|
+
user.before_save_called.should eq nil
|
92
|
+
user.after_save_called.should eq nil
|
93
|
+
user.save!
|
94
|
+
user.before_save_called.should eq 1
|
95
|
+
user.after_save_called.should eq 1
|
96
|
+
user.save!
|
97
|
+
user.before_save_called.should eq 2
|
98
|
+
user.after_save_called.should eq 2
|
99
|
+
end
|
100
|
+
|
101
|
+
it "#before_create, #after_create" do
|
102
|
+
user.before_create_called.should eq nil
|
103
|
+
user.after_create_called.should eq nil
|
104
|
+
user.save!
|
105
|
+
user.before_create_called.should eq 1
|
106
|
+
user.after_create_called.should eq 1
|
107
|
+
user.save!
|
108
|
+
user.before_create_called.should eq 1
|
109
|
+
user.after_create_called.should eq 1
|
110
|
+
end
|
111
|
+
|
112
|
+
it "#before_destroy, #after_destroy" do
|
113
|
+
user.save!
|
114
|
+
user.before_destroy_called.should eq nil
|
115
|
+
user.after_destroy_called.should eq nil
|
116
|
+
user.destroy!
|
117
|
+
user.before_destroy_called.should eq 1
|
118
|
+
user.after_destroy_called.should eq 1
|
119
|
+
end
|
120
|
+
|
121
|
+
it "#before_validation, #after_validation" do
|
122
|
+
user.before_validation_called.should eq nil
|
123
|
+
user.after_validation_called.should eq nil
|
124
|
+
user.valid?
|
125
|
+
user.before_validation_called.should eq 1
|
126
|
+
user.after_validation_called.should eq 1
|
127
|
+
end
|
128
|
+
|
129
|
+
it "#before_validation_on_create, #after_validation_on_create, #before_validation_on_update, #after_validation_on_update" do
|
130
|
+
user.before_validation_on_create_called.should eq nil
|
131
|
+
user.after_validation_on_create_called.should eq nil
|
132
|
+
|
133
|
+
user.before_validation_on_update_called.should eq nil
|
134
|
+
user.after_validation_on_update_called.should eq nil
|
135
|
+
|
136
|
+
user.save!
|
137
|
+
|
138
|
+
user.before_validation_on_create_called.should eq 1
|
139
|
+
user.after_validation_on_create_called.should eq 1
|
140
|
+
|
141
|
+
user.before_validation_on_update_called.should eq nil
|
142
|
+
user.after_validation_on_update_called.should eq nil
|
143
|
+
|
144
|
+
user.update_attributes!(email: "newemail@example.com")
|
145
|
+
|
146
|
+
user.before_validation_on_create_called.should eq 1
|
147
|
+
user.after_validation_on_create_called.should eq 1
|
148
|
+
|
149
|
+
user.before_validation_on_update_called.should eq 1
|
150
|
+
user.after_validation_on_update_called.should eq 1
|
151
|
+
end
|
152
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'baza_models'
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class UserTest < BazaModels::Model
|
2
|
+
validates :email, presence: true
|
3
|
+
|
4
|
+
# Used to test callbacks.
|
5
|
+
BazaModels::Model::CALLBACK_TYPES.each do |callback_type|
|
6
|
+
attr_reader "#{callback_type}_called"
|
7
|
+
__send__(callback_type, :set_callback, callback_type)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def set_callback(callback_type)
|
13
|
+
variable_name = "@#{callback_type}_called"
|
14
|
+
instance_variable_set(variable_name, 0) unless instance_variable_get(variable_name)
|
15
|
+
instance_variable_set(variable_name, instance_variable_get(variable_name) + 1)
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baza_models
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kaspernj
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: baza
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.15
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.15
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: string-cases
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.8.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rdoc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jeweler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.0.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.0.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ActiveRecord like models for the Baza database framework
|
112
|
+
email: k@spernj.org
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- LICENSE.txt
|
117
|
+
- README.md
|
118
|
+
files:
|
119
|
+
- ".document"
|
120
|
+
- ".rspec"
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- VERSION
|
127
|
+
- lib/baza_models.rb
|
128
|
+
- lib/baza_models/errors.rb
|
129
|
+
- lib/baza_models/model.rb
|
130
|
+
- lib/baza_models/validators/base_validator.rb
|
131
|
+
- lib/baza_models/validators/presence_validator.rb
|
132
|
+
- shippable.yml
|
133
|
+
- spec/baza_models/model_spec.rb
|
134
|
+
- spec/baza_models_spec.rb
|
135
|
+
- spec/spec_helper.rb
|
136
|
+
- spec/test_classes/user_test.rb
|
137
|
+
homepage: http://github.com/kaspernj/baza_models
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.4.0
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: ActiveRecord like models for the Baza database framework
|
161
|
+
test_files: []
|