Class: PgEventstore::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_eventstore/utils.rb

Class Method Summary collapse

Class Method Details

.deep_dup(object) ⇒ Object

Deep dup Array or Hash

Parameters:

  • object (Object)

Returns:

  • (Object)


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

Parameters:

  • object (Object)

Returns:

  • (Object)

    a hash with transformed keys



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.

Parameters:

  • message (String)


75
76
77
# File 'lib/pg_eventstore/utils.rb', line 75

def deprecation_warning(message)
  PgEventstore.logger&.warn("\e[31m[DEPRECATED]: #{message}\e[0m")
end

.error_info(error) ⇒ Hash

Transforms exception instance into a hash

Parameters:

  • error (StandardError)

Returns:

  • (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.message,
    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.

Parameters:

  • raw_event (Hash)

Returns:

  • (Integer)


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

Parameters:

  • array (Array)

Returns:

  • (String)

    positional variables, based on array size. Example: “$1, $2, $3”



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?

Parameters:

  • file_path (String)

Returns:

  • (String, nil)


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.

Parameters:

  • file_path (String)


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

Parameters:

  • str (String)

Returns:

  • (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.

Parameters:

  • file_path (String)
  • content (String)


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