evt-settings 0.5.0.0 → 2.1.1.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: 15b240e6f595c283986941afb829ea499065593fdd50a27d5f32df7958d0c968
4
- data.tar.gz: 44200ae8a2edb411af6613dcbcbe00e608abecc0ddc056811d0d05d16da886ce
3
+ metadata.gz: 68e0691d8994930cab7259e915ec865bab803c3ff7a025a3eea790c2e769a405
4
+ data.tar.gz: 5acd8233c2624f5a7883846b51b33ada95f40fa9c5ad64155597178a50d715e2
5
5
  SHA512:
6
- metadata.gz: 8d665061ae5670648f42b5db434bb1b16b780617d1f7da6007ccad1858d4a83f5f474cd00409ebc98210cc6bddd22ec08a5e9906bf724fa1a8c871e1a1e35269
7
- data.tar.gz: 2498f0a1856df3cec33430813fcb752b9bef27a56122f3fcdbd375b7fc6253a6e5e29014eff6ada9351705fcdd7c0184aca45c17036f030a1384459f3b1cf07c
6
+ metadata.gz: d3765bde5adcfc8f19023d246fdbccc07073d32aa5acf0ce7a8aa5b52bc37f3586541fe06091a34021ae1d1a737a225163438163f09930526232cb92ee2dcfdc
7
+ data.tar.gz: 259ba6bb657b7beed6fa5dd5dbffd12884819e2b8f5d94543f6ef2ceacdf47ae6001bcc2dbe0bdd23d2b880cb4307c464d0831f52fdfa75d26be8cb0027045fe
@@ -1,14 +1,12 @@
1
1
  require 'pathname'
2
2
  require 'json'
3
3
 
4
- require 'log'
5
4
  require 'casing'
6
5
  require 'attribute'
7
- require 'confstruct'
8
6
 
9
- require 'settings/log'
10
- require 'settings/data_source/data_source'
7
+ require 'settings/data_source'
11
8
  require 'settings/data_source/hash'
9
+ require 'settings/data_source/env'
12
10
  require 'settings/data_source/file'
13
11
  require 'settings/data_source/build'
14
12
  require 'settings/registry'
@@ -1,3 +1,4 @@
1
1
  require 'settings/controls/subject'
2
2
  require 'settings/controls/data_source'
3
+ require 'settings/controls/data'
3
4
  require 'settings/controls/settings'
@@ -0,0 +1,54 @@
1
+ class Settings
2
+ module Controls
3
+ module Data
4
+ module Flat
5
+ module Single
6
+ def self.example
7
+ {
8
+ 'some_setting' => 'some value'
9
+ }
10
+ end
11
+ end
12
+
13
+ module Multiple
14
+ def self.example
15
+ {
16
+ 'some_setting' => 'some value',
17
+ 'some_other_setting' => 'some other value',
18
+ }
19
+ end
20
+ end
21
+ end
22
+
23
+ module Hierarchical
24
+ def self.example
25
+ {
26
+ 'some_namespace' => {
27
+ 'some_setting' => 'some value'
28
+ }
29
+ }
30
+ end
31
+ end
32
+
33
+ module CamelCaseKeys
34
+ def self.example
35
+ {
36
+ 'someNamespace' => {
37
+ 'someSetting' => 'some value'
38
+ }
39
+ }
40
+ end
41
+ end
42
+
43
+ module SymbolKeys
44
+ def self.example
45
+ {
46
+ :some_namespace => {
47
+ :some_setting => 'some value'
48
+ }
49
+ }
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -7,9 +7,17 @@ class Settings
7
7
  end
8
8
 
9
9
  def self.data
10
- {
11
- some_setting: 'some value'
12
- }
10
+ Data::Flat::Single.example
11
+ end
12
+
13
+ module SymbolKeys
14
+ def self.example
15
+ ::Settings::DataSource::Hash.build(data)
16
+ end
17
+
18
+ def self.data
19
+ Data::SymbolKeys.example
20
+ end
13
21
  end
14
22
  end
15
23
 
@@ -3,84 +3,44 @@ class Settings
3
3
  module Settings
4
4
  module SettingAttribute
5
5
  def self.example
6
- ::Settings.new(data)
6
+ ::Settings.build(data)
7
7
  end
8
8
 
9
9
  def self.data
10
- {
11
- "some_setting" => "some value"
12
- }
10
+ Data::Flat::Single.example
13
11
  end
14
12
  end
15
13
 
16
- module SettingAttributes
14
+ module PartialMatch
17
15
  def self.example
