Class: HakumiComponents::Progress::InfoValue

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/progress/info_value.rb

Constant Summary collapse

ProgressValue =
T.type_alias { Numeric }
FormatProc =
T.type_alias do
  T.proc.params(
    percent: ProgressValue,
    success_percent: T.nilable(ProgressValue)
  ).returns(String)
end
FormatValue =
T.type_alias { T.nilable(T.any(Types::Renderable, FormatProc)) }

Instance Method Summary collapse

Constructor Details

#initialize(percent:, success_percent:, content:, format_value:, rendered_format:) ⇒ InfoValue

Returns a new instance of InfoValue.



27
28
29
30
31
32
33
# File 'app/components/hakumi_components/progress/info_value.rb', line 27

def initialize(percent:, success_percent:, content:, format_value:, rendered_format:)
  @percent = T.let(percent, ProgressValue)
  @success_percent = T.let(success_percent, T.nilable(ProgressValue))
  @content = T.let(content, T.untyped)
  @format_value = T.let(format_value, FormatValue)
  @rendered_format = T.let(rendered_format, T.untyped)
end

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/components/hakumi_components/progress/info_value.rb', line 53

def custom?
  present?(@content) || !@format_value.nil?
end

#percent_labelObject



58
59
60
# File 'app/components/hakumi_components/progress/info_value.rb', line 58

def percent_label
  whole_number?(@percent.to_f) ? "#{@percent.to_i}%" : "#{@percent}%"
end

#textObject



36
37
38
39
40
41
42
43
44
# File 'app/components/hakumi_components/progress/info_value.rb', line 36

def text
  return @content if present?(@content)

  format_value = @format_value
  return format_value.call(@percent, @success_percent) if format_value.is_a?(Proc)
  return @rendered_format if present?(@rendered_format)

  percent_label
end

#tooltip_labelObject



47
48
49
50
# File 'app/components/hakumi_components/progress/info_value.rb', line 47

def tooltip_label
  value = text
  present?(value) ? value.to_s : percent_label
end