Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/extension/core/string.rb

Constant Summary collapse

NUMBER_HELPER =
ActiveSupport::NumberHelper

Instance Method Summary collapse

Instance Method Details

#classify(keep_plural: false) ⇒ Object

Extending active support classify to allow leaving the s on the end



37
38
39
40
41
42
43
# File 'lib/esm/extension/core/string.rb', line 37

def classify(keep_plural: false)
  if keep_plural
    sub(/.*\./, "").camelize
  else
    ActiveSupport::Inflector.classify(self)
  end
end

#discord_id?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/esm/extension/core/string.rb', line 10

def discord_id?
  ESM::Regex::DISCORD_ID_ONLY.match?(self)
end

#steam_uid?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/esm/extension/core/string.rb', line 6

def steam_uid?
  ESM::Regex::STEAM_UID_ONLY.match?(self)
end

#to_deep_hObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/esm/extension/core/string.rb', line 47

def to_deep_h
  recursive_convert = lambda do |object|
    case object
    when Array
      object.map { |v| recursive_convert.call(v) }
    when String
      parsed = object.parse_json

      # If it parsed successfully as JSON, recursively convert it
      if parsed.is_a?(Array) || parsed.is_a?(Hash)
        recursive_convert.call(parsed)
      else
        object
      end
    when Hash
      object.transform_values { |v| recursive_convert.call(v) }
    else
      object
    end
  end

  result = self.parse_json
  return if result.nil?

  recursive_convert.call(result)
end

#to_delimitedObject



32
33
34
# File 'lib/esm/extension/core/string.rb', line 32

def to_delimited
  NUMBER_HELPER.number_to_delimited(self)
end

#to_poptabObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/esm/extension/core/string.rb', line 14

def to_poptab
  # Convert from Scientific notation.
  # Arma automatically converts to scientific notation if the value is greater than 2mil
  value = "%f" % self
  value = NUMBER_HELPER.number_to_currency(value, unit: I18n.t(:poptabs), format: "%n %u", precision: 0)

  # 1 will convert to "1 poptabs", this makes it "1 poptab"
  if self == "1"
    value.gsub(I18n.t(:poptabs), I18n.t(:poptab))
  else
    value
  end
end

#to_readable(precision: 0) ⇒ Object



28
29
30
# File 'lib/esm/extension/core/string.rb', line 28

def to_readable(precision: 0)
  NUMBER_HELPER.number_to_currency("%f" % self, format: "%n", precision: precision)
end