Class: Maguire::DataSet
- Inherits:
-
Object
- Object
- Maguire::DataSet
- Includes:
- Enumerable
- Defined in:
- lib/maguire/data_set.rb
Defined Under Namespace
Classes: NoDataFound
Instance Method Summary collapse
- #<<(path) ⇒ Object
- #clear ⇒ Object
- #clear_cache! ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ DataSet
constructor
A new instance of DataSet.
- #load(id) ⇒ Object
- #merge_data(data_sets) ⇒ Object
Constructor Details
#initialize ⇒ DataSet
Returns a new instance of DataSet.
9 10 11 12 |
# File 'lib/maguire/data_set.rb', line 9 def initialize() @cache = {} @paths = [] end |
Instance Method Details
#<<(path) ⇒ Object
18 19 20 21 |
# File 'lib/maguire/data_set.rb', line 18 def <<(path) clear_cache! @paths << path end |
#clear ⇒ Object
27 28 29 30 |
# File 'lib/maguire/data_set.rb', line 27 def clear clear_cache! @paths = [] end |
#clear_cache! ⇒ Object
23 24 25 |
# File 'lib/maguire/data_set.rb', line 23 def clear_cache! @cache = {} end |
#each(&block) ⇒ Object
14 15 16 |
# File 'lib/maguire/data_set.rb', line 14 def each(&block) @paths.each(&block) end |
#load(id) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/maguire/data_set.rb', line 32 def load(id) return @cache[id.to_sym] if @cache && @cache[id.to_sym] data_sets = map do |data_path| path = data_path.join("#{id}.json") if File.exists?(path) JSON.parse(File.read(path), symbolize_names: true) else {} end end merged_data = merge_data(data_sets) if merged_data.empty? raise NoDataFound.new(@paths.join(':')) end @cache[id.to_sym] = merged_data end |