static-record 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +16 -1
- data/lib/static_record/exceptions.rb +1 -0
- data/lib/static_record/models/base.rb +27 -1
- data/lib/static_record/models/query_interface/search_modifiers.rb +1 -1
- data/lib/static_record/version.rb +1 -1
- data/spec/models/default_value_spec.rb +20 -0
- data/spec/models/relation_spec.rb +11 -11
- data/spec/test_app/app/models/article.rb +18 -1
- data/spec/test_app/app/models/articles/article_five.rb +5 -0
- data/spec/test_app/app/models/articles/article_four.rb +1 -0
- data/spec/test_app/app/models/articles/article_one.rb +1 -0
- data/spec/test_app/app/models/articles/article_three.rb +1 -0
- data/spec/test_app/app/models/articles/article_two.rb +1 -0
- data/spec/test_app/app/models/badge.rb +6 -0
- data/spec/test_app/app/models/badges/badge_one.rb +3 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d06c5fc59b117a47f86a218489bd6d95f68cfbe
|
4
|
+
data.tar.gz: 68da7ab138c5dbd25db2cbd359acc30296f0eb16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c2535dc8d88779a4d1489dc21dfd8a3c00ef13ff6c7d98869b1c87eb30c68edba56d9d01529cc17f582e946969fff2ff26858d38cdf7e5c37e1344febc8b1b1
|
7
|
+
data.tar.gz: 46ccc4996e2c053e29e153cb8101c44b3aaa58e5a6b4cf1e14a37404f5f36cf8bf24aea4bc1d5224239b411bc7817f891b03a26a5f494a7a696c7262bd6b68d5
|
data/README.rdoc
CHANGED
@@ -36,10 +36,24 @@ Create your base class inheriting from StaticRecord::Base.
|
|
36
36
|
# .find will only be available if a primary key is defined
|
37
37
|
primary_key :name
|
38
38
|
|
39
|
+
# You can specify default values for each column of your static records
|
40
|
+
# Just create a method with name: default_<attribute_name>
|
41
|
+
def default_author
|
42
|
+
'Default author'
|
43
|
+
end
|
44
|
+
|
45
|
+
# You can override any attribute value
|
46
|
+
# Just create a method with name: override_<attribute_name>
|
47
|
+
def override_cover(cover)
|
48
|
+
return cover if cover.to_s.include?('/')
|
49
|
+
Rails.root.join('public', 'default_cover_path', cover)
|
50
|
+
end
|
51
|
+
|
39
52
|
# Specify which "article" attributes can be queried over with their types
|
40
53
|
columns name: :string,
|
41
54
|
author: :string,
|
42
|
-
rank: :integer
|
55
|
+
rank: :integer,
|
56
|
+
cover: :string
|
43
57
|
end
|
44
58
|
|
45
59
|
At each application startup, an SQLite3 database will be created to store this class' children.
|
@@ -59,6 +73,7 @@ Create has many child class as you want.
|
|
59
73
|
attribute :name, 'Article One'
|
60
74
|
attribute :author, 'The author'
|
61
75
|
attribute :rank, 2
|
76
|
+
attribute :cover, Rails.root.join('public', 'articles', 'one.jpg')
|
62
77
|
|
63
78
|
# Your class can be used as any other Ruby class
|
64
79
|
def initialize
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module StaticRecord
|
2
2
|
class RecordNotFound < RuntimeError; end
|
3
3
|
class ReservedAttributeName < RuntimeError; end
|
4
|
+
class MissingAttribute < RuntimeError; end
|
4
5
|
class NoPrimaryKey < RuntimeError; end
|
5
6
|
class UnkownType < RuntimeError; end
|
6
7
|
class ClassError < RuntimeError; end
|
@@ -67,7 +67,7 @@ module StaticRecord
|
|
67
67
|
next if RESERVED_ATTRIBUTES.include?(var)
|
68
68
|
attrs[var.to_s.sub(/@@/, '').to_sym] = klass.class_variable_get(var)
|
69
69
|
end
|
70
|
-
attrs
|
70
|
+
default_attributes(attrs)
|
71
71
|
end
|
72
72
|
|
73
73
|
def self.columns(cols)
|
@@ -78,5 +78,31 @@ module StaticRecord
|
|
78
78
|
def self.get_column_type(column)
|
79
79
|
class_variable_get(:@@_columns)[column]
|
80
80
|
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def default_attributes(attrs)
|
85
|
+
klass = self.class
|
86
|
+
klass.class_variable_get(:@@_columns).each do |column, _ctype|
|
87
|
+
column_defined = attrs.key?(column)
|
88
|
+
unless column_defined || default?(column)
|
89
|
+
err = "You must define attribute '#{column}' for #{klass.name}"
|
90
|
+
raise StaticRecord::MissingAttribute, err
|
91
|
+
end
|
92
|
+
|
93
|
+
v = column_defined ? attrs[column] : klass.send(:"default_#{column}")
|
94
|
+
v = klass.send(:"override_#{column}", v) if override?(column)
|
95
|
+
attrs[column] = v
|
96
|
+
end
|
97
|
+
attrs
|
98
|
+
end
|
99
|
+
|
100
|
+
def default?(column)
|
101
|
+
self.class.respond_to?("default_#{column}")
|
102
|
+
end
|
103
|
+
|
104
|
+
def override?(column)
|
105
|
+
self.class.respond_to?("override_#{column}")
|
106
|
+
end
|
81
107
|
end
|
82
108
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rails_helper'
|
3
|
+
|
4
|
+
RSpec.describe StaticRecord::Base, :type => :module do
|
5
|
+
it 'calls default_<column> method when attribute is not defined' do
|
6
|
+
article = Article.find('Article Five')
|
7
|
+
expect(article.author).to eql(Article.default_author)
|
8
|
+
expect(article.rank).to eql(Article.default_rank)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'calls override_<column> method to override attribute when available' do
|
12
|
+
article = Article.find('Article Five')
|
13
|
+
expect(article.cover).to eql(Rails.root.join('public', 'articles', 'cover', 'article_five.jpg'))
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'raises an error when attribute is not defined and no default method exists' do
|
17
|
+
err = "You must define attribute 'description' for BadgeOne"
|
18
|
+
expect { Badge.find('Badge One') }.to raise_error(StaticRecord::MissingAttribute, err)
|
19
|
+
end
|
20
|
+
end
|
@@ -10,7 +10,7 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
10
10
|
|
11
11
|
context '.all' do
|
12
12
|
it 'returns all results' do
|
13
|
-
expect(Article.all.count).to eql(
|
13
|
+
expect(Article.all.count).to eql(5)
|
14
14
|
expect(Article.see_sql_of.all).to eql("SELECT * FROM articles")
|
15
15
|
end
|
16
16
|
end
|
@@ -18,7 +18,7 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
18
18
|
context '.order' do
|
19
19
|
context 'with a symbol' do
|
20
20
|
it 'returns results ordered' do
|
21
|
-
expected = [ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
21
|
+
expected = [ArticleFive, ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
22
22
|
expect(Article.order(:name).all.map(&:class)).to eql(expected)
|
23
23
|
expect(Article.order(:name).to_sql).to eql("SELECT * FROM articles ORDER BY articles.name ASC")
|
24
24
|
end
|
@@ -26,7 +26,7 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
26
26
|
|
27
27
|
context 'with a string' do
|
28
28
|
it 'returns results ordered' do
|
29
|
-
expected = [ArticleTwo, ArticleThree, ArticleOne, ArticleFour]
|
29
|
+
expected = [ArticleTwo, ArticleThree, ArticleOne, ArticleFour, ArticleFive]
|
30
30
|
expect(Article.order("name DESC").all.map(&:class)).to eql(expected)
|
31
31
|
expect(Article.order("name DESC").to_sql).to eql("SELECT * FROM articles ORDER BY name DESC")
|
32
32
|
end
|
@@ -34,13 +34,13 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
34
34
|
|
35
35
|
context 'with a hash' do
|
36
36
|
it 'returns results ordered with one key' do
|
37
|
-
expected = [ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
37
|
+
expected = [ArticleFive, ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
38
38
|
expect(Article.order(name: :asc).all.map(&:class)).to eql(expected)
|
39
39
|
expect(Article.order(name: :asc).to_sql).to eql("SELECT * FROM articles ORDER BY articles.name ASC")
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'returns results ordered with serveral keys' do
|
43
|
-
expected = [ArticleThree, ArticleOne, ArticleTwo, ArticleFour]
|
43
|
+
expected = [ArticleThree, ArticleOne, ArticleTwo, ArticleFour, ArticleFive]
|
44
44
|
expect(Article.order(rank: :asc, name: :desc).all.map(&:class)).to eql(expected)
|
45
45
|
expect(Article.order(rank: :asc, name: :desc).to_sql).to eql("SELECT * FROM articles ORDER BY articles.rank ASC, articles.name DESC")
|
46
46
|
end
|
@@ -48,13 +48,13 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
48
48
|
|
49
49
|
context 'with an array' do
|
50
50
|
it 'returns results ordered with one key' do
|
51
|
-
expected = [ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
51
|
+
expected = [ArticleFive, ArticleFour, ArticleOne, ArticleThree, ArticleTwo]
|
52
52
|
expect(Article.order([:name]).all.map(&:class)).to eql(expected)
|
53
53
|
expect(Article.order([:name]).to_sql).to eql("SELECT * FROM articles ORDER BY articles.name ASC")
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'returns results ordered with several keys' do
|
57
|
-
expected = [ArticleThree, ArticleOne, ArticleFour, ArticleTwo]
|
57
|
+
expected = [ArticleThree, ArticleOne, ArticleFive, ArticleFour, ArticleTwo]
|
58
58
|
expect(Article.order([:rank, :name]).all.map(&:class)).to eql(expected)
|
59
59
|
expect(Article.order([:rank, :name]).to_sql).to eql("SELECT * FROM articles ORDER BY articles.rank ASC, articles.name ASC")
|
60
60
|
end
|
@@ -64,14 +64,14 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
64
64
|
context '.first' do
|
65
65
|
context 'without parameter' do
|
66
66
|
it 'returns first record ordered by primary key' do
|
67
|
-
expect(Article.first.class).to eql(
|
67
|
+
expect(Article.first.class).to eql(ArticleFive)
|
68
68
|
expect(Article.see_sql_of.first).to eql("SELECT * FROM articles ORDER BY articles.name ASC LIMIT 1")
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
context 'with a parameter' do
|
73
73
|
it 'orders records by primary key and returns up to specified number of records from the beginning' do
|
74
|
-
expect(Article.first(2).map(&:class)).to eql([
|
74
|
+
expect(Article.first(2).map(&:class)).to eql([ArticleFive, ArticleFour])
|
75
75
|
expect(Article.see_sql_of.first(2)).to eql("SELECT * FROM articles ORDER BY articles.name ASC LIMIT 2")
|
76
76
|
end
|
77
77
|
end
|
@@ -107,14 +107,14 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
107
107
|
|
108
108
|
context '.limit' do
|
109
109
|
it 'returns up to specified number of records' do
|
110
|
-
expect(Article.limit(2).all.map(&:class)).to eql([
|
110
|
+
expect(Article.limit(2).all.map(&:class)).to eql([ArticleFive, ArticleFour])
|
111
111
|
expect(Article.limit(2).to_sql).to eql("SELECT * FROM articles LIMIT 2")
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
115
|
context '.limit.offset' do
|
116
116
|
it 'returns up to specified number of records with specified offset' do
|
117
|
-
expect(Article.limit(2).offset(1).all.map(&:class)).to eql([
|
117
|
+
expect(Article.limit(2).offset(1).all.map(&:class)).to eql([ArticleFour, ArticleOne])
|
118
118
|
expect(Article.limit(2).offset(1).to_sql).to eql("SELECT * FROM articles LIMIT 2 OFFSET 1")
|
119
119
|
end
|
120
120
|
end
|
@@ -3,8 +3,25 @@ class Article < StaticRecord::Base
|
|
3
3
|
path Rails.root.join('app', 'models', 'articles', '**', '*.rb')
|
4
4
|
primary_key :name
|
5
5
|
|
6
|
+
def self.default_author
|
7
|
+
'Default author'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.default_rank
|
11
|
+
3
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.override_cover(cover)
|
15
|
+
unless cover.to_s.include?('/')
|
16
|
+
folder = Rails.root.join('public', 'articles', 'cover').itself
|
17
|
+
return folder + cover
|
18
|
+
end
|
19
|
+
cover
|
20
|
+
end
|
21
|
+
|
6
22
|
columns name: :string,
|
7
23
|
author: :string,
|
8
24
|
rank: :integer,
|
9
|
-
important: :boolean
|
25
|
+
important: :boolean,
|
26
|
+
cover: :string
|
10
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Chevalier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- spec/helpers/migration_has_been_run.rb
|
165
165
|
- spec/migrations/railtie_spec.rb
|
166
166
|
- spec/models/base_spec.rb
|
167
|
+
- spec/models/default_value_spec.rb
|
167
168
|
- spec/models/querying_spec.rb
|
168
169
|
- spec/models/relation_spec.rb
|
169
170
|
- spec/rails_helper.rb
|
@@ -171,10 +172,13 @@ files:
|
|
171
172
|
- spec/test_app/app/controllers/application_controller.rb
|
172
173
|
- spec/test_app/app/helpers/application_helper.rb
|
173
174
|
- spec/test_app/app/models/article.rb
|
175
|
+
- spec/test_app/app/models/articles/article_five.rb
|
174
176
|
- spec/test_app/app/models/articles/article_four.rb
|
175
177
|
- spec/test_app/app/models/articles/article_one.rb
|
176
178
|
- spec/test_app/app/models/articles/article_three.rb
|
177
179
|
- spec/test_app/app/models/articles/article_two.rb
|
180
|
+
- spec/test_app/app/models/badge.rb
|
181
|
+
- spec/test_app/app/models/badges/badge_one.rb
|
178
182
|
- spec/test_app/app/models/role.rb
|
179
183
|
- spec/test_app/app/models/roles/role_one.rb
|
180
184
|
- spec/test_app/app/models/roles/role_two.rb
|
@@ -227,6 +231,7 @@ test_files:
|
|
227
231
|
- spec/helpers/migration_has_been_run.rb
|
228
232
|
- spec/migrations/railtie_spec.rb
|
229
233
|
- spec/models/base_spec.rb
|
234
|
+
- spec/models/default_value_spec.rb
|
230
235
|
- spec/models/querying_spec.rb
|
231
236
|
- spec/models/relation_spec.rb
|
232
237
|
- spec/rails_helper.rb
|
@@ -234,10 +239,13 @@ test_files:
|
|
234
239
|
- spec/test_app/app/controllers/application_controller.rb
|
235
240
|
- spec/test_app/app/helpers/application_helper.rb
|
236
241
|
- spec/test_app/app/models/article.rb
|
242
|
+
- spec/test_app/app/models/articles/article_five.rb
|
237
243
|
- spec/test_app/app/models/articles/article_four.rb
|
238
244
|
- spec/test_app/app/models/articles/article_one.rb
|
239
245
|
- spec/test_app/app/models/articles/article_three.rb
|
240
246
|
- spec/test_app/app/models/articles/article_two.rb
|
247
|
+
- spec/test_app/app/models/badge.rb
|
248
|
+
- spec/test_app/app/models/badges/badge_one.rb
|
241
249
|
- spec/test_app/app/models/role.rb
|
242
250
|
- spec/test_app/app/models/roles/role_one.rb
|
243
251
|
- spec/test_app/app/models/roles/role_two.rb
|