lotus-model 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -1
- data/lib/lotus/model/adapters/abstract.rb +21 -0
- data/lib/lotus/model/adapters/memory/query.rb +1 -1
- data/lib/lotus/model/adapters/sql/console.rb +33 -0
- data/lib/lotus/model/adapters/sql/consoles/mysql.rb +49 -0
- data/lib/lotus/model/adapters/sql/consoles/postgresql.rb +48 -0
- data/lib/lotus/model/adapters/sql/consoles/sqlite.rb +26 -0
- data/lib/lotus/model/adapters/sql_adapter.rb +12 -0
- data/lib/lotus/model/config/adapter.rb +1 -1
- data/lib/lotus/model/configuration.rb +1 -1
- data/lib/lotus/model/mapping/coercer.rb +4 -3
- data/lib/lotus/model/mapping/collection.rb +0 -19
- data/lib/lotus/model/version.rb +1 -1
- data/lotus-model.gemspec +1 -1
- metadata +8 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa3a89ff8d50e481fffb8cbd68c8cc38bb97e52a
|
4
|
+
data.tar.gz: c1205c925a3cadac434c381d8f80dd5a15846544
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 573a4b357ec946e3945e819279fe646f72b22144da46e3d8a1f7adf140d6845e9811fad860bd33cd97afcc5587bac93a470489c73ae7e5355be7e463c453d5cd
|
7
|
+
data.tar.gz: 2d380bd41f0f1d3f934d1d4b74e036126f34f5fc0207a3066549c0ab9f31fd5c930f06ffea5baa081734b9dd02de19a34028e3a51cc3094ba99f1e1e9a260f46
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
# Lotus::Model
|
2
2
|
A persistence layer for Lotus
|
3
3
|
|
4
|
+
## v0.3.0 - 2015-03-23
|
5
|
+
### Added
|
6
|
+
- [Linus Pettersson] Database console
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
- [Alfonso Uceda Pompa] Don't send unwanted null values to the database, while coercing entities
|
10
|
+
- [Jan Lelis] Do not define top-level `Boolean`, because it is already defined by `lotus-utils`
|
11
|
+
- [Vsevolod Romashov] Fix entity class resolving in `Coercer#from_record`
|
12
|
+
- [Jason Harrelson] Add file and line to `instance_eval` in `Coercer` to make backtrace more usable
|
13
|
+
|
4
14
|
## v0.2.4 - 2015-02-20
|
5
15
|
### Fixed
|
6
|
-
- [Luca Guidi] When duplicate the framework don't copy over the original `Lotus::Model` configuration
|
16
|
+
- [Luca Guidi] When duplicate the framework don't copy over the original `Lotus::Model` configuration
|
7
17
|
|
8
18
|
## v0.2.3 - 2015-02-13
|
9
19
|
### Added
|
@@ -12,6 +12,17 @@ module Lotus
|
|
12
12
|
class DatabaseAdapterNotFound < ::StandardError
|
13
13
|
end
|
14
14
|
|
15
|
+
# It's raised when an adapter does not support a feature.
|
16
|
+
#
|
17
|
+
# Example: When we try to get a connection string for the current database
|
18
|
+
# but the adapter has not implemented it.
|
19
|
+
#
|
20
|
+
# @see Lotus::Model::Adapters::Abstract#connection_string
|
21
|
+
#
|
22
|
+
# @since 0.3.0
|
23
|
+
class NotSupportedError < ::StandardError
|
24
|
+
end
|
25
|
+
|
15
26
|
# Abstract adapter.
|
16
27
|
#
|
17
28
|
# An adapter is a concrete implementation that allows a repository to
|
@@ -180,6 +191,16 @@ module Lotus
|
|
180
191
|
def transaction(options = {})
|
181
192
|
raise NotImplementedError
|
182
193
|
end
|
194
|
+
|
195
|
+
# Returns a string which can be executed to start a console suitable
|
196
|
+
# for the configured database.
|
197
|
+
#
|
198
|
+
# @return [String] to be executed to start a database console
|
199
|
+
#
|
200
|
+
# @since 0.3.0
|
201
|
+
def connection_string
|
202
|
+
raise NotSupportedError
|
203
|
+
end
|
183
204
|
end
|
184
205
|
end
|
185
206
|
end
|
@@ -100,7 +100,7 @@ module Lotus
|
|
100
100
|
# .where(framework: 'lotus')
|
101
101
|
def where(condition)
|
102
102
|
column, value = _expand_condition(condition)
|
103
|
-
conditions.push([:where, Proc.new{ find_all{|r| r.fetch(column) == value} }])
|
103
|
+
conditions.push([:where, Proc.new{ find_all{|r| r.fetch(column, nil) == value} }])
|
104
104
|
self
|
105
105
|
end
|
106
106
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Lotus
|
2
|
+
module Model
|
3
|
+
module Adapters
|
4
|
+
module Sql
|
5
|
+
class Console
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegator :console, :connection_string
|
9
|
+
|
10
|
+
def initialize(uri)
|
11
|
+
@uri = URI.parse(uri)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def console
|
17
|
+
case @uri.scheme
|
18
|
+
when 'sqlite'
|
19
|
+
require 'lotus/model/adapters/sql/consoles/sqlite'
|
20
|
+
Consoles::Sqlite.new(@uri)
|
21
|
+
when 'postgres'
|
22
|
+
require 'lotus/model/adapters/sql/consoles/postgresql'
|
23
|
+
Consoles::Postgresql.new(@uri)
|
24
|
+
when 'mysql', 'mysql2'
|
25
|
+
require 'lotus/model/adapters/sql/consoles/mysql'
|
26
|
+
Consoles::Mysql.new(@uri)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
module Lotus
|
3
|
+
module Model
|
4
|
+
module Adapters
|
5
|
+
module Sql
|
6
|
+
module Consoles
|
7
|
+
class Mysql
|
8
|
+
def initialize(uri)
|
9
|
+
@uri = uri
|
10
|
+
end
|
11
|
+
|
12
|
+
def connection_string
|
13
|
+
str = 'mysql'
|
14
|
+
str << host
|
15
|
+
str << database
|
16
|
+
str << port if port
|
17
|
+
str << username if username
|
18
|
+
str << password if password
|
19
|
+
str
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def host
|
25
|
+
" -h #{@uri.host}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def database
|
29
|
+
" -D #{@uri.path.sub(/^\//, '')}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def port
|
33
|
+
" -P #{@uri.port}" if @uri.port
|
34
|
+
end
|
35
|
+
|
36
|
+
def username
|
37
|
+
" -u #{@uri.user}" if @uri.user
|
38
|
+
end
|
39
|
+
|
40
|
+
def password
|
41
|
+
" -p #{@uri.password}" if @uri.password
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
module Lotus
|
3
|
+
module Model
|
4
|
+
module Adapters
|
5
|
+
module Sql
|
6
|
+
module Consoles
|
7
|
+
class Postgresql
|
8
|
+
def initialize(uri)
|
9
|
+
@uri = uri
|
10
|
+
end
|
11
|
+
|
12
|
+
def connection_string
|
13
|
+
configure_password
|
14
|
+
str = 'psql'
|
15
|
+
str << host
|
16
|
+
str << database
|
17
|
+
str << port if port
|
18
|
+
str << username if username
|
19
|
+
str
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def host
|
25
|
+
" -h #{@uri.host}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def database
|
29
|
+
" -d #{@uri.path.sub(/^\//, '')}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def port
|
33
|
+
" -p #{@uri.port}" if @uri.port
|
34
|
+
end
|
35
|
+
|
36
|
+
def username
|
37
|
+
" -U #{@uri.user}" if @uri.user
|
38
|
+
end
|
39
|
+
|
40
|
+
def configure_password
|
41
|
+
ENV['PGPASSWORD'] = @uri.password if @uri.password
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
module Lotus
|
3
|
+
module Model
|
4
|
+
module Adapters
|
5
|
+
module Sql
|
6
|
+
module Consoles
|
7
|
+
class Sqlite
|
8
|
+
def initialize(uri)
|
9
|
+
@uri = uri
|
10
|
+
end
|
11
|
+
|
12
|
+
def connection_string
|
13
|
+
"sqlite3 #{@uri.host}#{database}"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def database
|
19
|
+
Shellwords.escape(@uri.path)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -3,6 +3,7 @@ require 'lotus/model/adapters/implementation'
|
|
3
3
|
require 'lotus/model/adapters/sql/collection'
|
4
4
|
require 'lotus/model/adapters/sql/command'
|
5
5
|
require 'lotus/model/adapters/sql/query'
|
6
|
+
require 'lotus/model/adapters/sql/console'
|
6
7
|
require 'sequel'
|
7
8
|
|
8
9
|
module Lotus
|
@@ -215,6 +216,17 @@ module Lotus
|
|
215
216
|
end
|
216
217
|
end
|
217
218
|
|
219
|
+
# Returns a string which can be executed to start a console suitable
|
220
|
+
# for the configured database, adding the necessary CLI flags, such as
|
221
|
+
# url, password, port number etc.
|
222
|
+
#
|
223
|
+
# @return [String]
|
224
|
+
#
|
225
|
+
# @since 0.3.0
|
226
|
+
def connection_string
|
227
|
+
Sql::Console.new(@uri).connection_string
|
228
|
+
end
|
229
|
+
|
218
230
|
private
|
219
231
|
|
220
232
|
# Returns a collection from the given name.
|
@@ -28,7 +28,7 @@ module Lotus
|
|
28
28
|
# adapter type: :sql, uri: 'postgres://localhost/database'
|
29
29
|
# end
|
30
30
|
#
|
31
|
-
# Lotus::Model.adapter_config
|
31
|
+
# Lotus::Model.configuration.adapter_config
|
32
32
|
# # => Lotus::Model::Config::Adapter(type: :sql, uri: 'postgres://localhost/database')
|
33
33
|
#
|
34
34
|
# By convention, Lotus inflects type to find the adapter class
|
@@ -60,12 +60,14 @@ module Lotus
|
|
60
60
|
if entity.id
|
61
61
|
Hash[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{mapped},Lotus::Model::Mapping::Coercions.#{klass}(entity.#{name})"}.join(',') }]
|
62
62
|
else
|
63
|
-
Hash[
|
63
|
+
Hash[].tap do |record|
|
64
|
+
#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,(klass,mapped)| "value = Lotus::Model::Mapping::Coercions.#{klass}(entity.#{name}); record[:#{mapped}] = value unless value.nil?"}.join('; ') }
|
65
|
+
end
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
67
69
|
def from_record(record)
|
68
|
-
|
70
|
+
::#{ @collection.entity }.new(
|
69
71
|
Hash[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{name},Lotus::Model::Mapping::Coercions.#{klass}(record[:#{mapped}])"}.join(',') }]
|
70
72
|
)
|
71
73
|
end
|
@@ -77,4 +79,3 @@ module Lotus
|
|
77
79
|
end
|
78
80
|
end
|
79
81
|
end
|
80
|
-
|
@@ -35,25 +35,6 @@ module Lotus
|
|
35
35
|
# @see Lotus::Repository
|
36
36
|
REPOSITORY_SUFFIX = 'Repository'.freeze
|
37
37
|
|
38
|
-
# Defines top level constant for attribute usage.
|
39
|
-
#
|
40
|
-
# @since 0.1.0
|
41
|
-
#
|
42
|
-
# @see Lotus::Model::Mapping::Collection#attribute
|
43
|
-
#
|
44
|
-
# @example
|
45
|
-
# require 'lotus/model'
|
46
|
-
#
|
47
|
-
# mapper = Lotus::Model::Mapper.new do
|
48
|
-
# collection :articles do
|
49
|
-
# entity Article
|
50
|
-
#
|
51
|
-
# attribute :published, Boolean
|
52
|
-
# end
|
53
|
-
# end
|
54
|
-
class ::Boolean
|
55
|
-
end
|
56
|
-
|
57
38
|
# @attr_reader name [Symbol] the name of the collection
|
58
39
|
#
|
59
40
|
# @since 0.1.0
|
data/lib/lotus/model/version.rb
CHANGED
data/lotus-model.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
spec.required_ruby_version = '>= 2.0.0'
|
21
21
|
|
22
|
-
spec.add_runtime_dependency 'lotus-utils', '~> 0.
|
22
|
+
spec.add_runtime_dependency 'lotus-utils', '~> 0.4'
|
23
23
|
spec.add_runtime_dependency 'sequel', '~> 4.9'
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lotus-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: lotus-utils
|
@@ -17,20 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '0.
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.3.2
|
20
|
+
version: '0.4'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - "~>"
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: '0.
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.3.2
|
27
|
+
version: '0.4'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: sequel
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,6 +107,10 @@ files:
|
|
113
107
|
- lib/lotus/model/adapters/null_adapter.rb
|
114
108
|
- lib/lotus/model/adapters/sql/collection.rb
|
115
109
|
- lib/lotus/model/adapters/sql/command.rb
|
110
|
+
- lib/lotus/model/adapters/sql/console.rb
|
111
|
+
- lib/lotus/model/adapters/sql/consoles/mysql.rb
|
112
|
+
- lib/lotus/model/adapters/sql/consoles/postgresql.rb
|
113
|
+
- lib/lotus/model/adapters/sql/consoles/sqlite.rb
|
116
114
|
- lib/lotus/model/adapters/sql/query.rb
|
117
115
|
- lib/lotus/model/adapters/sql_adapter.rb
|
118
116
|
- lib/lotus/model/config/adapter.rb
|