class Zn::Repos::JsonRepo
JsonRepo
abstracts loading of external json data. For more demanding datasets, repos of other nature can be created later as needed. Examples of this include loading data asynchronously from json files, loading data from a database, elasticsearch, etc.
Public Class Methods
new(params)
click to toggle source
# File lib/zn/repos/json_repo.rb, line 12 def initialize(params) dir = params.fetch('dir', '.') Dir.chdir(dir) do @io = File.new(params.fetch('file')) end end
Public Instance Methods
all()
click to toggle source
# File lib/zn/repos/json_repo.rb, line 20 def all @all ||= MultiJson.load(@io) end
search(params) { |result| ... }
click to toggle source
# File lib/zn/repos/json_repo.rb, line 24 def search(params) return enum_for(:search, params) unless block_given? key = params.fetch(:key) value = params.fetch(:value) exact = params.fetch(:exact) if exact || value.empty? search_exact(key, value) { |result| yield result } else search_for_inclusion(key, value) { |result| yield result } end end
Private Instance Methods
search_exact(key, value) { |element| ... }
click to toggle source
# File lib/zn/repos/json_repo.rb, line 40 def search_exact(key, value) return enum_for(:search_exact, key, value) unless block_given? all.each do |element| yield element if element.fetch(key, '').to_s == value.to_s end end
search_for_inclusion(key, value) { |element| ... }
click to toggle source
# File lib/zn/repos/json_repo.rb, line 48 def search_for_inclusion(key, value) return enum_for(:search_for_inclusion, key, value) unless block_given? all.each do |element| yield element if element.fetch(key, '').to_s.include?(value.to_s) end end