class R3Status::Blocks::Base
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
Constants
- DEFAULT_COLOR
The default text color. Will be used if no color was supplied, or
nil
was given.- DEFAULT_FORMAT
The default format. Will be used if no format was supplied, or
nil
was given.
Attributes
A proc that will be invoked when the block is clicked.
A hash of colors to be used by the block.
A hash of formats to be used by the block.
The block’s current text. Might be overwritten by update
.
The maximum length of the block’s text. Longer texts will be truncated. Ignored if 0 or lower.
The name of the block. Used for click handling. Will be assigned automatically if empty.
The block’s current text color. Might be overwritten by update
.
Public Class Methods
Creates a new instance of this class. If a block is passed, it will be stored and yielded when the block is clicked.
# 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
Public Instance Methods
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.
# File lib/r3status/blocks/base.rb, line 58 def clicked(button, x, y) return false if @clicked_block.nil? args = [self, button, x, y].take(@clicked_block.arity) @clicked_block.(*args) return true end
Returns the default color.
# File lib/r3status/blocks/base.rb, line 89 def color colors.default end
Sets the default color.
# File lib/r3status/blocks/base.rb, line 94 def color=(color) colors.default = color end
Returns the default format string.
# File lib/r3status/blocks/base.rb, line 79 def format formats.default end
Sets the default format string.
# File lib/r3status/blocks/base.rb, line 84 def format=(fmt) formats.default = fmt end
When implemented in derived class, returns the current state of the block, if available.
# File lib/r3status/blocks/base.rb, line 103 def state; end
Signals the block to release any resources.
# File lib/r3status/blocks/base.rb, line 99 def terminate; end
Returns the string representation of this block.
# 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
When implemented in derived classes, updates the text and color of this block. Implmentations should set the full_text
and text_color
attributes.
# File lib/r3status/blocks/base.rb, line 50 def update; end