key_tree 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88a507ce5dd70323ccb58dacefa9bb7bcbdb9479b977f086c09cfe3409326341
4
- data.tar.gz: 205f90e3467562c846a0139cba358879f4942fc8fa6f91e726af5325f9093baf
3
+ metadata.gz: 173c5891978625d7465eda060527e347d2b027398ee4cd91c295515acd7f0e51
4
+ data.tar.gz: 4cad395ecbe13ad5e515e59c0a8fc9b19328590280623d95d1537358bd7ba821
5
5
  SHA512:
6
- metadata.gz: f9df024317615e4ac92d1b552fc1c8adc51acb3dc9f1af9e1636cbdd2edb1e6db986cbcfb6ff1b00ff974ad91c4c0383be5e1ff1d909a0027745047521d315da
7
- data.tar.gz: d34833616cc2eecb57630715e3a98c3a850c28257b670a22c9b99c5a5481f69c237244198a8e0967b434cdd58adb6da0096aa7449e242d1d069cbb68ec039c0f
6
+ metadata.gz: 8adf5ecf6a524a71d9dfb1989f5a9410a6b50ab63eed51c45abb07a434748343ef4e83e26fbec860876ee32f366ed2d78440eeb5f20257e8b873e37f4a34b502
7
+ data.tar.gz: b45dbebd32d4b54dbe9c38760e362cb41773d400d6859f7913eb052bd86c58a514539bfc58f204e10b119446d757bee089b80fd8cfdb3d0d596b7482fd74a95c
@@ -0,0 +1,21 @@
1
+ module KeyTree
2
+ # Module to manage key tree loaders
3
+ module Loader
4
+ BUILTIN_LOADERS = {
5
+ json: 'JSON',
6
+ yaml: 'YAML', yml: 'YAML'
7
+ }.freeze
8
+
9
+ def self.[](loader_type)
10
+ @loaders ||= BUILTIN_LOADERS.each_with_object({}) do |pair, result|
11
+ type, name = pair
12
+ result[type] = const_get(name) if const_defined?(name)
13
+ end
14
+ @loaders[loader_type]
15
+ end
16
+
17
+ def self.[]=(type, loader_class)
18
+ @loaders[type] = loader_class
19
+ end
20
+ end
21
+ end
data/lib/key_tree.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'key_tree/version'
2
2
  require 'key_tree/tree'
3
3
  require 'key_tree/forest'
4
+ require 'key_tree/loader'
4
5
 
5
6
  # Manage a tree of keys
6
7
  #
@@ -40,10 +41,9 @@ module KeyTree
40
41
  end
41
42
 
42
43
  type, serialization = typed_serialization.flatten
43
-
44
- loader = get_loader(type)
44
+ loader = Loader[type]
45
45
  self[loader.load(serialization)].with_meta_data do |meta_data|
46
- meta_data << { load: { type: type.to_sym,
46
+ meta_data << { load: { type: type,
47
47
  loader: loader } }
48
48
  end
49
49
  end
@@ -52,6 +52,8 @@ module KeyTree
52
52
  #
53
53
  def self.open(file_name)
54
54
  type = File.extname(file_name)[/[^.]+/]
55
+ type = type.to_sym unless type.nil?
56
+
55
57
  keytree = File.open(file_name, mode: 'rb:utf-8') do |file|
56
58
  load_from_file(file, type)
57
59
  end
@@ -77,16 +79,6 @@ module KeyTree
77
79
 
78
80
  private_class_method
79
81
 
80
- # Get a class for loading external serialization for +type+
81
- # +require+s the class provider if necessary.
82
- #
83
- def self.get_loader(type)
84
- Class.const_get(type.upcase)
85
- rescue NameError
86
- require type.to_s
87
- retry
88
- end
89
-
90
82
  def self.load_from_file(file, type)
91
83
  load(type => file.read).with_meta_data do |meta_data|
92
84
  file_path = file.path
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: key_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calle Englund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-06 00:00:00.000000000 Z
11
+ date: 2018-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git-version-bump
@@ -101,6 +101,7 @@ files:
101
101
  - key_tree.gemspec
102
102
  - lib/key_tree.rb
103
103
  - lib/key_tree/forest.rb
104
+ - lib/key_tree/loader.rb
104
105
  - lib/key_tree/meta_data.rb
105
106
  - lib/key_tree/path.rb
106
107
  - lib/key_tree/tree.rb