Class: Cosmo::Config
- Inherits:
-
Object
- Object
- Cosmo::Config
- Extended by:
- Forwardable
- Defined in:
- lib/cosmo/config.rb
Constant Summary collapse
- NANO =
1_000_000_000- DEFAULT_PATH =
"config/cosmo.yml"
Class Method Summary collapse
- .deliver_policy(start_position) ⇒ Object
- .instance ⇒ Object
-
.normalize!(config) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- .parse_file(path) ⇒ Object
- .system ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #dig(*keys) ⇒ Object
- #fetch(key, default = nil) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load(path = nil) ⇒ Object
- #set ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
66 67 68 69 70 |
# File 'lib/cosmo/config.rb', line 66 def initialize @config = nil @system = {} @defaults = self.class.parse_file(File.("defaults.yml", __dir__)) end |
Class Method Details
.deliver_policy(start_position) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cosmo/config.rb', line 43 def self.deliver_policy(start_position) case start_position when "last", :last { deliver_policy: "last" } when "new", :new { deliver_policy: "new" } when Time { deliver_policy: "by_start_time", opt_start_time: start_position.iso8601 } when String { deliver_policy: "by_start_time", opt_start_time: start_position } else { deliver_policy: "all" } end end |
.instance ⇒ Object
58 59 60 |
# File 'lib/cosmo/config.rb', line 58 def self.instance @instance ||= new end |
.normalize!(config) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cosmo/config.rb', line 21 def self.normalize!(config) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity Utils::Hash.symbolize_keys!(config) config[:consumers]&.each_key do |name| config[:consumers][name].each do |stream_name, c| next unless c c[:subject] = format(c[:subject], { name: stream_name }) if c[:subject] c[:subjects] = c[:subjects].map { |s| format(s, name: stream_name) } if c[:subjects] end end config[:setup]&.each_key do |type| config[:setup][type]&.each_key do |name| c = config[:setup][type][name] c[:max_age] = c[:max_age].to_i * NANO if c[:max_age] c[:duplicate_window] = c[:duplicate_window].to_i * NANO if c[:duplicate_window] c[:subjects] = c[:subjects].map { |s| format(s, name: name) } if c[:subjects] end end end |
.parse_file(path) ⇒ Object
17 18 19 |
# File 'lib/cosmo/config.rb', line 17 def self.parse_file(path) YAML.load_file(path, aliases: true).tap { normalize!(_1) } end |
.system ⇒ Object
62 63 64 |
# File 'lib/cosmo/config.rb', line 62 def self.system @system ||= {} end |
Instance Method Details
#[](key) ⇒ Object
72 73 74 |
# File 'lib/cosmo/config.rb', line 72 def [](key) dig(key) end |
#dig(*keys) ⇒ Object
82 83 84 85 86 |
# File 'lib/cosmo/config.rb', line 82 def dig(*keys) return @config&.dig(*keys) if @config && Utils::Hash.keys?(@config, *keys) @defaults.dig(*keys) end |
#fetch(key, default = nil) ⇒ Object
76 77 78 79 80 |
# File 'lib/cosmo/config.rb', line 76 def fetch(key, default = nil) return @config.fetch(key, default) if @config && Utils::Hash.keys?(@config, key) @defaults.fetch(key, default) end |
#load(path = nil) ⇒ Object
97 98 99 100 101 |
# File 'lib/cosmo/config.rb', line 97 def load(path = nil) return unless path @config = self.class.parse_file(path) end |