static-record 1.0.0 → 1.0.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 +4 -4
- data/lib/static_record.rb +16 -4
- data/lib/static_record/concerns/sqlite_storing_concern.rb +4 -0
- data/lib/static_record/migrations/has_static_record.rb +40 -37
- data/lib/static_record/migrations/railtie.rb +3 -8
- data/lib/static_record/models/query_interface.rb +11 -0
- data/lib/static_record/models/query_interface/conditioners.rb +28 -0
- data/lib/static_record/models/query_interface/retrievers.rb +75 -0
- data/lib/static_record/models/query_interface/search_modifiers.rb +29 -0
- data/lib/static_record/models/query_interface/sql_helpers.rb +23 -0
- data/lib/static_record/models/relation.rb +1 -1
- data/lib/static_record/version.rb +1 -1
- metadata +6 -2
- data/lib/static_record/models/predicates.rb +0 -125
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7da53ed48dde6c544df166fad92cba19ba41114a
|
4
|
+
data.tar.gz: eb494a5eab6d82746d0654fcddc20b2d5d6cfc8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eec734004a50419ddedfd06c5d8c44432f73ac58cc7e3280407ce95ce775a559d9e98ae2ec618d04293c58dfb15afa3ad5a8efd80a5147e62f5900750d228eb4
|
7
|
+
data.tar.gz: ebee3f534fc292c7f80c9f0d7e0f80aca269f4e02e336198670665d0468c7f628e8fdea2ebd39021d871d4da36dc302f4727c861c41e13d8641ebb2f59b9c0e2
|
data/lib/static_record.rb
CHANGED
@@ -4,14 +4,26 @@ require 'static_record/exceptions'
|
|
4
4
|
require 'static_record/concerns/query_building_concern'
|
5
5
|
require 'static_record/concerns/sqlite_storing_concern'
|
6
6
|
|
7
|
-
require 'static_record/models/
|
7
|
+
require 'static_record/models/query_interface/conditioners'
|
8
|
+
require 'static_record/models/query_interface/retrievers'
|
9
|
+
require 'static_record/models/query_interface/search_modifiers'
|
10
|
+
require 'static_record/models/query_interface/sql_helpers'
|
11
|
+
require 'static_record/models/query_interface'
|
12
|
+
|
8
13
|
require 'static_record/models/querying'
|
9
14
|
require 'static_record/models/relation'
|
10
15
|
require 'static_record/models/base'
|
11
16
|
|
12
|
-
require 'static_record/migrations/schema'
|
13
|
-
require 'static_record/migrations/railtie'
|
14
17
|
require 'static_record/migrations/has_static_record'
|
15
18
|
|
16
|
-
module
|
19
|
+
# This ClassMethods module will be extended by ActiveRecord
|
20
|
+
module StaticRecord
|
21
|
+
module ClassMethods # :nodoc:
|
22
|
+
def has_static_record(name, options = nil)
|
23
|
+
HasStaticRecord.define_on(self, name, options || {})
|
24
|
+
end
|
25
|
+
end
|
17
26
|
end
|
27
|
+
|
28
|
+
require 'static_record/migrations/schema'
|
29
|
+
require 'static_record/migrations/railtie'
|
@@ -45,6 +45,10 @@ module StaticRecord
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def insert_into_database(db, record, index, cols)
|
48
|
+
# TODO: get attributes without instantiating the record
|
49
|
+
# that currently forces to declare base file (ex: badge.rb) columns
|
50
|
+
# at the end of file, without what Badge instance methods are not defined
|
51
|
+
# yet during BadgeOne#initialize
|
48
52
|
attrs = record.constantize.new.attributes
|
49
53
|
# id, klass
|
50
54
|
sqlized = [index.to_s, "'#{record}'"]
|
@@ -1,52 +1,55 @@
|
|
1
1
|
module StaticRecord
|
2
2
|
# Provides has_static_record when included
|
3
|
-
|
4
|
-
def self.
|
5
|
-
|
3
|
+
class HasStaticRecord
|
4
|
+
def self.define_on(klass, name, options)
|
5
|
+
new(klass, name, options).define
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
define_getter(table_name, options)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
8
|
+
def initialize(klass, name, options)
|
9
|
+
@klass = klass
|
10
|
+
@name = name
|
11
|
+
@options = options
|
12
|
+
end
|
18
13
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
superklass = static_record.class.superclass
|
14
|
+
def define
|
15
|
+
define_getter
|
16
|
+
define_setter
|
17
|
+
end
|
24
18
|
|
25
|
-
|
26
|
-
err = "Record must be an instance of #{options[:class_name]}, got #{superklass}"
|
27
|
-
raise ClassError, err
|
28
|
-
end
|
19
|
+
private
|
29
20
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
def define_getter
|
22
|
+
name = @name
|
23
|
+
options = @options
|
24
|
+
@klass.send :define_method, @name do
|
25
|
+
record_type = send(:"#{name}_static_record_type")
|
26
|
+
return nil unless record_type
|
34
27
|
|
35
|
-
|
36
|
-
|
28
|
+
options[:class_name] ||= name.to_s.camelize
|
29
|
+
# eager loading may be disabled, initialize parent class
|
30
|
+
superklass = options[:class_name].constantize
|
31
|
+
superklass.find_by(klass: record_type)
|
37
32
|
end
|
33
|
+
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
35
|
+
def define_setter
|
36
|
+
name = @name
|
37
|
+
options = @options
|
38
|
+
@klass.send :define_method, "#{@name}=" do |static_record|
|
39
|
+
options[:class_name] ||= name.to_s.camelize
|
40
|
+
superklass = static_record.class.superclass
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
superklass.find_by(klass: record_type)
|
42
|
+
unless superklass.to_s == options[:class_name]
|
43
|
+
err = "Record must be an instance of #{options[:class_name]}, got #{superklass}"
|
44
|
+
raise ClassError, err
|
49
45
|
end
|
46
|
+
|
47
|
+
unless superklass.pkey
|
48
|
+
err = "No primary key has been defined for #{superklass.class}"
|
49
|
+
raise NoPrimaryKey, err
|
50
|
+
end
|
51
|
+
|
52
|
+
send(:"#{name}_static_record_type=", static_record.class.name)
|
50
53
|
end
|
51
54
|
end
|
52
55
|
end
|
@@ -2,14 +2,9 @@ module StaticRecord
|
|
2
2
|
class Railtie < Rails::Railtie # :nodoc:
|
3
3
|
initializer 'static_record.insert_into_active_record' do |_app|
|
4
4
|
ActiveSupport.on_load :active_record do
|
5
|
-
StaticRecord::
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def self.insert
|
10
|
-
if defined?(ActiveRecord)
|
11
|
-
ActiveRecord::Base.send(:include, StaticRecord::Schema)
|
12
|
-
ActiveRecord::Base.send(:include, StaticRecord::HasStaticRecord)
|
5
|
+
send(:extend, StaticRecord::ClassMethods)
|
6
|
+
send(:include, StaticRecord::Schema)
|
7
|
+
ActiveSupport.run_load_hooks(:static_record, StaticRecord::Base)
|
13
8
|
end
|
14
9
|
end
|
15
10
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module StaticRecord
|
2
|
+
# Contains ActiveRecord-like query interface methods
|
3
|
+
module QueryInterface
|
4
|
+
module Interface # :nodoc:
|
5
|
+
include StaticRecord::QueryInterface::Conditioners
|
6
|
+
include StaticRecord::QueryInterface::Retrievers
|
7
|
+
include StaticRecord::QueryInterface::SearchModifiers
|
8
|
+
include StaticRecord::QueryInterface::SqlHelpers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module StaticRecord
|
2
|
+
# Contains ActiveRecord-like query interface methods
|
3
|
+
module QueryInterface
|
4
|
+
# Contains conditioners
|
5
|
+
module Conditioners
|
6
|
+
private
|
7
|
+
|
8
|
+
def where(query = nil, *params)
|
9
|
+
params = [params] unless params.is_a?(Array) || params.is_a?(Hash)
|
10
|
+
params = params.first if params.size == 1 && params[0].is_a?(Hash)
|
11
|
+
add_subclause({ q: query }, params) if query
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def not(query, *params)
|
16
|
+
params = [params] unless params.is_a?(Array) || params.is_a?(Hash)
|
17
|
+
params = params.first if params.size == 1 && params[0].is_a?(Hash)
|
18
|
+
add_subclause({ q: query, operator: :not_eq }, params)
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def or
|
23
|
+
@chain = :or
|
24
|
+
self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module StaticRecord
|
2
|
+
# Contains ActiveRecord-like query interface methods
|
3
|
+
module QueryInterface
|
4
|
+
# Contains retrievers
|
5
|
+
module Retrievers
|
6
|
+
private
|
7
|
+
|
8
|
+
def to_a
|
9
|
+
exec_request
|
10
|
+
end
|
11
|
+
|
12
|
+
def all
|
13
|
+
to_a
|
14
|
+
end
|
15
|
+
|
16
|
+
def find(value)
|
17
|
+
raise StaticRecord::NoPrimaryKey, 'No primary key have been set' if @primary_key.nil?
|
18
|
+
unless value.is_a?(Array)
|
19
|
+
@result_type = :record
|
20
|
+
@sql_limit = 1
|
21
|
+
end
|
22
|
+
add_subclause(q: { :"#{@primary_key}" => value })
|
23
|
+
res = to_a
|
24
|
+
|
25
|
+
unless @only_sql
|
26
|
+
case @result_type
|
27
|
+
when :array
|
28
|
+
if res.size != value.size
|
29
|
+
err = "Couldn't find all #{@store.singularize.capitalize} "\
|
30
|
+
"with '#{@primary_key}' IN #{value}"
|
31
|
+
raise StaticRecord::RecordNotFound, err
|
32
|
+
end
|
33
|
+
when :record
|
34
|
+
if res.nil?
|
35
|
+
err = "Couldn't find #{@store.singularize.capitalize} "\
|
36
|
+
"with '#{@primary_key}'=#{value}"
|
37
|
+
raise StaticRecord::RecordNotFound, err
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
res
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_by(query)
|
46
|
+
add_subclause(q: query)
|
47
|
+
take(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
def take(amount = 1)
|
51
|
+
@sql_limit = amount
|
52
|
+
@result_type = :record if amount == 1
|
53
|
+
to_a
|
54
|
+
end
|
55
|
+
|
56
|
+
def first(amount = 1)
|
57
|
+
@order_by << { :"#{@primary_key}" => :asc } if @primary_key && @order_by.empty?
|
58
|
+
take(amount)
|
59
|
+
end
|
60
|
+
|
61
|
+
def last(amount = 1)
|
62
|
+
if !@primary_key && @order_by.empty?
|
63
|
+
cnt = self.class.new(self, store: @store, primary_key: @primary_key).no_sql.send(:count)
|
64
|
+
@sql_offset = [cnt - amount, 0].max
|
65
|
+
res = take(amount)
|
66
|
+
else
|
67
|
+
@order_by << { :"#{@primary_key}" => :desc } if @order_by.empty?
|
68
|
+
res = take(amount)
|
69
|
+
res.reverse! if res.is_a?(Array)
|
70
|
+
end
|
71
|
+
res
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module StaticRecord
|
2
|
+
# Contains ActiveRecord-like query interface methods
|
3
|
+
module QueryInterface
|
4
|
+
# Contains search modifiers
|
5
|
+
module SearchModifiers
|
6
|
+
private
|
7
|
+
|
8
|
+
def limit(amount)
|
9
|
+
@sql_limit = amount
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def offset(amount)
|
14
|
+
@sql_offset = amount
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def order(ord)
|
19
|
+
@order_by << ord
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def count
|
24
|
+
@columns = 'COUNT(*)'
|
25
|
+
exec_request(:integer)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module StaticRecord
|
2
|
+
# Contains ActiveRecord-like query interface methods
|
3
|
+
module QueryInterface
|
4
|
+
# Contains SQL helpers
|
5
|
+
module SqlHelpers
|
6
|
+
private
|
7
|
+
|
8
|
+
def to_sql
|
9
|
+
build_query
|
10
|
+
end
|
11
|
+
|
12
|
+
def see_sql_of
|
13
|
+
@only_sql = true
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def no_sql
|
18
|
+
@only_sql = false
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
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.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Chevalier
|
@@ -152,7 +152,11 @@ files:
|
|
152
152
|
- lib/static_record/migrations/railtie.rb
|
153
153
|
- lib/static_record/migrations/schema.rb
|
154
154
|
- lib/static_record/models/base.rb
|
155
|
-
- lib/static_record/models/
|
155
|
+
- lib/static_record/models/query_interface.rb
|
156
|
+
- lib/static_record/models/query_interface/conditioners.rb
|
157
|
+
- lib/static_record/models/query_interface/retrievers.rb
|
158
|
+
- lib/static_record/models/query_interface/search_modifiers.rb
|
159
|
+
- lib/static_record/models/query_interface/sql_helpers.rb
|
156
160
|
- lib/static_record/models/querying.rb
|
157
161
|
- lib/static_record/models/relation.rb
|
158
162
|
- lib/static_record/version.rb
|
@@ -1,125 +0,0 @@
|
|
1
|
-
module StaticRecord
|
2
|
-
# Contains ActiveRecord-like query predicates
|
3
|
-
module Predicates
|
4
|
-
private
|
5
|
-
|
6
|
-
def where(query = nil, *params)
|
7
|
-
params = [params] unless params.is_a?(Array) || params.is_a?(Hash)
|
8
|
-
params = params.first if params.size == 1 && params[0].is_a?(Hash)
|
9
|
-
add_subclause({ q: query }, params) if query
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
def not(query, *params)
|
14
|
-
params = [params] unless params.is_a?(Array) || params.is_a?(Hash)
|
15
|
-
params = params.first if params.size == 1 && params[0].is_a?(Hash)
|
16
|
-
add_subclause({ q: query, operator: :not_eq }, params)
|
17
|
-
self
|
18
|
-
end
|
19
|
-
|
20
|
-
def or
|
21
|
-
@chain = :or
|
22
|
-
self
|
23
|
-
end
|
24
|
-
|
25
|
-
def all
|
26
|
-
to_a
|
27
|
-
end
|
28
|
-
|
29
|
-
def find(value)
|
30
|
-
raise StaticRecord::NoPrimaryKey, 'No primary key have been set' if @primary_key.nil?
|
31
|
-
unless value.is_a?(Array)
|
32
|
-
@result_type = :record
|
33
|
-
@sql_limit = 1
|
34
|
-
end
|
35
|
-
add_subclause(q: { :"#{@primary_key}" => value })
|
36
|
-
res = to_a
|
37
|
-
|
38
|
-
unless @only_sql
|
39
|
-
case @result_type
|
40
|
-
when :array
|
41
|
-
if res.size != value.size
|
42
|
-
err = "Couldn't find all #{@store.singularize.capitalize} "\
|
43
|
-
"with '#{@primary_key}' IN #{value}"
|
44
|
-
raise StaticRecord::RecordNotFound, err
|
45
|
-
end
|
46
|
-
when :record
|
47
|
-
if res.nil?
|
48
|
-
err = "Couldn't find #{@store.singularize.capitalize} "\
|
49
|
-
"with '#{@primary_key}'=#{value}"
|
50
|
-
raise StaticRecord::RecordNotFound, err
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
res
|
56
|
-
end
|
57
|
-
|
58
|
-
def find_by(query)
|
59
|
-
add_subclause(q: query)
|
60
|
-
take(1)
|
61
|
-
end
|
62
|
-
|
63
|
-
def take(amount = 1)
|
64
|
-
@sql_limit = amount
|
65
|
-
@result_type = :record if amount == 1
|
66
|
-
to_a
|
67
|
-
end
|
68
|
-
|
69
|
-
def first(amount = 1)
|
70
|
-
@order_by << { :"#{@primary_key}" => :asc } if @primary_key && @order_by.empty?
|
71
|
-
take(amount)
|
72
|
-
end
|
73
|
-
|
74
|
-
def last(amount = 1)
|
75
|
-
if !@primary_key && @order_by.empty?
|
76
|
-
cnt = self.class.new(self, store: @store, primary_key: @primary_key).no_sql.send(:count)
|
77
|
-
@sql_offset = [cnt - amount, 0].max
|
78
|
-
res = take(amount)
|
79
|
-
else
|
80
|
-
@order_by << { :"#{@primary_key}" => :desc } if @order_by.empty?
|
81
|
-
res = take(amount)
|
82
|
-
res.reverse! if res.is_a?(Array)
|
83
|
-
end
|
84
|
-
res
|
85
|
-
end
|
86
|
-
|
87
|
-
def limit(amount)
|
88
|
-
@sql_limit = amount
|
89
|
-
self
|
90
|
-
end
|
91
|
-
|
92
|
-
def offset(amount)
|
93
|
-
@sql_offset = amount
|
94
|
-
self
|
95
|
-
end
|
96
|
-
|
97
|
-
def order(ord)
|
98
|
-
@order_by << ord
|
99
|
-
self
|
100
|
-
end
|
101
|
-
|
102
|
-
def count
|
103
|
-
@columns = 'COUNT(*)'
|
104
|
-
exec_request(:integer)
|
105
|
-
end
|
106
|
-
|
107
|
-
def to_sql
|
108
|
-
build_query
|
109
|
-
end
|
110
|
-
|
111
|
-
def see_sql_of
|
112
|
-
@only_sql = true
|
113
|
-
self
|
114
|
-
end
|
115
|
-
|
116
|
-
def no_sql
|
117
|
-
@only_sql = false
|
118
|
-
self
|
119
|
-
end
|
120
|
-
|
121
|
-
def to_a
|
122
|
-
exec_request
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|