18
- ::Settings.new(data)
16
+ ::Settings.build(data)
19
17
  end
20
18
 
21
19
  def self.data
22
- {
23
- "some_setting" => "some value",
24
- "some_other_setting" => "some other value",
25
- "yet_another_setting" => "yet another value"
26
- }
27
- end
28
-
29
- module Partial
30
- def self.example
31
- ::Settings.new(data)
32
- end
33
-
34
- def self.data
35
- {
36
- "some_setting" => "some value",
37
- "some_other_setting" => "some other value",
38
- }
39
- end
20
+ Data::Flat::Multiple.example
40
21
  end
41
22
  end
42
23
 
43
- module AccessorAttribute
24
+ module Namespace
44
25
  def self.example
45
- ::Settings.new(data)
26
+ ::Settings.build(data)
46
27
  end
47
28
 
48
29
  def self.data
49
- {
50
- "some_accessor_attribute" => "some accessor value"
51
- }
30
+ Data::Hierarchical.example
52
31
  end
53
32
  end
54
33
 
55
- module Namespace
34
+ module AccessorAttribute
56
35
  def self.example
57
- ::Settings.new(data)
36
+ ::Settings.build(data)
58
37
  end
59
38
 
60
39
  def self.data
61
40
  {
62
- "some_namespace" => {
63
- "some_setting" => "some value"
64
- }
41
+ 'some_accessor_attribute' => 'some accessor value'
65
42
  }
66
43
  end
67
-
68
- module Override
69
- def self.example
70
- s = ::Settings.build(Namespace.data)
71
- s.override(data)
72
- s
73
- end
74
-
75
- def self.data
76
- {
77
- "some_namespace" => {
78
- "some_setting" => "some overridden value",
79
- "some_other_setting" => "some other value"
80
- }
81
- }
82
- end
83
- end
84
44
  end
85
45
  end
86
46
  end
@@ -1,11 +1,15 @@
1
1
  class Settings
2
2
  class DataSource
3
- include Log::Dependency
4
-
5
3
  attr_reader :source
6
4
 
7
5
  def initialize(source)
8
6
  @source = source
9
7
  end
8
+
9
+ def self.build(source=nil)
10
+ source = canonize(source) if respond_to? :canonize
11
+
12
+ new(source)
13
+ end
10
14
  end
11
15
  end
@@ -10,6 +10,7 @@ class Settings
10
10
  def self.type(input=nil)
11
11
  return Settings::DataSource::Hash if input.is_a?(::Hash)
12
12
  return Settings::DataSource::File if input.is_a?(String) || input.nil?
13
+ return Settings::DataSource::Env if input.instance_of?(Object)
13
14
 
14
15
  raise Settings::Error, "Input is not supported: #{input}"
15
16
  end
@@ -0,0 +1,11 @@
1
+ class Settings
2
+ class DataSource
3
+ class Env < DataSource
4
+ def get_data
5
+ source.map do |k, v|
6
+ [k.downcase, v]
7
+ end.to_h
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,22 +1,14 @@
1
1
  class Settings
2
2
  class DataSource
3
3
  class File < DataSource
4
- def self.build(source=nil)
5
- canonical = canonical(source)
6
- validate(canonical)
7
-
8
- new(canonical)
9
- end
10
-
11
- def self.canonical(source)
12
- logger.trace { "Canonizing the file source (#{source})" }
4
+ def self.canonize(source)
5
+ canonized_filepath = canonize_filepath(source)
6
+ validate(canonized_filepath)
13
7
 
14
- canonize(source).tap do |instance|
15
- logger.debug { "Canonized the file source (#{source})" }
16
- end
8
+ canonized_filepath
17
9
  end
18
10
 
19
- def self.canonize(source)
11
+ def self.canonize_filepath(source)
20
12
  return default_filepath if source.nil?
21
13
  return source if full_path?(source)
22
14
 
@@ -59,58 +51,32 @@ class Settings
59
51
  end
60
52
 
61
53
  def self.validate(pathname)
62
- logger.trace { "Validating the pathname (#{pathname})" }
63
-
64
54
  pathname = Pathname.new(pathname)
65
55
 
66
56
  unless pathname.file?
67
- msg = "Settings cannot be read from #{pathname}. The file doesn't exist."
68
- logger.error { msg }
69
- raise Settings::Error, msg
57
+ raise Settings::Error, "Settings cannot be read from #{pathname}. The file doesn't exist."
70
58
  end
71
-
72
- logger.trace { "Validated the pathname (#{pathname})" }
73
- end
74
-
75
- def self.logger
76
- @logger ||= Log.get(self)
77
59
  end
