static-record 1.0.0.pre.2 → 1.0.0.pre.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +19 -10
- data/Rakefile +8 -0
- data/lib/static_record/concerns/sqlite_storing_concern.rb +27 -9
- data/lib/static_record/exceptions.rb +1 -0
- data/lib/static_record/models/base.rb +11 -0
- data/lib/static_record/version.rb +1 -1
- data/spec/models/static_record/base_spec.rb +1 -1
- data/spec/models/static_record/querying_spec.rb +8 -0
- data/spec/test_app/app/models/article.rb +4 -1
- data/spec/test_app/app/models/articles/article_four.rb +1 -1
- data/spec/test_app/app/models/articles/article_one.rb +1 -1
- data/spec/test_app/app/models/articles/article_three.rb +1 -1
- data/spec/test_app/app/models/articles/article_two.rb +1 -1
- metadata +29 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a3706048403361650a6116e13d8e88d6afd574d
|
4
|
+
data.tar.gz: e3576955961ae0c8f612feae91608eb589e718cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22b0f67f4373b06df4555dbbb99464b914414a8c9e6c21d383964959efd17d7d179a560c5f9ec4597e5124b8555dd72c895e7c84f7b12cbfec8f2d15beb1a5c5
|
7
|
+
data.tar.gz: 57fe5f972a3c944fb0ebb17a2cfddc6d53c3c4ba03951e2ca8928a8052c140f3b3fddfe747b51f0d82dd6b97369f68f5fc71ac2fa0079bbe10e9e803d00dabc6
|
data/README.rdoc
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
= StaticRecord
|
2
2
|
|
3
|
+
{<img src="https://badge.fury.io/rb/static-record.svg" alt="Gem Version" />}[https://badge.fury.io/rb/static-record] {<img src="https://codeclimate.com/github/hchevalier/static_record/badges/gpa.svg" />}[https://codeclimate.com/github/hchevalier/static_record] {<img src="https://travis-ci.org/hchevalier/static_record.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/hchevalier/static_record]
|
4
|
+
|
3
5
|
StaticRecord allows you to perform ActiveRecord-like queries over ruby files.
|
4
6
|
|
5
7
|
Those files act as immutable database records that only developers can alter.
|
@@ -10,7 +12,7 @@ You can use it when you need several files inheriting a base class.
|
|
10
12
|
|
11
13
|
Add this to your Gemfile:
|
12
14
|
|
13
|
-
gem '
|
15
|
+
gem 'static-record', require: 'static_record'
|
14
16
|
|
15
17
|
and run the bundle install command.
|
16
18
|
|
@@ -31,15 +33,23 @@ Create your base class inheriting from StaticRecord::Base.
|
|
31
33
|
path Rails.root.join('app', 'models', 'articles', '**', '*.rb')
|
32
34
|
|
33
35
|
# Optionnal, declare which column can be used as the primary key (must be unique)
|
34
|
-
# .find will only be
|
36
|
+
# .find will only be available if a primary key is defined
|
35
37
|
primary_key :name
|
36
38
|
|
37
|
-
# Specify which "article" attributes can be queried over
|
38
|
-
columns
|
39
|
+
# Specify which "article" attributes can be queried over with their types
|
40
|
+
columns name: :string,
|
41
|
+
author: :string,
|
42
|
+
rank: :integer
|
39
43
|
end
|
40
44
|
|
41
45
|
At each application startup, an SQLite3 database will be created to store this class' children.
|
42
46
|
|
47
|
+
Available column types are
|
48
|
+
- :string
|
49
|
+
- :integer
|
50
|
+
- :boolean
|
51
|
+
- :float
|
52
|
+
|
43
53
|
=== Child class
|
44
54
|
|
45
55
|
Create has many child class as you want.
|
@@ -48,7 +58,7 @@ Create has many child class as you want.
|
|
48
58
|
# Define the attributes that will be available for your StaticRecord queries
|
49
59
|
attribute :name, 'Article One'
|
50
60
|
attribute :author, 'The author'
|
51
|
-
attribute :rank,
|
61
|
+
attribute :rank, 2
|
52
62
|
|
53
63
|
# Your class can be used as any other Ruby class
|
54
64
|
def initialize
|
@@ -67,17 +77,17 @@ Create has many child class as you want.
|
|
67
77
|
|
68
78
|
In your code, you can perform queries like this one:
|
69
79
|
|
70
|
-
Article.where(name: 'Article Two').or.where
|
80
|
+
Article.where(name: 'Article Two').or.where('rank >= 2').limit(2).offset(3)
|
71
81
|
|
72
82
|
I tried to implement as many SQL functions wrappers that ActiveRecord provides as I could.
|
73
83
|
|
74
|
-
There is still a lot of work before everything is available, but I
|
84
|
+
There is still a lot of work before everything is available, but I chose to release the 1.0.0.pre nevertheless.
|
75
85
|
|
76
86
|
Here is a full list:
|
77
87
|
* where
|
78
88
|
* supports Hash -> where(name: 'Name', author: 'Author')
|
79
89
|
* supports String -> where("name = 'Name'") or where("name = ?", 'Name') or where("name = :name", name: 'Name')
|
80
|
-
* find (
|
90
|
+
* find (only if a primary key has been set)
|
81
91
|
* find_by
|
82
92
|
* not
|
83
93
|
* or
|
@@ -109,7 +119,6 @@ As StaticRecord is in active development and a full list of feature is already s
|
|
109
119
|
|
110
120
|
Here is what will be available soon:
|
111
121
|
- Better documentation
|
112
|
-
-
|
113
|
-
- Foreign keys
|
122
|
+
- Support for Date, Datetime and Foreign keys
|
114
123
|
- Joins
|
115
124
|
- Generators
|
data/Rakefile
CHANGED
@@ -20,3 +20,11 @@ load 'rails/tasks/engine.rake'
|
|
20
20
|
load 'rails/tasks/statistics.rake'
|
21
21
|
|
22
22
|
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
28
|
+
t.pattern = Dir.glob('../spec/**/*_spec.rb')
|
29
|
+
end
|
30
|
+
task :default => :spec
|
@@ -6,15 +6,15 @@ module StaticRecord
|
|
6
6
|
|
7
7
|
module ClassMethods # :nodoc:
|
8
8
|
def create_store
|
9
|
-
|
9
|
+
cols = class_variable_get(:@@_columns)
|
10
10
|
begin
|
11
11
|
dbname = Rails.root.join('db', "static_#{store}.sqlite3").to_s
|
12
12
|
SQLite3::Database.new(dbname)
|
13
13
|
db = SQLite3::Database.open(dbname)
|
14
14
|
db.execute("DROP TABLE IF EXISTS #{store}")
|
15
|
-
create_table(db,
|
15
|
+
create_table(db, cols)
|
16
16
|
load_records.each_with_index do |record, index|
|
17
|
-
insert_into_database(db, record, index,
|
17
|
+
insert_into_database(db, record, index, cols)
|
18
18
|
end
|
19
19
|
rescue SQLite3::Exception => e
|
20
20
|
puts 'Exception occurred', e
|
@@ -25,16 +25,34 @@ module StaticRecord
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def create_table(db,
|
29
|
-
|
30
|
-
|
28
|
+
def create_table(db, cols)
|
29
|
+
attr_list = []
|
30
|
+
cols.each do |c, v|
|
31
|
+
attr_list << c.to_s + column_type_to_sql(v)
|
32
|
+
end
|
33
|
+
str_attr = attr_list.join(', ')
|
34
|
+
sql = "CREATE TABLE #{store}(id INTEGER PRIMARY KEY, klass TEXT, #{str_attr})"
|
31
35
|
db.execute(sql)
|
32
36
|
end
|
33
37
|
|
34
|
-
def
|
38
|
+
def column_type_to_sql(ctype)
|
39
|
+
case ctype.to_s
|
40
|
+
when 'string'
|
41
|
+
' TEXT'
|
42
|
+
else
|
43
|
+
" #{ctype.to_s.upcase}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def insert_into_database(db, record, index, cols)
|
35
48
|
attrs = record.constantize.new.attributes
|
36
|
-
|
37
|
-
sqlized
|
49
|
+
# id, klass
|
50
|
+
sqlized = [index.to_s, "'#{record}'"]
|
51
|
+
# model's attributes
|
52
|
+
sqlized += cols.map do |name, ctype|
|
53
|
+
ctype == :integer ? attrs[name].to_s : "'#{attrs[name]}'"
|
54
|
+
end
|
55
|
+
|
38
56
|
db.execute("INSERT INTO #{store} VALUES(#{sqlized.join(', ')})")
|
39
57
|
end
|
40
58
|
|
@@ -11,6 +11,13 @@ module StaticRecord
|
|
11
11
|
:@@_store
|
12
12
|
].freeze
|
13
13
|
|
14
|
+
KNOWN_TYPES = [
|
15
|
+
:string,
|
16
|
+
:boolean,
|
17
|
+
:integer,
|
18
|
+
:float
|
19
|
+
].freeze
|
20
|
+
|
14
21
|
def initialize
|
15
22
|
attributes.each do |attr, value|
|
16
23
|
instance_variable_set "@#{attr}", value
|
@@ -67,5 +74,9 @@ module StaticRecord
|
|
67
74
|
class_variable_set('@@_columns', cols)
|
68
75
|
create_store
|
69
76
|
end
|
77
|
+
|
78
|
+
def self.get_column_type(column)
|
79
|
+
class_variable_get(:@@_columns)[column]
|
80
|
+
end
|
70
81
|
end
|
71
82
|
end
|
@@ -12,6 +12,6 @@ RSpec.describe StaticRecord::Base, :type => :model do
|
|
12
12
|
article = Article.find('Article One')
|
13
13
|
expect(article.name).to eql('Article One')
|
14
14
|
expect(article.author).to eql('The author')
|
15
|
-
expect(article.rank).to eql(
|
15
|
+
expect(article.rank).to eql(2)
|
16
16
|
end
|
17
17
|
end
|
@@ -5,4 +5,12 @@ RSpec.describe StaticRecord::Querying, :type => :model do
|
|
5
5
|
it 'delegates requests to StaticRecord::Relation' do
|
6
6
|
expect(Article.where(author: 'The author')).to be_a(StaticRecord::Relation)
|
7
7
|
end
|
8
|
+
|
9
|
+
it 'returns a new StaticRecord::Relation for each method chaining node' do
|
10
|
+
first_node = Article.where(author: 'The author')
|
11
|
+
expect(first_node).to be_a(StaticRecord::Relation)
|
12
|
+
second_node = first_node.where(rank: 3)
|
13
|
+
expect(second_node).to be_a(StaticRecord::Relation)
|
14
|
+
expect(second_node).not_to eql(first_node)
|
15
|
+
end
|
8
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.
|
4
|
+
version: 1.0.0.pre.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Chevalier
|
@@ -28,56 +28,70 @@ dependencies:
|
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.3
|
33
|
+
version: '1.3'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1.3
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 12.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.0.0
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
|
-
- -
|
59
|
+
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.5
|
61
|
+
version: '3.5'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- -
|
66
|
+
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.5
|
68
|
+
version: '3.5'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec-rails
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- -
|
73
|
+
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: 3.5
|
75
|
+
version: '3.5'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- -
|
80
|
+
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: 3.5
|
82
|
+
version: '3.5'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rubocop
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- -
|
87
|
+
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
89
|
version: 0.46.0
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- -
|
94
|
+
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: 0.46.0
|
83
97
|
description: |2
|