Class: EventStore::HTTP::Session::Factory

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/event_store/http/session/factory.rb

Constant Summary collapse

UnknownType =
Class.new StandardError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typeObject



11
12
13
# File 'lib/event_store/http/session/factory.rb', line 11

def type
  @type ||= get_type
end

Class Method Details

.build(settings = nil, namespace: nil, type: nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/event_store/http/session/factory.rb', line 15

def self.build(settings=nil, namespace: nil, type: nil)
  settings ||= Settings.instance
  namespace = Array(namespace)

  instance = new settings, namespace
  instance.type = type unless type.nil?
  instance
end

.call(settings = nil, **arguments) ⇒ Object



24
25
26
27
# File 'lib/event_store/http/session/factory.rb', line 24

def self.call(settings=nil, **arguments)
  instance = build settings, **arguments
  instance.()
end

.typesObject



63
64
65
66
67
68
# File 'lib/event_store/http/session/factory.rb', line 63

def self.types
  @types ||= {
    :any_member => AnyMember,
    :leader => Leader
  }
end

Instance Method Details

#callObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/event_store/http/session/factory.rb', line 29

def call
  logger.trace { "Constructing Session (Type: #{type.inspect})" }

  cls = resolve_class

  instance = cls.new

  settings.set instance, namespace

  Retry.configure instance, settings, namespace: namespace
  Log::Data.configure instance, Session, attr_name: :data_logger
  connect = Connect.configure instance, settings, namespace: namespace

  instance.configure

  logger.debug { "Session constructed (Type: #{type.inspect}, Class: #{instance.class})" }

  instance
end

#get_typeObject



49
50
51
52
53
# File 'lib/event_store/http/session/factory.rb', line 49

def get_type
  type = settings.get :type, namespace
  type ||= Defaults.type
  type.to_sym
end

#resolve_classObject



55
56
57
58
59
60
61
# File 'lib/event_store/http/session/factory.rb', line 55

def resolve_class
  self.class.types.fetch type do
    error_message = "Unknown session type (Type: #{type.inspect}, KnownTypes: #{self.class.types.keys.inspect})"
    logger.error { error_message }
    raise UnknownType, error_message
  end
end