78
60
 
79
61
  def get_data
80
- logger.trace { "Reading file: #{source}" }
81
62
  file = ::File.open(source)
82
- data = JSON.load(file).tap do
83
- logger.debug { "Read file: #{source}" }
84
- end
85
63
 
86
- data = Casing::Underscore.(data)
64
+ data = JSON.load(file)
87
65
 
88
66
  hash_data_source = Hash.build data
89
67
  hash_data_source.get_data
90
68
  end
91
69
 
92
70
  module Defaults
93
- def self.logger
94
- @logger ||= Log.get(self)
95
- end
96
-
97
71
  def self.filename
98
- default_file = 'settings.json'
99
- logger.debug { "Using the default settings file name (#{default_file})" }
100
- default_file
72
+ 'settings.json'
101
73
  end
102
74
  end
103
75
 
104
76
  module Directory
105
77
  module Defaults
106
- def self.logger
107
- @logger ||= Log.get(self)
108
- end
109
-
110
78
  def self.pathname
111
- default_dir = Dir.pwd
112
- logger.debug { "Using the working directory default settings directory (#{default_dir})" }
113
- default_dir
79
+ Dir.pwd
114
80
  end
115
81
  end
116
82
  end
@@ -1,18 +1,27 @@
1
1
  class Settings
2
2
  class DataSource
3
3
  class Hash < DataSource
4
- def self.logger
5
- @logger ||= Log.get(self)
4
+ def get_data
5
+ AssureStringKeys.(source)
6
6
  end
7
7
 
8
- def self.build(source)
9
- new(source)
10
- end
8
+ module AssureStringKeys
9
+ def self.call(data)
10
+ new_data = {}
11
11
 
12
- def get_data
13
- logger.trace { "Converting the raw source data to Confstruct" }
14
- Confstruct::Configuration.new(source).tap do |instance|
15
- logger.debug { "Converted the raw source data to Confstruct" }
12
+ data.each do |key, value|
13
+ new_key = key.to_s
14
+
15
+ if value.is_a?(::Hash)
16
+ new_value = self.(value)
17
+ else
18
+ new_value = value
19
+ end
20
+
21
+ new_data[new_key] = new_value
22
+ end
23
+
24
+ new_data
16
25
  end
17
26
  end
18
27
  end
@@ -3,10 +3,6 @@ class Settings
3
3
  module Assignment
4
4
  extend self
5
5
 
6
- def logger
7
- @logger ||= Log.get(self)
8
- end
9
-
10
6
  def assign(receiver, attribute, value, strict=false)
11
7
  settable = assure_settable(receiver, attribute, strict)
12
8
  if settable
@@ -17,11 +13,7 @@ class Settings
17
13
  end
18
14
 
19
15
  def assign_value(receiver, attribute, value)
20
- logger.trace { "Assigning to #{attribute}" }
21
- receiver.public_send("#{attribute}=", value).tap do
22
- logger.debug { "Assigned to #{attribute}" }
23
- logger.debug(tag: :data) { "#{attribute}: #{value}" }
24
- end
16
+ receiver.public_send("#{attribute}=", value)
25
17
  end
26
18
 
27
19
  def setting?(receiver, attribute)
@@ -37,40 +29,22 @@ class Settings
37
29
  :"#{attribute.to_s}=" unless attribute.to_s.end_with? '='
38
30
  end
39
31
 
40
- def digest(receiver, attribute, strict)
41
- content = []
42
- content << "Attribute: #{attribute}" if attribute
43
- content << "Receiver: #{receiver.class.name}"
44
- strict = "<not set>" if strict.nil?
45
- content << "Strict: #{strict}"
46
- content.join ', '
47
- end
48
-
49
32
  module Object
50
33
  extend Assignment
51
34
 
52
- def logger
53
- @logger ||= Log.get(self)
54
- end
55
-
56
35
  def self.assure_settable(receiver, attribute, strict=true)
57
- logger.trace { "Approving attribute (#{digest(receiver, attribute, strict)})" }
58
-
59
36
  if strict
60
37
  setting = setting?(receiver, attribute)
61
38
  unless setting
62
- logger.warn { "Can't set \"#{attribute}\". It isn't a setting of #{receiver.class.name}." }
63
39
  return false
64
40
  end
65
41
  end
66
42
 
67
43
  assignable = assignable? receiver, attribute
68
44
  unless assignable
69
- logger.warn { "Can't set \"#{attribute}\". It isn't assignable to #{receiver.class.name}." }
70
45
  return false
