Class: Repofetch::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/repofetch/stat.rb

Overview

Base class for stats.

Direct Known Subclasses

TimespanStat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, value, emoji: nil) ⇒ Stat

Creates a stat

Parameters:

  • label (String)

    The label of the stat

  • value

    The value of the stat

  • emoji (String) (defaults to: nil)

    An optional emoji for the stat



14
15
16
17
18
19
# File 'lib/repofetch/stat.rb', line 14

def initialize(label, value, emoji: nil)
  @label = label
  @value = value
  @emoji = emoji
  @label_styles = []
end

Instance Attribute Details

#emojiObject (readonly)

Returns the value of attribute emoji.



6
7
8
# File 'lib/repofetch/stat.rb', line 6

def emoji
  @emoji
end

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'lib/repofetch/stat.rb', line 6

def label
  @label
end

#theme=(value) ⇒ Object (writeonly)

Sets the attribute theme

Parameters:

  • value

    the value to set the attribute theme to.



7
8
9
# File 'lib/repofetch/stat.rb', line 7

def theme=(value)
  @theme = value
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/repofetch/stat.rb', line 6

def value
  @value
end

Instance Method Details

#format(theme = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/repofetch/stat.rb', line 21

def format(theme = nil)
  return to_s if theme.nil?

  emoji = @emoji
  emoji = nil unless Repofetch.config.nil? || Repofetch.config.emojis?
  styled_label = @label_styles.inject(@label) { |label, style| theme.format(style, label) }
  "#{emoji}#{styled_label}: #{format_value}"
end

#format_valueObject

Formats the value of the stat

Simply calls to_s, but can be overridden by subclasses.



33
34
35
# File 'lib/repofetch/stat.rb', line 33

def format_value
  @value.to_s
end

#style_label(style, *styles) ⇒ Stat

Adds one or more styles for the label, returning a new stat

Parameters:

  • style (Symbol)

    The theme's style to add

Returns:

  • (Stat)

    A new stat with the style added



56
57
58
# File 'lib/repofetch/stat.rb', line 56

def style_label(style, *styles)
  dup.tap { |stat| stat.style_label!(style, *styles) }
end

#style_label!(style, *styles) ⇒ Object

Adds one or more styles for the label

Parameters:

  • style (Symbol)

    The theme's style to add

  • styles (Symbol)

    Additional styles to add



47
48
49
50
# File 'lib/repofetch/stat.rb', line 47

def style_label!(style, *styles)
  @label_styles << style
  @label_styles.concat(styles)
end

#to_sObject



37
38
39
40
41
# File 'lib/repofetch/stat.rb', line 37

def to_s
  emoji = @emoji
  emoji = nil unless Repofetch.config.nil? || Repofetch.config.emojis?
  "#{emoji}#{@label}: #{@value}"
end