class HookAction

Constants

CONTROLLER_INSTANCE_VARIABLES
IGNORABLE_GLOBAL_VARS
IGNORABLE_INSTANCE_VARS
IGNORABLE_LOCAL_VARS
RSPEC_INSTANCE_VARIABLES

Attributes

binding[R]
pry[R]

Public Class Methods

new(binding, pry) click to toggle source
# File lib/pry-state/hook_action.rb, line 11
def initialize binding, pry
  @binding, @pry = binding, pry
end

Public Instance Methods

act() click to toggle source
# File lib/pry-state/hook_action.rb, line 15
def act
  # when using guard, locals :e, :lib, :pry_state_prev get printed.
  # this 'if' cuts them off.
  if @binding.eval("self.class") == Object
    return
  end

  if ENV['SHOW_GLOBAL_VARIABLES']
    (binding.eval('global_variables').sort - IGNORABLE_GLOBAL_VARS).each do |var|
      eval_and_print var, var_color: 'white', value_colore: 'yellow'
    end
  end

  unless ENV['HIDE_INSTANCE_VARIABLES']
    (binding.eval('instance_variables').sort - IGNORABLE_INSTANCE_VARS).each do |var|
      eval_and_print var, var_color: 'green'
    end
  end

  unless ENV['HIDE_LOCAL_VARIABLES']
    (binding.eval('local_variables').sort - IGNORABLE_LOCAL_VARS).each do |var|
      eval_and_print var, var_color: 'cyan'
    end
  end
end

Private Instance Methods

eval_and_print(var, var_color: 'green', value_color: 'white') click to toggle source
# File lib/pry-state/hook_action.rb, line 44
def eval_and_print var, var_color: 'green', value_color: 'white'
  value = binding.eval(var.to_s)
  if value_changed? var, value
    var_color = "bright_#{var_color}"; value_color = 'bright_yellow'
  end
  PryState::Printer.trunc_and_print var, value, var_color, value_color
  stick_value! var, value # to refer the value in next
end
prev_state() click to toggle source
# File lib/pry-state/hook_action.rb, line 61
def prev_state
  # init a hash to store state to be used in next session
  # in finding diff
  pry.config.extra_sticky_locals[:pry_state_prev] ||= {}
end
stick_value!(var, value) click to toggle source
# File lib/pry-state/hook_action.rb, line 57
def stick_value! var, value
  prev_state[var] = value
end
value_changed?(var, value) click to toggle source
# File lib/pry-state/hook_action.rb, line 53
def value_changed? var, value
  prev_state[var] and prev_state[var] != value
end