tzispa_data 0.5.0 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a167ebfaeb66387319ad08e3031931f13d585dbe
4
- data.tar.gz: 8b0b7305323240dab7f2efb0e4243fe323a013d8
2
+ SHA256:
3
+ metadata.gz: dcffc8be8acf0bee44e6f9ac69ddad7734ca600c14a5bcbda7e408f0dbdd4c79
4
+ data.tar.gz: 1563ef54f269562412bce9cf0cbb8ae2b9de758e11cdf02232595665e94280cd
5
5
  SHA512:
6
- metadata.gz: 736fafd5d4d9286ecdbd1d9b16554fba70ae5ad7f2c2ad34367e2403f510c642d53d782363c7a66066b1e139765317c17c524c3b73704f706ac44a8fe72095c4
7
- data.tar.gz: 248f53f9531f75f7cb35428ae7b71121a46fe4c98b133eb0c605be628cc62c94098108c5b29ff3eadd3a215214e8bce19dfb46d8f101d2c88ab6d92f054bef49
6
+ metadata.gz: 8ee5aaf97580453cb3be9c2d6f3e7e5c220d9af6ac4169922f862ebf5c04cd53ad77639347ab4482653c5b89d69fd4fbd18acb6db5854fa1ff49f1923b948c63
7
+ data.tar.gz: d8af5d02961f890ef79911c8e676d5dbf3e05bc1ac82a59bfe815c90669bdc104aeabfbe648c1b7f9c3f10e9f11a267f6676a5119a285457b36966c04e4d351c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  Tzispa Data
2
2
 
3
+ ## v0.6.0
4
+ - repository configuration moved from tzispa gem
5
+ - Repository is now a singletonn
6
+
3
7
  ## v0.4.4
4
8
  - fix missing block param at Transporter import method
5
9
 
data/lib/tzispa/data.rb CHANGED
@@ -3,11 +3,11 @@
3
3
  module Tzispa
4
4
  module Data
5
5
 
6
- require 'tzispa/data/adapter_pool'
7
- require 'tzispa/data/repository'
8
- require 'tzispa/data/transporter'
9
- require 'tzispa/data/entity'
10
- require 'tzispa/data/version'
6
+ autoload :Config, 'tzispa/data/config'
7
+ autoload :AdapterPool, 'tzispa/data/adapter_pool'
8
+ autoload :Repository, 'tzispa/data/repository'
9
+ autoload :TRansporter, 'tzispa/data/transporter'
10
+ autoload :Entity, 'tzispa/data/entity'
11
11
 
12
12
  end
13
13
  end
@@ -34,6 +34,10 @@ module Tzispa
34
34
  end
35
35
  end
36
36
 
37
+ def disconnect
38
+ @pool.each_value(&:disconnect)
39
+ end
40
+
37
41
  def connect(config)
38
42
  Sequel.connect("#{config.adapter}://#{config.database}").tap do |conn|
39
43
  if config.connection_validation
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/config/yaml'
4
+
5
+ module Tzispa
6
+ module Data
7
+
8
+ class Config
9
+ attr_reader :env, :config
10
+
11
+ CONFIG_FILENAME = 'database'
12
+
13
+ def initialize(env)
14
+ @cftime = nil
15
+ @env = env.to_sym
16
+ end
17
+
18
+ def to_h
19
+ load!
20
+ config.to_h[env]&.to_h
21
+ end
22
+
23
+ def load!
24
+ if @cftime.nil?
25
+ @cftime = File.ctime(filename)
26
+ elsif @cftime != File.ctime(filename)
27
+ @config = nil
28
+ @cftime = File.ctime(filename)
29
+ end
30
+ @config ||= Tzispa::Config::Yaml.load(filename)
31
+ end
32
+
33
+ def filename
34
+ Config.filename
35
+ end
36
+
37
+ class << self
38
+ def filename
39
+ @filename ||= "config/#{CONFIG_FILENAME}.yml"
40
+ end
41
+
42
+ def create_default(path)
43
+ hcfg = {}.tap do |cfg|
44
+ cfg['development'] = ''
45
+ cfg['deployment'] = ''
46
+ cfg['test'] = ''
47
+ end
48
+ Yaml.save(hcfg, File.join(path, filename))
49
+ end
50
+
51
+ def add_repository(name, adapter, dbconn)
52
+ hs = YAML.safe_load(File.open(filename))
53
+ hs.each_value do |v|
54
+ v[name] = {
55
+ 'adapter' => adapter,
56
+ 'database' => dbconn,
57
+ 'connection_validation' => 'No',
58
+ 'local' => 'Yes'
59
+ }
60
+ end
61
+ Yaml.save(hs, filename)
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
3
4
  require 'sequel'
