Class: Settings

Inherits:
Object
  • Object
show all
Defined in:
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/data_source/env.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, Registry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Settings

Returns a new instance of Settings.



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

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/settings/settings.rb', line 4

def data
  @data
end

Class Method Details

.build(source = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/settings/settings.rb', line 10

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

  data = get_data(source)

  instance = new(data)

  instance
end

.get_data(source) ⇒ Object



20
21
22
23
# File 'lib/settings/settings.rb', line 20

def self.get_data(source)
  data_source = DataSource::Build.(source)
  data = data_source.get_data
end

.implementer_sourceObject



25
26
27
28
29
30
31
# File 'lib/settings/settings.rb', line 25

def self.implementer_source
  unless self.respond_to?(:data_source)
    return nil
  end

  self.data_source
end

Instance Method Details

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



73
74
75
# File 'lib/settings/settings.rb', line 73

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

#get(*namespace) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/settings/settings.rb', line 77

def get(*namespace)
  namespace.flatten!

  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

  value
end

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



33
34
35
36
37
38
39
40
# File 'lib/settings/settings.rb', line 33

def set(receiver, *namespace, attribute: nil, strict: true)
  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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/settings/settings.rb', line 42

def set_attribute(receiver, attribute, namespace, strict)
  attribute = attribute.to_s if attribute.is_a?(Symbol)

  attribute_namespace = namespace.dup
  attribute_namespace << attribute

  value = get(attribute_namespace)

  if value.nil?
    raise Settings::Error, "#{attribute_namespace} not found in the data"
  end

  Settings::Setting::Assignment::Attribute.assign(receiver, attribute.to_sym, value, strict)

  value
end

#set_object(receiver, namespace, strict) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/settings/settings.rb', line 59

def set_object(receiver, namespace, strict)
  data = get(namespace)

  if data.nil?
    raise Settings::Error, "#{namespace} not found in the data"
  end

  data.each do |attribute, value|
    Settings::Setting::Assignment::Object.assign(receiver, attribute.to_sym, value, strict)
  end

  receiver
end