Class: Hlockey::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/hlockey/message.rb

Overview

Can be sent by a Game (in Game#stream) or a commonly used text

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, **data) ⇒ Message

Returns a new instance of Message.

Parameters:

  • event (Symbol)
  • data (Hash<Symbol => Object>)

    data for the message



15
16
17
18
# File 'lib/hlockey/message.rb', line 15

def initialize(event, **data)
  @event = event
  @data = data
end

Class Attribute Details

.colorize=(value) ⇒ Object (writeonly)

Variable controlling how coloring players/teams is done Set this to a Proc that takes params ‘color` and `string`, and returns the colorized string. `color` will be in hex format, for example “#ffffff” The default is to not color things at all



34
35
36
# File 'lib/hlockey/message.rb', line 34

def colorize=(value)
  @colorize = value
end

Instance Attribute Details

#eventSymbol (readonly)

Returns an action or event the message represents.

Returns:

  • (Symbol)

    an action or event the message represents



11
12
13
# File 'lib/hlockey/message.rb', line 11

def event
  @event
end

Class Method Details

.color(obj, do_color: true) ⇒ Object

Colors Team/associated classes in their team color To control how this colors things, see #colorize=

Parameters:

Returns:

  • String



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hlockey/message.rb', line 40

def color(obj, do_color: true)
  return obj.to_s unless do_color

  case obj
  when Team
    @colorize.call(obj.color, obj.to_s)
  when Team::Player, Team::Stadium
    @colorize.call(obj.team.color, obj.to_s)
  else
    obj.to_s
  end
end

Instance Method Details

#to_s(do_color: true) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/hlockey/message.rb', line 20

def to_s(do_color: true)
  return format(Data.messages[@event], **@data) unless do_color

  colorized_data = @data.transform_values { self.class.color(_1, do_color:) }

  format(Data.messages[@event], **colorized_data)
end