Sha256: 25700dcbf2882f40170e2377af0184d505975dd111b8860276a2e189a34d291a
Contents?: true
Size: 1020 Bytes
Versions: 2
Compression:
Stored size: 1020 Bytes
Contents
module SearchyJson class Data attr_reader :data_path, :primary_key, :search_fields, :scope def initialize(data_path, primary_key:, search_fields: []) @data_path = data_path @primary_key = primary_key @search_fields = search_fields end def call @scope = parsed_data @scope = collected_by_primary_key @scope = sort_by_relevant scope end private def collected_by_primary_key scope.inject({}) do |new_scope, data_item| key = data_item[primary_key] values = formatted_data(data_item) new_scope.merge(key => values) end end def sort_by_relevant scope.sort_by { |_primary_field, characters| characters.size }.to_h end def parsed_data @parsed_data ||= JSON.parse(File.read(data_path)) end def formatted_data(data_item) values = data_item.slice(*search_fields).values.flatten values.map { |item| item.delete(',').upcase.split(' ') }.flatten.uniq end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
searchy-json-0.3.1 | lib/searchy/data.rb |
searchy-json-0.3.0 | lib/searchy/data.rb |