4
5
  require 'tzispa_utils'
5
6
  require 'tzispa/data/adapter_pool'
7
+ require 'tzispa/data/config'
6
8
 
7
9
  module Tzispa
8
10
  module Data
@@ -18,62 +20,70 @@ module Tzispa
18
20
  class UnknownModel < DataError; end
19
21
 
20
22
  class Repository
23
+ extend Forwardable
24
+ include Singleton
21
25
  using Tzispa::Utils::TzString
22
26
 
23
- attr_reader :root, :adapters
27
+ attr_reader :adapters, :pool, :config
28
+ def_delegators :@adapters, :disconnect
24
29
 
25
- def initialize(config, root = nil)
26
- @config = config
27
- @root = root || LOCAL_REPO_ROOT
30
+ def initialize
31
+ @config = Config.new(Tzispa::Environment.environment)&.to_h
28
32
  @pool = {}
29
33
  @adapters = AdapterPool.new config
30
34
  end
31
35
 
32
- def [](model, repo_id = nil)
33
- selected_repo = repo_id || adapters.default_repo
34
- raise UnknownModel.new(model, selected_repo) unless pool.key?(selected_repo) &&
35
- pool[selected_repo].key?(model.to_sym)
36
- pool[selected_repo][model.to_sym]
37
- end
36
+ class << self
37
+ def [](model, repo_id = nil)
38
+ repo = active_repo(repo_id)
39
+ raise UnknownModel.new(model, repo) unless known?(model, repo)
40
+ instance.pool[repo][model.to_sym]
41
+ end
38
42
 
39
- def models(repo_id = nil)
40
- pool[repo_id || adapters.default_repo].values
41
- end
43
+ def disconnect
44
+ instance.disconnect
45
+ end
42
46
 
43
- def module_const(repo_id = nil)
44
- selected_repo = repo_id || adapters.default_repo
45
- pool[selected_repo][:__repository_module] ||= repository_module(selected_repo)
46
- end
47
+ def active_repo(repo_id = nil)
48
+ repo_id || instance.adapters.default_repo
49
+ end
50
+
51
+ def known?(model, repo)
52
+ instance.pool.key?(repo) && instance.pool[repo].key?(model.to_sym)
53
+ end
54
+
55
+ def models(repo_id = nil)
56
+ instance.pool[active_repo(repo_id)].values
57
+ end
58
+
59
+ def module_const(repo_id = nil)
60
+ repo = active_repo(repo_id)
61
+ instance.pool[repo][:__repository_module] ||= instance.repository_module(repo)
62
+ end
47
63
 
48
- def load!(domain)
49
- @config.each do |id, cfg|
50
- Mutex.new.synchronize do
51
- pool[id] = {}
52
- load_config_repo(id, cfg)
64
+ def load!(domain)
65
+ instance.config.each do |id, cfg|
66
+ instance.pool[id] = {}
67
+ instance.load_config_repo(id, cfg)
53
68
  domain.include module_const(id)
54
69
  end
55
70
  end
56
- self
57
- end
58
71
 
59
- def register(model_id, model_class, repo_id, config)
60
- unless pool[repo_id][model_id.to_sym]
61
- unless model_class.db == adapters[repo_id]
62
- model_class.db = adapters[repo_id]
63
- if config.caching
64
- model_class.plugin :caching, adapters.cache[repo_id],
65
- ttl: config.ttl || DEFAULT_CACHE_TTL,
66
- ignore_exceptions: true
67
- end
68
- end
69
- pool[repo_id][model_id.to_sym] = model_class
72
+ def register(model_id, model_class, repo_id, config)
73
+ return if known?(model_id, repo_id)
74
+ instance.setup_model(model_class, repo_id, config)
75
+ instance.pool[repo_id][model_id.to_sym] = model_class
70
76
  end
71
- pool[repo_id][model_id.to_sym]
72
77
  end
73
78
 
