tzispa_data 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/README.md +3 -0
- data/lib/tzispa/data.rb +13 -0
- data/lib/tzispa/data/adapter.rb +33 -0
- data/lib/tzispa/data/adapter_pool.rb +33 -0
- data/lib/tzispa/data/entity.rb +41 -0
- data/lib/tzispa/data/repository.rb +85 -0
- data/lib/tzispa/data/version.rb +11 -0
- data/lib/tzispa_data.rb +7 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 81f6d2fc4195aa94996282d8133b1585078c6b9e
|
4
|
+
data.tar.gz: 149267a81eeb8731de1adfe24cb81349c955c683
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 449f4f9285a404adfabac894d5dd59a57fd86eac7241ea5260359634009338d402eaec880f2e5ae0c7fde694370a679089e46183964be9254009472622d3e891
|
7
|
+
data.tar.gz: fe0811d116da497722d464b8aead0efa81a682046e81903c90d593df2bf35593ba232612c2dea65b43e4391d6343793abe0c591d55d36e943a1838067ca1c305
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
data/lib/tzispa/data.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Tzispa
|
5
|
+
module Data
|
6
|
+
|
7
|
+
class AdapterPool
|
8
|
+
include Enumerable
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
def_delegators :@pool, :has_key?, :keys
|
12
|
+
attr_reader :default
|
13
|
+
|
14
|
+
def initialize(config, default=nil)
|
15
|
+
Sequel.default_timezone = :utc
|
16
|
+
Sequel.datetime_class = DateTime
|
17
|
+
Sequel.extension :core_extensions
|
18
|
+
@default = default || config.first[0].to_sym
|
19
|
+
@pool = Hash.new.tap { |hpool|
|
20
|
+
config.each { |key, value|
|
21
|
+
hpool[key.to_sym] = Sequel.connect value.adapter
|
22
|
+
}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def [](name=nil)
|
27
|
+
@pool[name&.to_sym || default]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Tzispa
|
5
|
+
module Data
|
6
|
+
|
7
|
+
class AdapterPool
|
8
|
+
include Enumerable
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
def_delegators :@pool, :has_key?, :keys
|
12
|
+
attr_reader :default
|
13
|
+
|
14
|
+
def initialize(config, default=nil)
|
15
|
+
@default = default || config.first[0].to_sym
|
16
|
+
@pool = Hash.new.tap { |hash|
|
17
|
+
config.each { |key, value|
|
18
|
+
hash[key.to_sym] = Sequel.connect value.adapter
|
19
|
+
}
|
20
|
+
}
|
21
|
+
#@adapter_keys = @adapters.keys
|
22
|
+
Sequel.default_timezone = :utc
|
23
|
+
Sequel.datetime_class = DateTime
|
24
|
+
end
|
25
|
+
|
26
|
+
def [](name=nil)
|
27
|
+
@pool[name&.to_sym || default]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tzispa/utils/string'
|
4
|
+
|
5
|
+
module Tzispa
|
6
|
+
module Data
|
7
|
+
module Entity
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
def entity!
|
14
|
+
@__entity || @__entity = self.class.entity_class.new(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def entity_class
|
19
|
+
class_variable_defined?(:@@__entity_class) ?
|
20
|
+
class_variable_get(:@@__entity_class) :
|
21
|
+
class_variable_set(:@@__entity_class, TzString.constantize("#{self}Entity") )
|
22
|
+
end
|
23
|
+
alias_method :entity, :entity_class
|
24
|
+
end
|
25
|
+
|
26
|
+
#unless model_class.respond_to?(:entity_class!)
|
27
|
+
# model_class.send(:define_singleton_method, :entity_class) {
|
28
|
+
# class_variable_defined?(:@@__entity_class) ?
|
29
|
+
# class_variable_get(:@@__entity_class) :
|
30
|
+
# class_variable_set(:@@__entity_class, TzString.constantize("#{self}Entity") )
|
31
|
+
# }
|
32
|
+
#end
|
33
|
+
#model_class.send(:define_method, :entity!) {
|
34
|
+
# instance_variable_defined?(:@__entity) ?
|
35
|
+
# instance_variable_get(:@__entity) :
|
36
|
+
# instance_variable_set(:@__entity, self.class.entity_class!.new(self))
|
37
|
+
#}
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sequel'
|
4
|
+
require 'tzispa/utils/string'
|
5
|
+
require 'tzispa/data/adapter_pool'
|
6
|
+
|
7
|
+
module Tzispa
|
8
|
+
module Data
|
9
|
+
|
10
|
+
class DataError < StandardError; end;
|
11
|
+
class UnknownAdapter < DataError; end;
|
12
|
+
class UnknownModel < DataError; end;
|
13
|
+
|
14
|
+
|
15
|
+
class Repository
|
16
|
+
|
17
|
+
attr_reader :root, :selected_adapter
|
18
|
+
|
19
|
+
LOCAL_REPO_ROOT = :repository
|
20
|
+
|
21
|
+
def initialize(config, root = LOCAL_REPO_ROOT)
|
22
|
+
@config = config
|
23
|
+
@root = root
|
24
|
+
@pool = Hash.new
|
25
|
+
@adapters = AdapterPool.new config
|
26
|
+
@selected_adapter = @adapters.default
|
27
|
+
end
|
28
|
+
|
29
|
+
def use(repo_id)
|
30
|
+
raise UnknownAdapter.new("The '#{adapter}' adapter does not exists") unless @adapters.has_key? repo_id
|
31
|
+
@selected_adapter = repo_id
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](model)
|
35
|
+
raise UnknownModel.new("The '#{model}' model does not exists in the adapter '#{selected_adapter}'") unless @pool.has_key? self.class.key(model, selected_adapter)
|
36
|
+
@pool[self.class.key(model.to_sym, selected_adapter)]
|
37
|
+
end
|
38
|
+
|
39
|
+
def load!
|
40
|
+
@config.each { |id, cfg|
|
41
|
+
Mutex.new.synchronize {
|
42
|
+
Sequel::Model.db = @adapters[id]
|
43
|
+
if cfg.local
|
44
|
+
build_local_repo id, cfg
|
45
|
+
else
|
46
|
+
require cfg.gem
|
47
|
+
repo_mod, build_method = cfg.register.split('#')
|
48
|
+
(TzString.constantize repo_mod).send build_method, self, id, cfg
|
49
|
+
end
|
50
|
+
}
|
51
|
+
}
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def register(model_id, model_class, repo_id, config)
|
56
|
+
model_class.db = @adapters[repo_id]
|
57
|
+
config.extensions.split(',').each { |ext|
|
58
|
+
model_class.db.extension ext.to_sym
|
59
|
+
} if config.respond_to? :extensions
|
60
|
+
@pool[self.class.key(model_id, repo_id)] = model_class
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def build_local_repo(repo_id, config)
|
66
|
+
Dir["./#{root.to_s.downcase}/#{repo_id}/*.rb"].each { |file|
|
67
|
+
model_id = file.split('/').last.split('.').first
|
68
|
+
require local_model_source(model_id, repo_id)
|
69
|
+
model_class = TzString.constantize "Repository::#{TzString.camelize repo_id}::#{TzString.camelize model_id}"
|
70
|
+
register model_id, model_class, repo_id, config
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def local_model_source(model, repo_id)
|
75
|
+
"./#{root.to_s.downcase}/#{repo_id}/#{model}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.key(model_id, repo_id)
|
79
|
+
"#{model_id}@#{repo_id}".to_sym
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
data/lib/tzispa_data.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tzispa_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juan Antonio Piñero
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.31'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.31'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tzispa_utils
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
41
|
+
description: Data access layer for Tzispa
|
42
|
+
email:
|
43
|
+
- japinero@area-integral.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- README.md
|
50
|
+
- lib/tzispa/data.rb
|
51
|
+
- lib/tzispa/data/adapter.rb
|
52
|
+
- lib/tzispa/data/adapter_pool.rb
|
53
|
+
- lib/tzispa/data/entity.rb
|
54
|
+
- lib/tzispa/data/repository.rb
|
55
|
+
- lib/tzispa/data/version.rb
|
56
|
+
- lib/tzispa_data.rb
|
57
|
+
homepage: https://www.area-integral.com
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.3'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.5.1
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Data access for Tzispa framework
|
81
|
+
test_files: []
|