Module: Zif::Traceable

Overview

This mixin helps you wrap calling out to Services::TickTraceService by providing a local #mark method.

Messages logged using this #mark method are automatically prefixed with the class name using #mark_prefix Additionally, it sets up an attribute #tracer_service_name you can use to avoid having to dig into your $services hash. Therefore, the only requirement is that you set up the $services global variable to be an instance of Services::ServiceGroup. This is done for you if you are using Game.

Instance Attribute Summary collapse

2. Private-ish methods collapse

Instance Method Summary collapse

Instance Attribute Details

#tracer_service_nameSymbol

Returns The name of your Services::TickTraceService as registered in $services (Services::ServiceGroup).

Returns:



10
11
12
# File 'lib/zif/traceable.rb', line 10

def tracer_service_name
  @tracer_service_name
end

Instance Method Details

#mark(msg = 'Oh Hi Mark') ⇒ Object

This marks a section of code you wish to add to your tick traces to #tracer. It adds the class name to your message via invoking #mark_prefix for you.

Parameters:

  • msg (String) (defaults to: 'Oh Hi Mark')

    The message you wish to log for this section of code.



20
21
22
# File 'lib/zif/traceable.rb', line 20

def mark(msg='Oh Hi Mark')
  tracer&.mark("#{mark_prefix}#{msg}")
end

#mark_and_print(msg = 'Oh Hi Mark', frequency: 1) ⇒ Object

Same as #mark but also sends the output to puts so it will appear in your console.

Parameters:

  • msg (String) (defaults to: 'Oh Hi Mark')

    The message you wish to log for this section of code.

  • frequency (Integer) (defaults to: 1)

    The frequency at which you wish to print to console, every 1, 6, 60, or 600 ticks



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zif/traceable.rb', line 27

def mark_and_print(msg='Oh Hi Mark', frequency: 1)
  full_msg = "#{mark_prefix}#{msg}"
  case frequency
  when 6
    puts6 full_msg
  when 60
    puts60 full_msg
  when 600
    puts600 full_msg
  else
    puts full_msg
  end
  mark(msg)
end

#mark_prefixString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

This only prints the class name, figure out some way of printing the name of the caller method

Returns “<class name>: ”.

Returns:

  • (String)

    “<class name>: ”



48
49
50
# File 'lib/zif/traceable.rb', line 48

def mark_prefix
  "#{self.class.name}: "
end

#tracer(service_name = @tracer_service_name) ⇒ Zif::Services::TickTraceService

Returns your active instance of the Services::TickTraceService

Returns:



13
14
15
# File 'lib/zif/traceable.rb', line 13

def tracer(service_name=@tracer_service_name)
  $services[service_name.to_sym]
end