Class: Cosmo::Config

Inherits:
Hash
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cosmo/config.rb,
sig/cosmo/config.rbs

Constant Summary collapse

NANO =

Returns:

  • (Integer)
1_000_000_000
DEFAULT_PATH =

Returns:

  • (::String)
"config/cosmo.yml"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[]Object

Parameters:

  • key (Symbol)

Returns:

  • (Object)


21
# File 'sig/cosmo/config.rbs', line 21

def self.[]: (Symbol key) -> untyped

.deliver_policy(start_position) ⇒ Hash[Symbol, ::String]

Parameters:

  • start_position (Symbol, ::String, Time)

Returns:

  • (Hash[Symbol, ::String])


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cosmo/config.rb', line 60

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

.digObject

Parameters:

  • keys (Symbol)

Returns:

  • (Object)


25
# File 'sig/cosmo/config.rbs', line 25

def self.dig: (*Symbol keys) -> untyped

.fetchObject

Parameters:

  • key (Symbol)
  • default (Object)

Returns:

  • (Object)


23
# File 'sig/cosmo/config.rbs', line 23

def self.fetch: (Symbol key, ?untyped default) -> untyped

.instanceConfig

Returns:



75
76
77
# File 'lib/cosmo/config.rb', line 75

def self.instance
  @instance ||= new
end

.internalHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


79
80
81
# File 'lib/cosmo/config.rb', line 79

def self.internal
  @internal ||= {}
end

.loadvoid

This method returns an undefined value.

Parameters:

  • path (::String, nil)


31
# File 'sig/cosmo/config.rbs', line 31

def self.load: (?::String? path) -> void

.normalize!(config) ⇒ void

This method returns an undefined value.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Parameters:

  • config (Hash[Symbol, untyped])


25
26
27
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
53
54
55
56
57
58
# File 'lib/cosmo/config.rb', line 25

def self.normalize!(config) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  Utils::Hash.symbolize_keys!(config)

  config[:timeout] = Utils::Duration.parse(config[:timeout]) if config[:timeout]
  config[:batch_expiry] = Utils::Duration.parse(config[:batch_expiry]) if config[:batch_expiry]

  config[:consumers]&.each_key do |name|
    config[:consumers][name].each do |stream_name, c|
      next unless c

      c[:ack_wait] = Utils::Duration.parse(c[:ack_wait]) if c[:ack_wait]
      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|
    next if type == :cron

    config[:setup][type]&.each_key do |name|
      c = config[:setup][type][name]
      c[:max_age] = to_ns(Utils::Duration.parse(c[:max_age])) if c[:max_age]
      c[:duplicate_window] = to_ns(Utils::Duration.parse(c[:duplicate_window])) if c[:duplicate_window]
      c[:subjects] = c[:subjects].map { |s| format(s, name: name) } if c[:subjects]

      next unless type == :jobs # Every jobs stream supports NATS 2.14 message scheduling.

      c[:allow_msg_schedules] = true
      cron_subject = "#{API::Cron::Entry::SUBJECT_PREFIX}.#{name}.>"
      c[:subjects] = Array(c[:subjects])
      c[:subjects] << cron_subject unless c[:subjects].include?(cron_subject)
    end
  end
end

.parse_file(path) ⇒ Hash[Symbol, untyped]

Parameters:

  • path (::String)

Returns:

  • (Hash[Symbol, untyped])


21
22
23
# File 'lib/cosmo/config.rb', line 21

def self.parse_file(path)
  YAML.load_file(path, aliases: true).tap { normalize!(_1) }
end

.setObject

Parameters:

  • (Object)

Returns:

  • (Object)


29
# File 'sig/cosmo/config.rbs', line 29

def self.set: (*untyped) -> untyped

.to_hHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


27
# File 'sig/cosmo/config.rbs', line 27

def self.to_h: () -> Hash[Symbol, untyped]

.to_ns(seconds) ⇒ ::Integer

Parameters:

  • seconds (Numeric)

Returns:

  • (::Integer)


17
18
19
# File 'lib/cosmo/config.rb', line 17

def self.to_ns(seconds)
  (seconds.to_f * NANO).to_i
end

Instance Method Details

#load(path = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • path (::String, nil) (defaults to: nil)


87
88
89
90
91
# File 'lib/cosmo/config.rb', line 87

def load(path = nil)
  return unless path

  replace(self.class.parse_file(path))
end

#setObject

Parameters:

  • (Object)

Returns:

  • (Object)


83
84
85
# File 'lib/cosmo/config.rb', line 83

def set(...)
  Utils::Hash.set(self, ...)
end