Class: ActiveRecordConnectionTz::ConnectionSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_connection_tz/connection_settings.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ ConnectionSettings

: (Hash[untyped, untyped]? raw) -> void



30
31
32
33
34
35
# File 'lib/active_record_connection_tz/connection_settings.rb', line 30

def initialize(raw)
  raw = normalize_hash(raw)

  @time_zone_name = raw.fetch('time_zone', nil)
  @explicit_mysql_session_time_zone = raw.fetch('mysql_session_time_zone', nil)
end

Instance Attribute Details

#explicit_mysql_session_time_zoneObject (readonly)

: String?



20
21
22
# File 'lib/active_record_connection_tz/connection_settings.rb', line 20

def explicit_mysql_session_time_zone
  @explicit_mysql_session_time_zone
end

#time_zone_nameObject (readonly)

: String?



19
20
21
# File 'lib/active_record_connection_tz/connection_settings.rb', line 19

def time_zone_name
  @time_zone_name
end

Class Method Details

.from_config(config) ⇒ Object

: (Hash[untyped, untyped] config) -> ConnectionSettings



24
25
26
# File 'lib/active_record_connection_tz/connection_settings.rb', line 24

def from_config(config)
  new(config[KEY] || config[KEY.to_sym])
end

Instance Method Details

#apply_mysql_variables(config) ⇒ Object

: (Hash[untyped, untyped] config) -> Hash[untyped, untyped]



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_record_connection_tz/connection_settings.rb', line 75

def apply_mysql_variables(config)
  return config unless enabled?

  variables = normalize_hash(config['variables'] || config[:variables], context: 'connection_tz.variables')
  return config if variables['time_zone']

  session_time_zone = mysql_session_time_zone || raise(
    ConfigurationError,
    "connection_tz time_zone #{time_zone_name.inspect} is not fixed-offset " \
    'between 1970 and 2050; set mysql_session_time_zone explicitly'
  )

  variables['time_zone'] = session_time_zone

  config.merge(variables: variables.dup, 'variables' => variables.dup)
end

#enabled?Boolean

: () -> bool

Returns:

  • (Boolean)


38
39
40
# File 'lib/active_record_connection_tz/connection_settings.rb', line 38

def enabled?
  !time_zone_name.to_s.empty?
end

#mysql_session_time_zoneObject

: () -> String?



70
71
72
# File 'lib/active_record_connection_tz/connection_settings.rb', line 70

def mysql_session_time_zone
  explicit_mysql_session_time_zone || auto_mysql_session_time_zone
end

#time_zoneObject

: () -> ActiveSupport::TimeZone?



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record_connection_tz/connection_settings.rb', line 43

def time_zone
  return unless enabled?

  # `enabled?` guarantees `time_zone_name` is present here.
  # The `|| raise` keeps Steep's type narrowed from `String?` to `String`.
  name = time_zone_name || raise(ConfigurationError, 'connection_tz time_zone must be present')

  @time_zone ||= ActiveSupport::TimeZone[name] || raise(
    ConfigurationError,
    "Unknown connection_tz time_zone: #{name.inspect}"
  )
end

#type_map_keyObject

: () -> Hash[String, String]?



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record_connection_tz/connection_settings.rb', line 57

def type_map_key
  return unless enabled?

  # `enabled?` guarantees `time_zone_name` is present here.
  # The `|| raise` keeps Steep's type narrowed from `String?` to `String`.
  name = time_zone_name || raise(ConfigurationError, 'connection_tz time_zone must be present')

  {
    'time_zone' => name
  }
end