Class: Repofetch::Stat
- Inherits:
-
Object
- Object
- Repofetch::Stat
- Defined in:
- lib/repofetch/stat.rb
Overview
Base class for stats.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#emoji ⇒ Object
readonly
Returns the value of attribute emoji.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#theme ⇒ Object
writeonly
Sets the attribute theme.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #format(theme = nil) ⇒ Object
-
#format_value ⇒ Object
Formats the value of the stat.
-
#initialize(label, value, emoji: nil) ⇒ Stat
constructor
Creates a stat.
-
#style_label(style, *styles) ⇒ Stat
Adds one or more styles for the label, returning a new stat.
-
#style_label!(style, *styles) ⇒ Object
Adds one or more styles for the label.
- #to_s ⇒ Object
Constructor Details
#initialize(label, value, emoji: nil) ⇒ Stat
Creates a 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
#emoji ⇒ Object (readonly)
Returns the value of attribute emoji.
6 7 8 |
# File 'lib/repofetch/stat.rb', line 6 def emoji @emoji end |
#label ⇒ Object (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
7 8 9 |
# File 'lib/repofetch/stat.rb', line 7 def theme=(value) @theme = value end |
#value ⇒ Object (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_value ⇒ Object
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
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
47 48 49 50 |
# File 'lib/repofetch/stat.rb', line 47 def style_label!(style, *styles) @label_styles << style @label_styles.concat(styles) end |