71
46
  end
72
47
 
73
- logger.debug { "\"#{attribute}\" can be set" }
74
48
  true
75
49
  end
76
50
  end
@@ -78,28 +52,19 @@ class Settings
78
52
  module Attribute
79
53
  extend Assignment
80
54
 
81
- def logger
82
- @logger ||= Log.get(self)
83
- end
84
-
85
55
  def self.assure_settable(receiver, attribute, strict=true)
86
56
  if strict
87
57
  setting = setting? receiver, attribute
88
58
  unless setting
89
- msg = "Can't set \"#{attribute}\". It isn't a setting of #{receiver.class.name}."
90
- logger.error { msg }
91
- raise msg
59
+ raise "Can't set \"#{attribute}\". It isn't a setting of #{receiver.class.name}."
92
60
  end
93
61
  end
94
62
 
95
- assignable = assignable? receiver, attribute
63
+ assignable = assignable?(receiver, attribute)
96
64
  unless assignable
97
- msg = "Can't set \"#{attribute}\". It isn't assignable to #{receiver.class.name}."
98
- logger.error { msg }
99
- raise msg
65
+ raise "Can't set \"#{attribute}\". It isn't assignable to #{receiver.class.name}."
100
66
  end
101
67
 
102
- logger.debug { "\"#{attribute}\" can be set" }
103
68
  true
104
69
  end
105
70
  end
@@ -2,13 +2,8 @@ class Settings
2
2
  module Setting
3
3
  module Macro
4
4
  def setting_macro(attribute)
5
- logger = Log.get(self)
6
-
7
- logger.trace { "Defining setting attribute: #{attribute}" }
8
5
  Attribute::Define.(self, attribute, :accessor)
9
- Settings::Registry.register(self, attribute).tap do
10
- logger.debug { "Defined setting attribute: #{attribute}" }
11
- end
6
+ Settings::Registry.register(self, attribute)
12
7
  end
13
8
  alias :setting :setting_macro
14
9
  end
@@ -1,18 +1,12 @@
1
1
  class Settings
2
2
  class Error < RuntimeError; end
3
3
 
4
- include Log::Dependency
5
-
6
4
  attr_reader :data
7
5
 
8
6
  def initialize(data)
9
7
  @data = data
10
8
  end
11
9
 
12
- def self.logger
13
- @logger ||= Log.get(self)
14
- end
15
-
16
10
  def self.build(source=nil)
17
11
  source ||= implementer_source
18
12
 
@@ -20,41 +14,22 @@ class Settings
20
14
 
21
15
  data = data_source.get_data
22
16
 
17
+ data = Casing::Underscore.(data)
18
+
23
19
  instance = new(data)
24
20
 
25
21
  instance
26
22
  end
27
23
 
28
24
  def self.implementer_source
29
- logger.trace { "Getting data source from the implementer" }
30
-
31
- unless self.respond_to? :data_source
32
- logger.trace { "Implementer doesn't provide a data_source" }
25
+ unless self.respond_to?(:data_source)
33
26
  return nil
34
27
  end
35
28
 
36
- self.data_source.tap do |data_source|
37
- logger.trace { "Got data source from the implementer (#{data_source})" }
38
- end
39
- end
40
-
41
- def override(override_data)
42
- logger.trace { "Overriding settings data" }
43
- res = data.push!(override_data)
44
- logger.debug { "Overrode settings data" }
45
- logger.debug(tag: :data) { "Override data #{override_data}" }
46
- res
47
- end
48
-
49
- def reset
50
- logger.trace { "Resetting overridden settings data" }
51
- res = data.pop!
52
- logger.debug { "Reset overridden settings data" }
53
- res
29
+ self.data_source
54
30
  end
55
31
 
56
32
  def set(receiver, *namespace, attribute: nil, strict: true)
57
- logger.trace { "Setting #{receiver.class.name} (#{digest(namespace, attribute, strict)})" }
58
33
  unless attribute.nil?
59
34
  value = set_attribute(receiver, attribute, namespace, strict)
60
35
  else
@@ -64,8 +39,6 @@ class Settings
64
39
  end
65
40
 
66
41
  def set_attribute(receiver, attribute, namespace, strict)
67
- logger.trace { "Setting #{receiver.class.name} attribute (#{digest(namespace, attribute, strict)})" }
68
-
69
42
  attribute = attribute.to_s if attribute.is_a?(Symbol)
70
43
 
71
44
  attribute_namespace = namespace.dup
@@ -74,35 +47,25 @@ class Settings
74
47
  value = get(attribute_namespace)
