Class: Settings
- Inherits:
-
Object
show all
- Includes:
- Log::Dependency
- Defined in:
- lib/settings/log.rb,
lib/settings/setting.rb,
lib/settings/registry.rb,
lib/settings/settings.rb,
lib/settings/data_source.rb,
lib/settings/controls/data.rb,
lib/settings/setting/macro.rb,
lib/settings/controls/subject.rb,
lib/settings/data_source/file.rb,
lib/settings/data_source/hash.rb,
lib/settings/controls/settings.rb,
lib/settings/data_source/build.rb,
lib/settings/setting/assignment.rb,
lib/settings/controls/data_source.rb
Defined Under Namespace
Modules: Controls, Setting
Classes: DataSource, Error, Log, Registry
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#assign_value(receiver, attribute, value, strict = false) ⇒ Object
-
#digest(namespace, attribute, strict) ⇒ Object
-
#get(*namespace) ⇒ Object
-
#initialize(data) ⇒ Settings
constructor
A new instance of Settings.
-
#set(receiver, *namespace, attribute: nil, strict: true) ⇒ Object
-
#set_attribute(receiver, attribute, namespace, strict) ⇒ Object
-
#set_object(receiver, namespace, strict) ⇒ Object
Constructor Details
#initialize(data) ⇒ Settings
Returns a new instance of Settings.
8
9
10
|
# File 'lib/settings/settings.rb', line 8
def initialize(data)
@data = data
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6
7
8
|
# File 'lib/settings/settings.rb', line 6
def data
@data
end
|
Class Method Details
.build(source = nil) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/settings/settings.rb', line 16
def self.build(source=nil)
source ||= implementer_source
data_source = DataSource::Build.(source)
data = data_source.get_data
data = Casing::Underscore.(data)
instance = new(data)
instance
end
|
.implementer_source ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/settings/settings.rb', line 30
def self.implementer_source
logger.trace { "Getting data source from the implementer" }
unless self.respond_to? :data_source
logger.trace { "Implementer doesn't provide a data_source" }
return nil
end
self.data_source.tap do |data_source|
logger.trace { "Got data source from the implementer (#{data_source})" }
end
end
|
.logger ⇒ Object
12
13
14
|
# File 'lib/settings/settings.rb', line 12
def self.logger
@logger ||= Log.get(self)
end
|
Instance Method Details
#assign_value(receiver, attribute, value, strict = false) ⇒ Object
96
97
98
|
# File 'lib/settings/settings.rb', line 96
def assign_value(receiver, attribute, value, strict=false)
Settings::Setting::Assignment.assign(receiver, attribute, value, strict)
end
|
#digest(namespace, attribute, strict) ⇒ Object
119
120
121
122
123
124
125
126
|
# File 'lib/settings/settings.rb', line 119
def digest(namespace, attribute, strict)
content = []
content << "Namespace: #{namespace.join ', '}" unless namespace.empty?
content << "Attribute: #{attribute}" if attribute
strict = "<not set>" if strict.nil?
content << "Strict: #{strict}"
content.join ', '
end
|
#get(*namespace) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/settings/settings.rb', line 100
def get(*namespace)
namespace.flatten!
logger.trace { "Getting #{namespace}" }
keys = namespace.map { |n| n.is_a?(Symbol) ? n.to_s : n }
value = nil
if keys.empty?
value = data
else
value = data.dig(*keys)
end
logger.debug { "Got #{namespace}" }
logger.debug(tag: :data) { "#{namespace}: #{value.inspect}" }
value
end
|
#set(receiver, *namespace, attribute: nil, strict: true) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/settings/settings.rb', line 43
def set(receiver, *namespace, attribute: nil, strict: true)
logger.trace { "Setting #{receiver.class.name} (#{digest(namespace, attribute, strict)})" }
unless attribute.nil?
value = set_attribute(receiver, attribute, namespace, strict)
else
receiver = set_object(receiver, namespace, strict)
end
value || receiver
end
|
#set_attribute(receiver, attribute, namespace, strict) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/settings/settings.rb', line 53
def set_attribute(receiver, attribute, namespace, strict)
logger.trace { "Setting #{receiver.class.name} attribute (#{digest(namespace, attribute, strict)})" }
attribute = attribute.to_s if attribute.is_a?(Symbol)
attribute_namespace = namespace.dup
attribute_namespace << attribute
value = get(attribute_namespace)
if value.nil?
msg = "#{attribute_namespace} not found in the data"
logger.error { msg }
raise Settings::Error, msg
end
Settings::Setting::Assignment::Attribute.assign(receiver, attribute.to_sym, value, strict)
logger.debug { "Set #{receiver.class.name} #{attribute} to #{value.inspect}" }
value
end
|
#set_object(receiver, namespace, strict) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/settings/settings.rb', line 76
def set_object(receiver, namespace, strict)
logger.trace { "Setting #{receiver.class.name} object (#{digest(namespace, nil, strict)})" }
data = get(namespace)
if data.nil?
msg = "#{namespace} not found in the data"
logger.error { msg }
raise Settings::Error, msg
end
data.each do |attribute, value|
Settings::Setting::Assignment::Object.assign(receiver, attribute.to_sym, value, strict)
end
logger.debug { "Set #{receiver.class.name} object (#{digest(namespace, nil, strict)})" }
receiver
end
|