-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ident locales file * Fix edge cases * Expand test cases * Use constant for separator
- Loading branch information
Showing
9 changed files
with
281 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'psych' | ||
|
||
module Yamlook | ||
# Handler for Psych::Parser | ||
class Handler < ::Psych::Handler | ||
attr_reader :found | ||
|
||
def initialize(keys:, locales: []) | ||
super() | ||
@keys = keys | ||
@locales = locales | ||
@found = [] | ||
@level = -1 | ||
@active = @prev_active = @locale_active = false | ||
@start_line = @start_column = nil | ||
@offset = 0 | ||
|
||
@iterations = [] | ||
@current_iteration = Iteration.new(active: true) | ||
end | ||
|
||
def event_location(start_line, start_column, _end_line, _end_column) | ||
@start_line = start_line | ||
@start_column = start_column | ||
end | ||
|
||
def start_mapping(_anchor, _tag, _implicit, _style) | ||
@level += 1 | ||
@prev_active = @active || top_level? | ||
@active = false | ||
@iterations.push(@current_iteration.dup) | ||
@current_iteration.reset! | ||
end | ||
|
||
def end_mapping | ||
@level -= 1 | ||
@level -= @offset | ||
@offset = 0 | ||
@locale_active = false if top_level? | ||
@iterations.pop | ||
@current_iteration.reset! | ||
end | ||
|
||
def scalar(value, _anchor, _tag, _plain, _quoted, _style) | ||
if current_level == @keys.size.pred && @active && (current_level.zero? || @prev_active) | ||
@found << [value, @start_line.next, @start_column.next] | ||
end | ||
|
||
@active, @offset = match(value) | ||
@level += @offset | ||
@locale_active ||= top_level? && @locales.include?(value) | ||
|
||
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 | ||
|
||
def top_level? | ||
@level.zero? | ||
|
||
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 | ||
def current_level | ||
@locale_active ? @level.pred : @level | ||
|
||
def current_offset | ||
@iterations.sum(&:offset) | ||
end | ||
|
||
def match(value) | ||
return [false, 0] unless @keys[current_level] | ||
def current_keys | ||
@keys.drop(current_offset).take(@current_iteration.offset) | ||
end | ||
|
||
@keys[current_level..-1].each_with_index do |key, index| | ||
if value == @keys[current_level..index+current_level].join('.') | ||
return [true, index] | ||
end | ||
end | ||
def keys_out? | ||
current_offset + @current_iteration.offset == @keys.size | ||
end | ||
|
||
[false, 0] | ||
def all_active? | ||
@iterations.any? && @iterations.all?(&:active) && @current_iteration.active | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yamlook | ||
# Holds information for iteration over Psych::Handler iterating through mappings | ||
class Iteration | ||
attr_accessor :active, :offset | ||
|
||
def initialize(active: false, offset: 0) | ||
@active = active | ||
@offset = offset | ||
end | ||
|
||
def reset! | ||
@active = false | ||
@offset = 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yamlook | ||
VERSION = '0.4.0' | ||
VERSION = '0.4.1' | ||
end |
Oops, something went wrong.