Module: Familia::Utils

Included in:
Familia
Defined in:
lib/familia/utils.rb

Instance Method Summary collapse

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/familia/utils.rb', line 10

def debug?
  @debug == true
end

#distinguisher(value_to_distinguish, strict_values = true) ⇒ Object

This method determines the appropriate value to return based on the class of the input argument. It uses a case statement to handle different classes:

  • For Symbol, String, Integer, and Float classes, it traces the operation and converts the value to a string.

  • For Familia::Horreum class, it traces the operation and returns the identifier of the value.

  • For TrueClass, FalseClass, and NilClass, it traces the operation and converts the value to a string (“true”, “false”, or “”).

  • For any other class, it traces the operation and returns nil.

Alternative names for ‘value_to_distinguish` could be `input_value`, `value`, or `object`.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/familia/utils.rb', line 80

def distinguisher(value_to_distinguish, strict_values = true)
  case value_to_distinguish
  when ::Symbol, ::String, ::Integer, ::Float
    Familia.trace :TOREDIS_DISTINGUISHER, redis, "string", caller(1..1) if Familia.debug?
    # Symbols and numerics are naturally serializable to strings
    # so it's a relatively low risk operation.
    value_to_distinguish.to_s

  when ::TrueClass, ::FalseClass, ::NilClass
    Familia.trace :TOREDIS_DISTINGUISHER, redis, "true/false/nil", caller(1..1) if Familia.debug?
    # TrueClass, FalseClass, and NilClass are high risk because we can't
    # reliably determine the original type of the value from the serialized
    # string. This can lead to unexpected behavior when deserializing. For
    # example, if a TrueClass value is serialized as "true" and then later
    # deserialized as a String, it can cause errors in the application. Worse
    # still, if a NilClass value is serialized as an empty string we lose the
    # ability to distinguish between a nil value and an empty string when
    #
    raise Familia::HighRiskFactor, value_to_distinguish if strict_values
    value_to_distinguish.to_s #=> "true", "false", ""

  when Familia::Base, Class
    Familia.trace :TOREDIS_DISTINGUISHER, redis, "base", caller(1..1) if Familia.debug?
    if value_to_distinguish.is_a?(Class)
      value_to_distinguish.name
    else
      value_to_distinguish.identifier
    end

  else
    Familia.trace :TOREDIS_DISTINGUISHER, redis, "else1 #{strict_values}", caller(1..1) if Familia.debug?

    if value_to_distinguish.class.ancestors.member?(Familia::Base)
      Familia.trace :TOREDIS_DISTINGUISHER, redis, "isabase", caller(1..1) if Familia.debug?
      value_to_distinguish.identifier

    else
      Familia.trace :TOREDIS_DISTINGUISHER, redis, "else2 #{strict_values}", caller(1..1) if Familia.debug?
      raise Familia::HighRiskFactor, value_to_distinguish if strict_values
      nil
    end
  end
end

#generate_idObject



14
15
16
17
# File 'lib/familia/utils.rb', line 14

def generate_id
  input = SecureRandom.hex(32)  # 16=128 bits, 32=256 bits
  Digest::SHA256.hexdigest(input).to_i(16).to_s(36) # base-36 encoding
end

#generate_sha_hash(*elements) ⇒ Object



67
68
69
70
# File 'lib/familia/utils.rb', line 67

def generate_sha_hash(*elements)
  concatenated_string = Familia.join(*elements)
  DIGEST_CLASS.hexdigest(concatenated_string)
end

#join(*val) ⇒ Object



19
20
21
# File 'lib/familia/utils.rb', line 19

def join(*val)
  val.compact.join(Familia.delim)
end

#now(name = Time.now) ⇒ Object



48
49
50
# File 'lib/familia/utils.rb', line 48

def now(name = Time.now)
  name.utc.to_f
end

#qnow(quantum = 10.minutes, now = Familia.now) ⇒ Object

A quantized timestamp e.g. 12:32 -> 12:30



55
56
57
58
# File 'lib/familia/utils.rb', line 55

def qnow(quantum = 10.minutes, now = Familia.now)
  rounded = now - (now % quantum)
  Time.at(rounded).utc.to_i
end

#qstamp(quantum = nil, pattern = nil, now = Familia.now) ⇒ Object



60
61
62
63
64
65
# File 'lib/familia/utils.rb', line 60

def qstamp(quantum = nil, pattern = nil, now = Familia.now)
  quantum ||= ttl || 10.minutes
  pattern ||= '%H%M'
  rounded = now - (now % quantum)
  Time.at(rounded).utc.strftime(pattern)
end

#rediskey(*val) ⇒ Object



27
28
29
# File 'lib/familia/utils.rb', line 27

def rediskey(*val)
  join(*val)
end

#redisuri(uri) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/familia/utils.rb', line 31

def redisuri(uri)
  generic_uri = URI.parse(uri.to_s)

  # Create a new URI::Redis object
  redis_uri = URI::Redis.build(
    scheme: generic_uri.scheme,
    userinfo: generic_uri.userinfo,
    host: generic_uri.host,
    port: generic_uri.port,
    path: generic_uri.path,
    query: generic_uri.query,
    fragment: generic_uri.fragment
  )

  redis_uri
end

#split(val) ⇒ Object



23
24
25
# File 'lib/familia/utils.rb', line 23

def split(val)
  val.split(Familia.delim)
end