Class: Settings

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/settings/log.rb,
lib/settings/activate.rb,
lib/settings/registry.rb,
lib/settings/settings.rb,
lib/settings/setting/macro.rb,
lib/settings/data_source/file.rb,
lib/settings/data_source/hash.rb,
lib/settings/data_source/build.rb,
lib/settings/setting/assignment.rb,
lib/settings/data_source/data_source.rb

Defined Under Namespace

Modules: Setting Classes: DataSource, Error, Log, Registry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, pathname = nil) ⇒ Settings

Returns a new instance of Settings.



9
10
11
12
# File 'lib/settings/settings.rb', line 9

def initialize(data, pathname=nil)
  @data = data
  @pathname = pathname
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/settings/settings.rb', line 6

def data
  @data
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



7
8
9
# File 'lib/settings/settings.rb', line 7

def pathname
  @pathname
end

Class Method Details

.activate(target_class = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/settings/activate.rb', line 2

def self.activate(target_class=nil)
  target_class ||= Object

  macro_module = Settings::Setting::Macro

  return if target_class.is_a? macro_module

  target_class.extend(macro_module)
end

.build(source = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/settings/settings.rb', line 18

def self.build(source=nil)
  source ||= implementer_source

  data_source = DataSource::Build.(source)

  data = data_source.get_data

  instance = new data

  instance
end

.implementer_sourceObject



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

.loggerObject



14
15
16
# File 'lib/settings/settings.rb', line 14

def self.logger
  @logger ||= Log.get(self)
end

Instance Method Details

#assign_value(receiver, attribute, value, strict = false) ⇒ Object



114
115
116
# File 'lib/settings/settings.rb', line 114

def assign_value(receiver, attribute, value, strict=false)
  Settings::Setting::Assignment.assign(receiver, attribute, value, strict)
end

#digest(namespace, attribute, strict) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/settings/settings.rb', line 135

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/settings/settings.rb', line 118

def get(*namespace)
  namespace.flatten!
  logger.trace { "Getting #{namespace}" }

  string_keys = namespace.map { |n| n.is_a?(String) ? n : n.to_s }

  value = string_keys.inject(data) {|memo, k| memo ? memo[k] : nil }

  log_data = value
  log_data = log_data.to_h if log_data.respond_to? :to_h

  logger.debug { "Got #{namespace}" }
  logger.debug(tag: :data) { "#{namespace}: #{log_data}" }

  value
end

#override(override_data) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/settings/settings.rb', line 43

def override(override_data)
  logger.trace { "Overriding settings data" }
  res = data.push!(override_data)
  logger.debug { "Overrode settings data" }
  logger.debug(tag: :data) { "Override data #{override_data}" }
  res
end

#resetObject



51
52
53
54
55
56
# File 'lib/settings/settings.rb', line 51

def reset
  logger.trace { "Resetting overridden settings data" }
  res = data.pop!
  logger.debug { "Reset overridden settings data" }
  res
end

#set(receiver, *namespace, attribute: nil, strict: true) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/settings/settings.rb', line 58

def set(receiver, *namespace, attribute: nil, strict: true)
  logger.trace { "Setting #{receiver} (#{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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/settings/settings.rb', line 68

def set_attribute(receiver, attribute, namespace, strict)
  logger.trace { "Setting #{receiver} 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)

  log_value = value
  log_value = log_value.to_h if log_value.respond_to? :to_h

  logger.debug { "Set #{receiver} #{attribute} to #{log_value}" }

  value
end

#set_object(receiver, namespace, strict) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/settings/settings.rb', line 94

def set_object(receiver, namespace, strict)
  logger.trace { "Setting #{receiver} 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} object (#{digest(namespace, nil, strict)})" }

  receiver
end