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

clicked_block[RW]

A proc that will be invoked when the block is clicked.

colors[RW]

A hash of colors to be used by the block.

formats[RW]

A hash of formats to be used by the block.

full_text[RW]

The block’s current text. Might be overwritten by update.

max_length[RW]

The maximum length of the block’s text. Longer texts will be truncated. Ignored if 0 or lower.

name[RW]

The name of the block. Used for click handling. Will be assigned automatically if empty.

text_color[RW]

The block’s current text color. Might be overwritten by update.

Public Class Methods

new(**args, &block) click to toggle source

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

clicked(button, x, y) click to toggle source

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
color() click to toggle source

Returns the default color.

# File lib/r3status/blocks/base.rb, line 89
def color
  colors.default
end
color=(color) click to toggle source

Sets the default color.

# File lib/r3status/blocks/base.rb, line 94
def color=(color)
  colors.default = color
end
format() click to toggle source

Returns the default format string.

# File lib/r3status/blocks/base.rb, line 79
def format
  formats.default 
end
format=(fmt) click to toggle source

Sets the default format string.

# File lib/r3status/blocks/base.rb, line 84
def format=(fmt)
  formats.default = fmt 
end
state() click to toggle source

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
terminate() click to toggle source

Signals the block to release any resources.

# File lib/r3status/blocks/base.rb, line 99
def terminate; end
to_s(prefix: nil, postfix: nil) click to toggle source

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
update() click to toggle source

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