Class: D13n::Configuration::ServerSource

Inherits:
DottedHash
  • Object
show all
Defined in:
lib/d13n/configuration/server_source.rb

Constant Summary collapse

SERVICE_PREFIXES =
/^service/i
PROPERTY_PREFIXES =
/^property/i
IDC_PREFIXES =
/^idc/i
CLIENT_PREFIXES =
/^client/i
JURISDICTION_PREFIXES =
/^jurisdiction/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DottedHash

#inspect, symbolize, #to_hash

Constructor Details

#initialize(connect_reply) ⇒ ServerSource

Returns a new instance of ServerSource.



11
12
13
14
15
16
17
18
19
20
# File 'lib/d13n/configuration/server_source.rb', line 11

def initialize(connect_reply)
  #filter_keys(connect_reply)
  @type_map = {}

  DEFAULTS.each do |config_setting, value|
    self.type_map[config_setting] = value[:type]
  end

  super(connect_reply)
end

Instance Attribute Details

#type_mapObject

Returns the value of attribute type_map.



9
10
11
# File 'lib/d13n/configuration/server_source.rb', line 9

def type_map
  @type_map
end

Class Method Details

.build(connect_reply) ⇒ Object



54
55
56
57
58
# File 'lib/d13n/configuration/server_source.rb', line 54

def self.build(connect_reply)
  instance = new(connect_reply)
  self.filter_keys(instance)
  instance
end

.filter_keys(instance) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/d13n/configuration/server_source.rb', line 60

def self.filter_keys(instance)
  instance.delete_if do |key, _|
    s_key = key.to_s
    if s_key.match(SERVICE_PREFIXES) || s_key.match(PROPERTY_PREFIXES) || s_key.match(IDC_PREFIXES) || s_key.match(CLIENT_PREFIXES) || s_key.match(JURISDICTION_PREFIXES)
      false
    else
      setting_spec = DEFAULTS[key.to_sym]
      if setting_spec
        if setting_spec[:allowed_from_server]
          instance.set_key_by_type(key)
          false # it's allowed, so don't delete it
        else
          D13n.logger.warn("Ignoring server-sent config for '#{key}' - this setting cannot be set from the server")
          true # delete it
        end
      else
        D13n.logger.debug("Ignoring unrecognized config key from server: '#{key}'")
        true
      end
    end
  end
end

Instance Method Details

#set_key_by_type(config_key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/d13n/configuration/server_source.rb', line 28

def set_key_by_type(config_key)
  value = self[config_key]
  type = self.type_map[config_key]

  if type == String
    self[config_key] = value.to_s
  elsif type == Integer
    self[config_key] = value.to_i
  elsif type == Float
    self[config_key] = value.to_f
  elsif type == Symbol
    self[config_key] = value.to_sym
  elsif type == Array
    self[config_key] = value.split(/\s*,\s*/)
  elsif type == D13n::Configuration::Boolean
    if value =~ /false|off|no/i
      self[config_key] = false
    elsif value != nil
      self[config_key] = true
    end
  else
    D13n.logger.info("#{config_key} does not have a corresponding configuration setting (#{config_key} does not exist).")
    self[config_key] = value
  end
end

#set_keys_by_typeObject



22
23
24
25
26
# File 'lib/d13n/configuration/server_source.rb', line 22

def set_keys_by_type()
  self.keys.each do |key|
    set_key_by_type(key)
  end
end