Sha256: 5d4f232da72db5bc7a68e69a5aa8d86a59adfbdabe47ac6da41f45b8afa8c715

Contents?: true

Size: 1.73 KB

Versions: 17

Compression:

Stored size: 1.73 KB

Contents

require 'pry-state/printer'

class HookAction
  IGNORABLE_LOCAL_VARS = [:__, :_, :_ex_, :_pry_, :_out_, :_in_, :_dir_, :_file_]

  CONTROLLER_INSTANCE_VARIABLES = [:@_request, :@_response, :@_routes]
  RSPEC_INSTANCE_VARIABLES = [:@__inspect_output, :@__memoized]
  IGNORABLE_INSTANCE_VARS = CONTROLLER_INSTANCE_VARIABLES + RSPEC_INSTANCE_VARIABLES
  IGNORABLE_GLOBAL_VARS = [:$@]

  def initialize binding, pry
    @binding, @pry = binding, pry
  end

  def act
    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
  attr_reader :binding, :pry

  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

  def value_changed? var, value
    prev_state[var] and prev_state[var] != value
  end

  def stick_value! var, value
    prev_state[var] = value
  end

  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

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pryx-0.8.6 lib/pry-state/hook_action.rb
pryx-0.8.5 lib/pry-state/hook_action.rb
pryx-0.8.4 lib/pry-state/hook_action.rb
pryx-0.8.3 lib/pry-state/hook_action.rb
pryx-0.8.2 lib/pry-state/hook_action.rb
pryx-0.8.1 lib/pry-state/hook_action.rb
pryx-0.8.0 lib/pry-state/hook_action.rb
pryx-0.7.2 lib/pry-state/hook_action.rb
pryx-0.7.0 lib/pry-state/hook_action.rb
pryx-0.6.1 lib/pry-state/hook_action.rb
pryx-0.6.0 lib/pry-state/hook_action.rb
pryx-0.5.0 lib/pry-state/hook_action.rb
pryx-0.4.4 lib/pry-state/hook_action.rb
pryx-0.4.3 lib/pry-state/hook_action.rb
pryx-0.4.2 lib/pry-state/hook_action.rb
pryx-0.4.1 lib/pry-state/hook_action.rb
pryx-0.4.0 lib/pry-state/hook_action.rb