class JsonInspector::Context

Constants

OMITTED_VALUE

Attributes

doc[R]
filename[R]
options[R]
stack[R]

Public Class Methods

new(filename, options = {}) click to toggle source
# File lib/json_inspector/context.rb, line 9
def initialize(filename, options = {})
  @options  = options
  @filename = filename
  @basename = ::File.basename(filename)

  @doc   = ::MultiJson.load(::File.read(filename))
  @stack = Stack.new(doc)
end

Public Instance Methods

_prompt_() click to toggle source
# File lib/json_inspector/context.rb, line 18
def _prompt_
  [
    ->(obj, nest_level, _) { "inspect(#{@basename}:#{@stack.path})>" },
    ->(obj, nest_level, _) { "inspect(#{@basename}:#{@stack.path})*" }
  ]
end
c()
Alias for: current
current() click to toggle source
# File lib/json_inspector/context.rb, line 82
def current
  @stack.current
end
Also aliased as: c
f(query)
Alias for: find
find(query) click to toggle source
# File lib/json_inspector/context.rb, line 31
def find(query)
  enumerator('', ::Float::INFINITY).select { |key, value| value == query }.map { |key, _| key }
end
Also aliased as: f
find_key(query) click to toggle source
# File lib/json_inspector/context.rb, line 25
def find_key(query)
  enumerator('', ::Float::INFINITY).select { |key, _| key.split(?.).include?(query) }.map { |key, _| key }
end
Also aliased as: fk
fk(query)
Alias for: find_key
i(selector)
Alias for: into
into(selector) click to toggle source
# File lib/json_inspector/context.rb, line 55
def into(selector)
  @stack.push(selector)
  current
end
Also aliased as: i
k(selector = '')
Alias for: keys
keys(selector = '') click to toggle source
# File lib/json_inspector/context.rb, line 48
def keys(selector = '')
  value = @stack.current(selector)
  get_keys(value)
end
Also aliased as: k
o()
Alias for: out
out() click to toggle source
# File lib/json_inspector/context.rb, line 68
def out
  @stack.pop
  current
end
Also aliased as: o
r()
Alias for: reset
reset() click to toggle source
# File lib/json_inspector/context.rb, line 75
def reset
  @stack.clear!
  nil
end
Also aliased as: r
s(selector)
Alias for: show
show(selector) click to toggle source
# File lib/json_inspector/context.rb, line 62
def show(selector)
  @stack.current(selector)
end
Also aliased as: s
t(*args)
Alias for: tree
tree(*args) click to toggle source
# File lib/json_inspector/context.rb, line 37
def tree(*args)
  raise ::ArgumentError.new("wrong number of arguments (given #{args.size}, expected 2)") if args.size > 2

  initial_key = args.first.is_a?(::String) ? args.shift : ''
  level       = args.last.nil? ? ::Float::INFINITY : args.last.to_i

  enumerator(initial_key, level).map { |key, value| key unless value.is_a?(::Enumerable) }.compact
end
Also aliased as: t

Private Instance Methods

enumerator(initial_key, level) click to toggle source
# File lib/json_inspector/context.rb, line 90
def enumerator(initial_key, level)
  ::Enumerator.new do |yielder|
    recursive(initial_key, level, 0, yielder, @stack.current(initial_key))
  end
end
get_keys(value) click to toggle source
# File lib/json_inspector/context.rb, line 113
def get_keys(value)
  case
  when value.is_a?(::Array)
    (0...value.size).to_a
  when value.respond_to?(:keys)
    value.keys
  else
    []
  end
end
get_omitted_key_and_value(initial_key, value) click to toggle source
# File lib/json_inspector/context.rb, line 124
def get_omitted_key_and_value(initial_key, value)
  return ["#{initial_key}...", OMITTED_VALUE] if value.is_a?(::Enumerable)
  [initial_key, value]
end
recursive(initial_key, level, current_level, yielder, value) click to toggle source
# File lib/json_inspector/context.rb, line 96
def recursive(initial_key, level, current_level, yielder, value)
  return unless value.is_a?(::Enumerable)

  get_keys(value).each do |key|
    new_value         = value[key]
    new_initial_key   = [initial_key, key.to_s].reject(&:empty?).join('.')
    new_current_level = current_level + 1

    if new_current_level < level
      yielder.yield(new_initial_key, new_value)
      recursive(new_initial_key, level, new_current_level, yielder, new_value)
    else
      yielder.yield(*get_omitted_key_and_value(new_initial_key, new_value))
    end
  end
end