Class: JsonInspector::Context

Inherits:
BasicObject
Defined in:
lib/json_inspector/context.rb

Constant Summary collapse

OMITTED_VALUE =
'< omitted >'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/json_inspector/context.rb', line 5

def doc
  @doc
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/json_inspector/context.rb', line 5

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/json_inspector/context.rb', line 5

def options
  @options
end

#stackObject (readonly)

Returns the value of attribute stack.



5
6
7
# File 'lib/json_inspector/context.rb', line 5

def stack
  @stack
end

Instance Method Details

#_prompt_Object



18
19
20
21
22
23
# 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

#currentObject Also known as: c



82
83
84
# File 'lib/json_inspector/context.rb', line 82

def current
  @stack.current
end

#find(query) ⇒ Object Also known as: f



31
32
33
# File 'lib/json_inspector/context.rb', line 31

def find(query)
  enumerator('', ::Float::INFINITY).select { |key, value| value == query }.map { |key, _| key }
end

#find_key(query) ⇒ Object Also known as: fk



25
26
27
# 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

#into(selector) ⇒ Object Also known as: i



55
56
57
58
# File 'lib/json_inspector/context.rb', line 55

def into(selector)
  @stack.push(selector)
  current
end

#keys(selector = '') ⇒ Object Also known as: k



48
49
50
51
# File 'lib/json_inspector/context.rb', line 48

def keys(selector = '')
  value = @stack.current(selector)
  get_keys(value)
end

#outObject Also known as: o



68
69
70
71
# File 'lib/json_inspector/context.rb', line 68

def out
  @stack.pop
  current
end

#resetObject Also known as: r



75
76
77
78
# File 'lib/json_inspector/context.rb', line 75

def reset
  @stack.clear!
  nil
end

#show(selector) ⇒ Object Also known as: s



62
63
64
# File 'lib/json_inspector/context.rb', line 62

def show(selector)
  @stack.current(selector)
end

#tree(*args) ⇒ Object Also known as: t

Raises:

  • (::ArgumentError)


37
38
39
40
41
42
43
44
# 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