class Scryglass::Config

Attributes

cursor_tracking[RW]
dot_coloring[RW]
include_empty_associations[RW]
include_scoped_associations[RW]
include_through_associations[RW]
lenses[RW]
show_association_types[RW]
tab_length[RW]
tree_view_key_string_clip_length[RW]
tree_view_value_string_clip_length[RW]

Public Class Methods

new() click to toggle source
# File lib/scryglass/config.rb, line 17
def initialize
  ## Display
  self.tab_length = 2 # You can make it 0, but please don't make it 0.
  self.tree_view_key_string_clip_length = 200
  self.tree_view_value_string_clip_length = 500
  self.dot_coloring = true

  ## UX
  self.cursor_tracking = [:flexible_range, :dead_center][0] # One or the other
  self.lenses = [ # Custom lenses can easily be added as name+lambda hashes! Or comment some out to turn them off.
    { name: 'Pretty Print (`pp`)',
      lambda: ->(o) { Hexes.capture_io(char_limit: 20_000) { pp o } } },
    { name: 'Amazing Print (`ap`)',
      lambda: ->(o) { Hexes.capture_io(char_limit: 20_000) { ap o } } }, # This has colors!
    { name: 'Inspect (`.inspect`)',
      lambda: ->(o) { Hexes.capture_io(char_limit: 20_000) { puts o.inspect } } },
    { name: 'Yaml Print (`y`)',
      lambda: ->(o) { Hexes.capture_io(char_limit: 20_000) { require 'yaml' ; y o } } }, # OR: `puts o.to_yaml`
    { name: 'Puts (`puts`)',
      lambda: ->(o) { Hexes.capture_io(char_limit: 20_000) { puts o } } },
    { name: 'Method Showcase',
      lambda: ->(o) { Scryglass::LensHelper.method_showcase_for(o, char_limit: 20_000) } },
  ]

  ## AmazingPrint defaults, if the user has not set their own:
  ::AmazingPrint.defaults ||= {
    index: false,  # (Don't display array indices).
    raw:   true,   # (Recursively format instance variables).
  }
  # See https://github.com/amazing-print/amazing_print

  ## Building ActiveRecord association sub-rows:
  self.include_empty_associations = true
  self.include_through_associations = false
  self.include_scoped_associations = false
  self.show_association_types = true
end

Public Instance Methods

validate!() click to toggle source
# File lib/scryglass/config.rb, line 55
def validate!
  validate_boolean_attrs!
  validate_positive_integer_attrs!
  validate_cursor_tracking_options!
  validate_lenses!
end

Private Instance Methods

validate_boolean_attrs!() click to toggle source
# File lib/scryglass/config.rb, line 64
def validate_boolean_attrs!
  bool_attrs = [
    :include_empty_associations,
    :include_through_associations,
    :include_scoped_associations,
    :dot_coloring,
  ]
  bool_attrs.each do |bool_attr|
    value = send(bool_attr)
    unless [true, false].include?(value)
      raise ArgumentError, "#{bool_attr} must be true or false."
    end
  end
end
validate_cursor_tracking_options!() click to toggle source
# File lib/scryglass/config.rb, line 93
def validate_cursor_tracking_options!
  cursor_tracking_options = [:flexible_range, :dead_center]
  unless cursor_tracking_options.include?(cursor_tracking)
    raise ArgumentError, "#{cursor_tracking.inspect} not in " \
                         "[#{cursor_tracking_options.map(&:inspect).join(', ')}]."
  end
end
validate_lenses!() click to toggle source
# File lib/scryglass/config.rb, line 101
def validate_lenses!
  raise ArgumentError, 'lenses cannot be empty' unless lenses.any?

  lenses.each do |lens|
    unless lens.is_a?(Hash) && lens[:name].is_a?(String) && lens[:lambda].lambda?
      raise ArgumentError, "Lens #{lens.inspect} must be a hash of the form:" \
                           '{ name: String, lambda: lambda }'
    end
  end
end
validate_positive_integer_attrs!() click to toggle source
# File lib/scryglass/config.rb, line 79
def validate_positive_integer_attrs!
  positive_integer_attrs = [
    :tab_length,
    :tree_view_key_string_clip_length,
    :tree_view_value_string_clip_length,
  ]
  positive_integer_attrs.each do |int_attr|
    value = send(int_attr)
    unless value.integer? && value.positive?
      raise ArgumentError, "#{value} is not a positive integer."
    end
  end
end