Class: UNotifier::Settings
- Inherits:
-
Object
- Object
- UNotifier::Settings
- Defined in:
- lib/settings.rb
Constant Summary collapse
- DEFAULT_URGENCY =
"external".freeze
Class Method Summary collapse
- .customizable?(config) ⇒ Boolean
- .filter_system_customizable(config) ⇒ Object
- .filter_user_customizable(config) ⇒ Object
- .flatten_keys(keys) ⇒ Object
- .grouped_by_urgency_keys_from(config) ⇒ Object
- .keys_from(config) ⇒ Object
Class Method Details
.customizable?(config) ⇒ Boolean
5 6 7 8 9 10 11 12 13 |
# File 'lib/settings.rb', line 5 def self.customizable?(config) ( config.key?("urgency") && %w(regular optional).include?(config["urgency"]) ) || ( config.key?("target") && config["target"].values.any? { |c| customizable?(c) } ) end |
.filter_system_customizable(config) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/settings.rb', line 36 def self.filter_system_customizable(config) customizable = config.map do |key, subkeys| filtered = subkeys .select { |_, value| value.key?("system") } .map { |subkey, value| [subkey, value["system"]] } .select { |_, value| value["urgency"] == "regular" } .each_with_object({}) do |(subkey, value), out| out[subkey] = value["urgency"] end [key, filtered] end customizable.to_h.reject { |_, subkeys| subkeys.empty? } end |
.filter_user_customizable(config) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/settings.rb', line 15 def self.filter_user_customizable(config) customizable = config.map do |key, subkeys| filtered = subkeys .select { |_, value| value.key?("user") } .map { |subkey, value| [subkey, value["user"]] } .select { |_, value| customizable?(value) } .each_with_object({}) do |(subkey, value), out| out[subkey] = value["urgency"] || value["target"] .select { |_, subvalue| customizable?(subvalue) } .map { |_, subvalue| subvalue["urgency"] } .first end [key, filtered] end customizable.to_h.reject { |_, subkeys| subkeys.empty? } end |
.flatten_keys(keys) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/settings.rb', line 70 def self.flatten_keys(keys) keys.map do |key, subkeys| subkeys.map do |subkey, urgency| { "#{key}.#{subkey}" => urgency } end end.flatten.reduce({}, :merge) end |
.grouped_by_urgency_keys_from(config) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/settings.rb', line 60 def self.grouped_by_urgency_keys_from(config) keys_from(config).each_with_object({}) do |(channel, keys), obj| obj[channel] = keys.each_with_object({}) do |(key, urgency), out| out[urgency] ||= [] out[urgency] << key end end end |
.keys_from(config) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/settings.rb', line 53 def self.keys_from(config) { "user" => flatten_keys(filter_user_customizable(config)), "system" => flatten_keys(filter_system_customizable(config)), } end |