Skip to content

Commit

Permalink
Fix the edge cases
Browse files Browse the repository at this point in the history
* Ident locales file
* Fix edge cases
* Expand test cases
* Use constant for separator
  • Loading branch information
sl4vr committed Aug 25, 2021
1 parent 7225179 commit 9eb743d
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 224 deletions.
2 changes: 1 addition & 1 deletion bin/yamlook
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require 'yamlook'

cli_parser = Yamlook::Cli.new(ARGV)

Yamlook::Search.perform(cli_parser.argument.split('.'))
Yamlook::Search.perform(cli_parser.argument.split(Yamlook::SEPARATOR))
2 changes: 2 additions & 0 deletions lib/yamlook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

require 'psych'

require 'yamlook/iteration'
require 'yamlook/handler'
require 'yamlook/file'
require 'yamlook/search'
require 'yamlook/cli'

module Yamlook
SEPARATOR = '.'
LOCALES_FILEPATH = ::File.join(::File.expand_path('..', __dir__), 'locales.yaml')
LOCALES = Psych.load_file(LOCALES_FILEPATH)['locales'].freeze
end
78 changes: 39 additions & 39 deletions lib/yamlook/handler.rb
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
18 changes: 18 additions & 0 deletions lib/yamlook/iteration.rb
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
2 changes: 1 addition & 1 deletion lib/yamlook/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Search
def perform(keys)
raise NoArgumentsError, "Nothing to search for.\n" if keys.empty?

findings = Dir.glob(PATTERN).map do |filename|
findings = Dir.glob(PATTERN).flat_map do |filename|
result = File.new(filename).search(keys)
print_progress(result)
result
Expand Down
2 changes: 1 addition & 1 deletion lib/yamlook/version.rb
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
Loading

0 comments on commit 9eb743d

Please sign in to comment.