75
48
 
76
49
  if value.nil?
77
- msg = "#{attribute_namespace} not found in the data"
78
- logger.error { msg }
79
- raise Settings::Error, msg
50
+ raise Settings::Error, "#{attribute_namespace} not found in the data"
80
51
  end
81
52
 
82
53
  Settings::Setting::Assignment::Attribute.assign(receiver, attribute.to_sym, value, strict)
83
54
 
84
- logger.debug { "Set #{receiver.class.name} #{attribute} to #{value.inspect}" }
85
-
86
55
  value
87
56
  end
88
57
 
89
58
  def set_object(receiver, namespace, strict)
90
- logger.trace { "Setting #{receiver.class.name} object (#{digest(namespace, nil, strict)})" }
91
-
92
59
  data = get(namespace)
93
60
 
94
61
  if data.nil?
95
- msg = "#{namespace} not found in the data"
96
- logger.error { msg }
97
- raise Settings::Error, msg
62
+ raise Settings::Error, "#{namespace} not found in the data"
98
63
  end
99
64
 
100
65
  data.each do |attribute, value|
101
66
  Settings::Setting::Assignment::Object.assign(receiver, attribute.to_sym, value, strict)
102
67
  end
103
68
 
104
- logger.debug { "Set #{receiver.class.name} object (#{digest(namespace, nil, strict)})" }
105
-
106
69
  receiver
107
70
  end
108
71
 
@@ -112,24 +75,16 @@ class Settings
112
75
 
113
76
  def get(*namespace)
114
77
  namespace.flatten!
115
- logger.trace { "Getting #{namespace}" }
116
-
117
- string_keys = namespace.map { |n| n.is_a?(String) ? n : n.to_s }
118
78
 
119
- value = string_keys.inject(data) {|memo, k| memo ? memo[k] : nil }
79
+ keys = namespace.map { |n| n.is_a?(Symbol) ? n.to_s : n }
120
80
 
121
- logger.debug { "Got #{namespace}" }
122
- logger.debug(tag: :data) { "#{namespace}: #{value.inspect}" }
81
+ value = nil
82
+ if keys.empty?
83
+ value = data
84
+ else
85
+ value = data.dig(*keys)
86
+ end
123
87
 
124
88
  value
125
89
  end
126
-
127
- def digest(namespace, attribute, strict)
128
- content = []
129
- content << "Namespace: #{namespace.join ', '}" unless namespace.empty?
130
- content << "Attribute: #{attribute}" if attribute
131
- strict = "<not set>" if strict.nil?
132
- content << "Strict: #{strict}"
133
- content.join ', '
134
- end
135
90
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.0
4
+ version: 2.1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-12 00:00:00.000000000 Z
11
+ date: 2020-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: evt-log
14
+ name: evt-attribute
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: confstruct
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: test_bench
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -74,14 +60,15 @@ extra_rdoc_files: []
74
60
  files:
75
61
  - lib/settings.rb
76
62
  - lib/settings/controls.rb
63
+ - lib/settings/controls/data.rb
77
64
  - lib/settings/controls/data_source.rb
78
65
  - lib/settings/controls/settings.rb
79
66
  - lib/settings/controls/subject.rb
67
+ - lib/settings/data_source.rb
80
68
  - lib/settings/data_source/build.rb
81
- - lib/settings/data_source/data_source.rb
69
+ - lib/settings/data_source/env.rb
82
70
  - lib/settings/data_source/file.rb
83
71
  - lib/settings/data_source/hash.rb
84
- - lib/settings/log.rb
85
72
  - lib/settings/registry.rb
86
73
  - lib/settings/setting.rb
87
74
  - lib/settings/setting/assignment.rb
@@ -91,7 +78,7 @@ homepage: https://github.com/eventide-project/settings
91
78
  licenses:
92
79
  - MIT
93
80
  metadata: {}
94
- post_install_message:
81
+ post_install_message:
95
82
  rdoc_options: []
96
83
  require_paths:
97
84
  - lib
@@ -106,9 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
93
  - !ruby/object:Gem::Version
107
94
  version: '0'
108
95
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.7.6
111
- signing_key:
96
+ rubygems_version: 3.1.2
97
+ signing_key:
112
98
  specification_version: 4
113
99
  summary: Settings data access and assignment
114
100
  test_files: []
@@ -1,9 +0,0 @@
1
- class Settings
2
- class Log < ::Log
3
- def tag!(tags)
4
- tags << :settings
5
- tags << :library
6
- tags << :verbose
7
- end
8
- end
9
- end