Class: Zn::Repos::JsonRepo
- Inherits:
-
Object
- Object
- Zn::Repos::JsonRepo
- Defined in:
- lib/zn/repos/json_repo.rb
Overview
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.
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(params) ⇒ JsonRepo
constructor
A new instance of JsonRepo.
- #search(params) ⇒ Object
Constructor Details
#initialize(params) ⇒ JsonRepo
Returns a new instance of JsonRepo.
12 13 14 15 16 17 18 |
# 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 |
Instance Method Details
#all ⇒ Object
20 21 22 |
# File 'lib/zn/repos/json_repo.rb', line 20 def all @all ||= MultiJson.load(@io) end |
#search(params) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 search_exact(key, value) { |result| yield result } else search_for_inclusion(key, value) { |result| yield result } end end |