74
- private
75
-
76
- attr_reader :pool
79
+ def setup_model(model_class, repo_id, config)
80
+ return if model_class.db == adapters[repo_id]
81
+ model_class.db = adapters[repo_id]
82
+ return unless config.caching
83
+ model_class.plugin :caching, adapters.cache[repo_id],
84
+ ttl: config.ttl || DEFAULT_CACHE_TTL,
85
+ ignore_exceptions: true
86
+ end
77
87
 
78
88
  def repository_module(repo_id)
79
89
  rm = @pool[repo_id].first[1].name.split('::')
@@ -83,29 +93,40 @@ module Tzispa
83
93
 
84
94
  def load_config_repo(id, cfg)
85
95
  if cfg.local
86
- load_local_helpers id
87
- load_local_models id, cfg
88
- load_local_entities id
96
+ local_local(id, cfg)
89
97
  else
90
98
  require cfg.gem
91
99
  repo_module = id.to_s.camelize.constantize
92
100
  self.class.include repo_module
93
- self.class.send "load_#{id}", self, id, cfg
101
+ self.class.send "load_#{id}", id, cfg
94
102
  end
95
103
  end
96
104
 
97
- def load_local_models(repo_id, config)
105
+ private
106
+
107
+ def root(cfg)
108
+ cfg.root || LOCAL_REPO_ROOT
109
+ end
110
+
111
+ def local_local(id, cfg)
112
+ local_root = root(cfg)
113
+ load_local_helpers id, local_root
114
+ load_local_models id, cfg, local_root
115
+ load_local_entities id, local_root
116
+ end
117
+
118
+ def load_local_models(repo_id, cfg, root)
98
119
  models_path = "./#{root}/#{repo_id}/model"
99
120
  repo_module = "#{repo_id.to_s.camelize}::Model".constantize
100
121
  Dir["#{models_path}/*.rb"].each do |file|
101
122
  model_id = file.split('/').last.split('.').first
102
123
  require "#{models_path}/#{model_id}"
103
124
  model_class = "#{repo_module}::#{model_id.camelize}".constantize
104
- register model_id, model_class, repo_id, config
125
+ register model_id, model_class, repo_id, cfg
105
126
  end
106
127
  end
107
128
 
108
- def load_local_entities(repo_id)
129
+ def load_local_entities(repo_id, root)
109
130
  entities_path = "./#{root}/#{repo_id}/entity"
110
131
  Dir["#{entities_path}/*.rb"].each do |file|
111
132
  entity_id = file.split('/').last.split('.').first
@@ -113,7 +134,7 @@ module Tzispa
113
134
  end
114
135
  end
115
136
 
116
- def load_local_helpers(repo_id)
137
+ def load_local_helpers(repo_id, root)
117
138
  helpers_path = "./#{root}/#{repo_id}/helpers"
118
139
  Dir["#{helpers_path}/*.rb"].each do |file|
119
140
  helper_id = file.split('/').last.split('.').first
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Data
5
5
 
6
- VERSION = '0.5.0'
6
+ VERSION = '0.6.0'
7
7
  NAME = 'Tzispa Data'
8
8
  GEM_NAME = 'tzispa_data'
9
9
 
data/lib/tzispa_data.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  module Tzispa
4
4
  module Data
5
5
 
6
+ require 'tzispa/data/version'
6
7
  require 'tzispa/data'
7
8
 
8
9
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzispa_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Antonio Piñero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-16 00:00:00.000000000 Z
11
+ date: 2017-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dalli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: sequel
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,19 +39,19 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '5.2'
27
41
  - !ruby/object:Gem::Dependency
28
- name: dalli
42
+ name: tzispa_config
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '2.7'
47
+ version: 0.1.0
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '2.7'
54
+ version: 0.1.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: tzispa_utils
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +77,7 @@ files:
63
77
  - README.md
64
78
  - lib/tzispa/data.rb
65
79
  - lib/tzispa/data/adapter_pool.rb
80
+ - lib/tzispa/data/config.rb
66
81
  - lib/tzispa/data/entity.rb
67
82
  - lib/tzispa/data/repository.rb
68
83
  - lib/tzispa/data/transporter.rb
@@ -88,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
103
  version: '0'
89
104
  requirements: []
90
105
  rubyforge_project:
91
- rubygems_version: 2.6.14
106
+ rubygems_version: 2.7.3
92
107
  signing_key:
93
108
  specification_version: 4
94
109
  summary: Data access for Tzispa framework