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?
- .remove_file(file_path) ⇒ void
- .underscore_str(str) ⇒ String
- .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.
75 76 77 |
# File 'lib/pg_eventstore/utils.rb', line 75 def deprecation_warning() PgEventstore.logger&.warn("\e[31m[DEPRECATED]: #{}\e[0m") end |
.error_info(error) ⇒ Hash
Transforms exception instance into a hash
48 49 50 51 52 53 54 |
# File 'lib/pg_eventstore/utils.rb', line 48 def error_info(error) { class: error.class, message: error., backtrace: error.backtrace } 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.
69 70 71 |
# File 'lib/pg_eventstore/utils.rb', line 69 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?
97 98 99 100 101 102 103 |
# File 'lib/pg_eventstore/utils.rb', line 97 def read_pid(file_path) file = File.open(file_path, "r") file.readline.strip.tap do file.close end rescue Errno::ENOENT end |
.remove_file(file_path) ⇒ void
This method returns an undefined value.
90 91 92 93 |
# File 'lib/pg_eventstore/utils.rb', line 90 def remove_file(file_path) File.delete(file_path) rescue Errno::ENOENT end |
.underscore_str(str) ⇒ String
58 59 60 61 62 63 |
# File 'lib/pg_eventstore/utils.rb', line 58 def underscore_str(str) str = str.dup str[0] = str[0].downcase str.gsub!(/[A-Z]/) { |letter| '_' + letter.downcase } str end |
.write_to_file(file_path, content) ⇒ void
This method returns an undefined value.
82 83 84 85 86 |
# File 'lib/pg_eventstore/utils.rb', line 82 def write_to_file(file_path, content) file = File.open(file_path, "w") file.write(content) file.close end |