Class: R3Status::Blocks::Base
- Inherits:
-
Object
- Object
- R3Status::Blocks::Base
- Defined in:
- lib/r3status/blocks/base.rb
Overview
A base class for blocks, providing the needed methods and attributes. Can be use both as a base for other blocks and as a block in itself.
Examples
b = Blocks::Base.new(full_text: "0") do |block, button| block.full_text = button.to_s end
Constant Summary collapse
- DEFAULT_COLOR =
The default text color. Will be used if no color was supplied, or
nilwas given. '#ffffff'- DEFAULT_FORMAT =
The default format. Will be used if no format was supplied, or
nilwas given. '%{val}'
Instance Attribute Summary collapse
-
#clicked_block ⇒ Object
A proc that will be invoked when the block is clicked.
-
#colors ⇒ Object
A hash of colors to be used by the block.
-
#formats ⇒ Object
A hash of formats to be used by the block.
-
#full_text ⇒ Object
The block's current text.
-
#max_length ⇒ Object
The maximum length of the block's text.
-
#name ⇒ Object
The name of the block.
-
#text_color ⇒ Object
The block's current text color.
Instance Method Summary collapse
-
#clicked(button, x, y) ⇒ Object
Handles mouse interaction with the block.
-
#color ⇒ Object
Returns the default color.
-
#color=(color) ⇒ Object
Sets the default color.
-
#format ⇒ Object
Returns the default format string.
-
#format=(fmt) ⇒ Object
Sets the default format string.
-
#initialize(**args, &block) ⇒ Base
constructor
Creates a new instance of this class.
-
#state ⇒ Object
When implemented in derived class, returns the current state of the block, if available.
-
#terminate ⇒ Object
Signals the block to release any resources.
-
#to_s(prefix: nil, postfix: nil) ⇒ Object
Returns the string representation of this block.
-
#update ⇒ Object
When implemented in derived classes, updates the text and color of this block.
Constructor Details
#initialize(**args, &block) ⇒ Base
Creates a new instance of this class. If a block is passed, it will be stored and yielded when the block is clicked.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/r3status/blocks/base.rb', line 34 def initialize(**args, &block) args = {colors: {}, formats: Hash.new(DEFAULT_FORMAT), max_length: 0}.merge(args) args.each do |k, v| v.default = v[:default] if (v.is_a? Hash) && (v.key? :default) send "#{k}=", v end @clicked_block = block @name ||= SecureRandom.uuid @full_text ||= format formats.default ||= DEFAULT_FORMAT end |
Instance Attribute Details
#clicked_block ⇒ Object
A proc that will be invoked when the block is clicked.
30 31 32 |
# File 'lib/r3status/blocks/base.rb', line 30 def clicked_block @clicked_block end |
#colors ⇒ Object
A hash of colors to be used by the block.
28 29 30 |
# File 'lib/r3status/blocks/base.rb', line 28 def colors @colors end |
#formats ⇒ Object
A hash of formats to be used by the block.
26 27 28 |
# File 'lib/r3status/blocks/base.rb', line 26 def formats @formats end |
#full_text ⇒ Object
The block's current text. Might be overwritten by #update.
20 21 22 |
# File 'lib/r3status/blocks/base.rb', line 20 def full_text @full_text end |
#max_length ⇒ Object
The maximum length of the block's text. Longer texts will be truncated. Ignored if 0 or lower.
18 19 20 |
# File 'lib/r3status/blocks/base.rb', line 18 def max_length @max_length end |
#name ⇒ Object
The name of the block. Used for click handling. Will be assigned automatically if empty.
24 25 26 |
# File 'lib/r3status/blocks/base.rb', line 24 def name @name end |
#text_color ⇒ Object
The block's current text color. Might be overwritten by #update.
22 23 24 |
# File 'lib/r3status/blocks/base.rb', line 22 def text_color @text_color end |
Instance Method Details
#clicked(button, x, y) ⇒ Object
Handles mouse interaction with the block. If a block was supplied to the constructor, it will be yielded.
- button (Fixnum)
The mouse button that was used.
- x, y (Fixnum / Float)
The pointer coordinates on the screen.
Returns true if the click is handled, false otherwise.
58 59 60 61 62 63 |
# File 'lib/r3status/blocks/base.rb', line 58 def clicked(, x, y) return false if @clicked_block.nil? args = [self, , x, y].take(@clicked_block.arity) @clicked_block.(*args) return true end |
#color ⇒ Object
Returns the default color.
89 90 91 |
# File 'lib/r3status/blocks/base.rb', line 89 def color colors.default end |
#color=(color) ⇒ Object
Sets the default color.
94 95 96 |
# File 'lib/r3status/blocks/base.rb', line 94 def color=(color) colors.default = color end |
#format ⇒ Object
Returns the default format string.
79 80 81 |
# File 'lib/r3status/blocks/base.rb', line 79 def format formats.default end |
#format=(fmt) ⇒ Object
Sets the default format string.
84 85 86 |
# File 'lib/r3status/blocks/base.rb', line 84 def format=(fmt) formats.default = fmt end |
#state ⇒ Object
When implemented in derived class, returns the current state of the block, if available.
103 |
# File 'lib/r3status/blocks/base.rb', line 103 def state; end |
#terminate ⇒ Object
Signals the block to release any resources.
99 |
# File 'lib/r3status/blocks/base.rb', line 99 def terminate; end |
#to_s(prefix: nil, postfix: nil) ⇒ Object
Returns the string representation of this block.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/r3status/blocks/base.rb', line 66 def to_s(prefix: nil, postfix: nil) txt = if max_length > 0 && full_text.length > max_length "#{prefix}#{full_text[0..(max_length - 2)]}…#{postfix}" else "#{prefix}#{full_text}#{postfix}" end %({"full_text": "#{txt}", "color": "#{text_color || DEFAULT_COLOR}", "name": "#{name}", "separator": false}) end |
#update ⇒ Object
When implemented in derived classes, updates the text and color of this block. Implmentations should set the #full_text and #text_color attributes.
50 |
# File 'lib/r3status/blocks/base.rb', line 50 def update; end |