Class: Yamlook::Handler

Inherits:
Psych::Handler
  • Object
show all
Defined in:
lib/yamlook/handler.rb

Overview

Handler for Psych::Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys:, locales: []) ⇒ Handler

Returns a new instance of Handler.



10
11
12
13
14
15
16
17
18
# File 'lib/yamlook/handler.rb', line 10

def initialize(keys:, locales: [])
  super()
  @keys = keys
  @locales = locales
  @found = []

  @iterations = []
  @current_iteration = Iteration.new(active: true)
end

Instance Attribute Details

#foundObject (readonly)

Returns the value of attribute found.



8
9
10
# File 'lib/yamlook/handler.rb', line 8

def found
  @found
end

Instance Method Details

#all_active?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/yamlook/handler.rb', line 62

def all_active?
  @iterations.any? && @iterations.all?(&:active) && @current_iteration.active
end

#current_keysObject



54
55
56
# File 'lib/yamlook/handler.rb', line 54

def current_keys
  @keys.drop(current_offset).take(@current_iteration.offset)
end

#current_offsetObject



50
51
52
# File 'lib/yamlook/handler.rb', line 50

def current_offset
  @iterations.sum(&:offset)
end

#end_mappingObject



30
31
32
33
# File 'lib/yamlook/handler.rb', line 30

def end_mapping
  @iterations.pop
  @current_iteration.reset!
end

#event_location(start_line, start_column, _end_line, _end_column) ⇒ Object



20
21
22
23
# File 'lib/yamlook/handler.rb', line 20

def event_location(start_line, start_column, _end_line, _end_column)
  @start_line = start_line
  @start_column = start_column
end

#keys_out?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/yamlook/handler.rb', line 58

def keys_out?
  current_offset + @current_iteration.offset == @keys.size
end

#refresh_current_interation!(value) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/yamlook/handler.rb', line 41

def refresh_current_interation!(value)
  value_keys = value.split(SEPARATOR)

  value_keys.shift if current_offset.zero? && LOCALES.include?(value_keys.first)

  @current_iteration.offset = value_keys.count
  @current_iteration.active = current_keys == value_keys
end

#scalar(value, _anchor, _tag, _plain, _quoted, _style) ⇒ Object

rubocop:disable Metrics/ParameterLists



35
36
37
38
39
# File 'lib/yamlook/handler.rb', line 35

def scalar(value, _anchor, _tag, _plain, _quoted, _style) # rubocop:disable Metrics/ParameterLists
  @found << [value, @start_line.next, @start_column.next] if keys_out? && all_active?

  refresh_current_interation!(value)
end

#start_mapping(_anchor, _tag, _implicit, _style) ⇒ Object



25
26
27
28
# File 'lib/yamlook/handler.rb', line 25

def start_mapping(_anchor, _tag, _implicit, _style)
  @iterations.push(@current_iteration.dup)
  @current_iteration.reset!
end