Module: Cosmo::Stream::ClassMethods

Defined in:
lib/cosmo/stream.rb

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



26
27
28
# File 'lib/cosmo/stream.rb', line 26

def default_options
  @default_options ||= Utils::Hash.dup(superclass.respond_to?(:default_options) ? superclass.default_options : Data::DEFAULTS)
end

#options(stream: nil, consumer_name: nil, batch_size: nil, fetch_timeout: nil, start_position: nil, consumer: nil, publisher: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



15
16
17
18
# File 'lib/cosmo/stream.rb', line 15

def options(stream: nil, consumer_name: nil, batch_size: nil, fetch_timeout: nil, start_position: nil, consumer: nil, publisher: nil) # rubocop:disable Metrics/ParameterLists
  register
  default_options.merge!({ stream:, consumer_name:, batch_size:, fetch_timeout:, start_position:, consumer:, publisher: }.compact)
end

#publish(data, subject: nil, **options) ⇒ Object



20
21
22
23
24
# File 'lib/cosmo/stream.rb', line 20

def publish(data, subject: nil, **options)
  stream = default_options[:stream]
  subject ||= default_options.dig(:publisher, :subject)
  Publisher.publish(subject, data, stream: stream, serializer: default_options.dig(:publisher, :serializer), **options)
end

#registerObject

rubocop:disable Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cosmo/stream.rb', line 30

def register # rubocop:disable Metrics/AbcSize
  Config.system[:streams] ||= []
  Config.system[:streams] << self

  # settings are inherited, don't try to modify them
  return if default_options != Data::DEFAULTS

  class_name = Utils::String.underscore(name)
  default_options.merge!(stream: class_name,
                         consumer_name: "consumer-#{class_name}",
                         publisher: { subject: "#{class_name}.default" })
  subjects = default_options.dig(:consumer, :subjects)
  subjects&.map! { format(_1, name: class_name) }

  subject = default_options[:publisher][:subject]
  default_options[:publisher][:subject] = format(subject, name: class_name)
end