Class: PgEventstore::Utils
- Inherits:
-
Object
- Object
- PgEventstore::Utils
- Defined in:
- lib/pg_eventstore/utils.rb
Class Method Summary collapse
-
.deep_dup(object) ⇒ Object
Deep dup Array or Hash.
-
.deep_transform_keys(object, &block) ⇒ Object
Deep transforms keys of a given Hash.
- .deprecation_warning(message) ⇒ void
-
.error_info(error) ⇒ Hash
Transforms exception instance into a hash.
-
.original_global_position(raw_event) ⇒ Integer
Detect the global position of the event record in the database.
-
.positional_vars(array) ⇒ String
Converts array to the string containing SQL positional variables.
-
.read_pid(file_path) ⇒ String?
rubocop:disable Lint/SuppressedException.
-
.remove_file(file_path) ⇒ void
rubocop:disable Lint/SuppressedException.
- .underscore_str(str) ⇒ String
- .unwrap_exception(wrapped_exception) ⇒ StandardError
- .wrap_exception(exception, **extra) ⇒ PgEventstore::WrappedException
- .write_to_file(file_path, content) ⇒ void
Class Method Details
.deep_dup(object) ⇒ Object
Deep dup Array or Hash
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pg_eventstore/utils.rb', line 25 def deep_dup(object) case object when Hash object.each_with_object({}) do |(key, value), result| result[deep_dup(key)] = deep_dup(value) end when Array object.map { |e| deep_dup(e) } else object.dup end end |
.deep_transform_keys(object, &block) ⇒ Object
Deep transforms keys of a given Hash
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pg_eventstore/utils.rb', line 9 def deep_transform_keys(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| result[yield(key)] = deep_transform_keys(value, &block) end when Array object.map { |e| deep_transform_keys(e, &block) } else object end end |
.deprecation_warning(message) ⇒ void
This method returns an undefined value.
78 79 80 |
# File 'lib/pg_eventstore/utils.rb', line 78 def deprecation_warning() PgEventstore.logger&.warn("\e[31m[DEPRECATED]: #{message}\e[0m") end |
.error_info(error) ⇒ Hash
Transforms exception instance into a hash
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pg_eventstore/utils.rb', line 48 def error_info(error) original_error = unwrap_exception(error) { class: original_error.class, message: original_error., backtrace: original_error.backtrace, }.tap do |attrs| attrs.merge!(error.extra) if error.is_a?(WrappedException) end end |
.original_global_position(raw_event) ⇒ Integer
Detect the global position of the event record in the database. If it is a link event - we pick a global_position of the link instead of picking a global_position of an event this link points to.
72 73 74 |
# File 'lib/pg_eventstore/utils.rb', line 72 def original_global_position(raw_event) raw_event['link'] ? raw_event['link']['global_position'] : raw_event['global_position'] end |
.positional_vars(array) ⇒ String
Converts array to the string containing SQL positional variables
41 42 43 |
# File 'lib/pg_eventstore/utils.rb', line 41 def positional_vars(array) array.size.times.map { |t| "$#{t + 1}" }.join(', ') end |
.read_pid(file_path) ⇒ String?
rubocop:disable Lint/SuppressedException
104 105 106 107 108 109 110 |
# File 'lib/pg_eventstore/utils.rb', line 104 def read_pid(file_path) file = File.open(file_path, 'r') file.readline.strip rescue Errno::ENOENT ensure file&.close end |
.remove_file(file_path) ⇒ void
This method returns an undefined value.
rubocop:disable Lint/SuppressedException
95 96 97 98 |
# File 'lib/pg_eventstore/utils.rb', line 95 def remove_file(file_path) File.delete(file_path) rescue Errno::ENOENT end |
.underscore_str(str) ⇒ String
61 62 63 64 65 66 |
# File 'lib/pg_eventstore/utils.rb', line 61 def underscore_str(str) str = str.dup str[0] = str[0].downcase str.gsub!(/[A-Z]/) { |letter| "_#{letter.downcase}" } str end |
.unwrap_exception(wrapped_exception) ⇒ StandardError
122 123 124 125 126 |
# File 'lib/pg_eventstore/utils.rb', line 122 def unwrap_exception(wrapped_exception) return wrapped_exception.original_exception if wrapped_exception.is_a?(WrappedException) wrapped_exception end |
.wrap_exception(exception, **extra) ⇒ PgEventstore::WrappedException
116 117 118 |
# File 'lib/pg_eventstore/utils.rb', line 116 def wrap_exception(exception, **extra) WrappedException.new(exception, extra) end |
.write_to_file(file_path, content) ⇒ void
This method returns an undefined value.
85 86 87 88 89 90 |
# File 'lib/pg_eventstore/utils.rb', line 85 def write_to_file(file_path, content) file = File.open(file_path, 'w') file.write(content) ensure file&.close end |