class Gumdrop::DataManager
Attributes
cache[R]
Public Class Methods
new(data_dir="./data")
click to toggle source
# File lib/gumdrop/data.rb, line 10 def initialize(data_dir="./data") @dir= File.expand_path data_dir # reset @cache= Hash.new &method(:_cache_dataset) end
Public Instance Methods
clear()
click to toggle source
# File lib/gumdrop/data.rb, line 32 def clear @cache.clear end
contents(pattern=nil, opts={})
click to toggle source
# File lib/gumdrop/data.rb, line 36 def contents(pattern=nil, opts={}) if pattern.nil? site.contents.all else site.contents(pattern, opts) end end
dir=(path)
click to toggle source
# File lib/gumdrop/data.rb, line 16 def dir=(path) @dir= File.expand_path path end
method_missing(key, value=nil)
click to toggle source
# File lib/gumdrop/data.rb, line 20 def method_missing(key, value=nil) @cache[key] end
pager_for(key, opts={})
click to toggle source
# File lib/gumdrop/data.rb, line 44 def pager_for(key, opts={}) data= case key when Symbol @cache[key] when Array key else raise "pager_for requires a lookup symbol or array data." end base_path= opts.fetch(:base_path, 'page') page_size= opts.fetch(:page_size, 5) Util::Pager.new( data, base_path, page_size ) end
parse_file(path, target_ext=nil)
click to toggle source
Not used internally, but useful for external usage
# File lib/gumdrop/data.rb, line 59 def parse_file(path, target_ext=nil) return nil if path.nil? return nil if File.directory? path _load_from_file path, target_ext # if File.directory? path # _load_from_directory path # else # _load_from_file path, target_ext # end end
reset()
click to toggle source
# File lib/gumdrop/data.rb, line 28 def reset @cache.clear end
set(key, value)
click to toggle source
# File lib/gumdrop/data.rb, line 24 def set(key, value) @cache[key]= value end
Private Instance Methods
_cache_dataset(hash, key)
click to toggle source
# File lib/gumdrop/data.rb, line 72 def _cache_dataset(hash, key) hash[key]= load_data(key) #unless @cache.has_key? key end
_get_filename(path)
click to toggle source
# File lib/gumdrop/data.rb, line 112 def _get_filename(path) lpath= _local_path_to(path) if File.directory? lpath lpath else _registered_data_types.each do |ext| lpath= _local_path_to("#{path}.#{ext}") return lpath if File.exists? lpath end log.warn "No data found for #{path}" nil end end
_load_from_directory( filepath )
click to toggle source
# File lib/gumdrop/data.rb, line 101 def _load_from_directory( filepath ) all=[] Dir[ filepath / _supported_type_glob ].each do |filename| id= File.basename(filename).gsub(File.extname(filename), '') obj_hash= _load_from_file filename obj_hash._id = id all << obj_hash end all end
_load_from_file( filename, target_ext=nil )
click to toggle source
# File lib/gumdrop/data.rb, line 86 def _load_from_file( filename, target_ext=nil ) ext= target_ext || File.extname(filename)[1..-1] provider= Data::Provider.for ext case when provider.nil? raise "Unknown data type (#{ext}) for #{filename}" when provider.available? data= provider.data_for filename log.debug " loaded: #{filename}" data else raise "Unavailable data type (#{ext}) for #{filename}" end end
_local_path_to(filename)
click to toggle source
# File lib/gumdrop/data.rb, line 126 def _local_path_to(filename) File.join(@dir.to_s, filename.to_s) end
_registered_data_types()
click to toggle source
# File lib/gumdrop/data.rb, line 130 def _registered_data_types Data::Provider.registered_exts end
_supported_type_glob()
click to toggle source
# File lib/gumdrop/data.rb, line 134 def _supported_type_glob "{*.#{ _registered_data_types.join ',*.'}}" end
load_data(key)
click to toggle source
# File lib/gumdrop/data.rb, line 76 def load_data(key) path=_get_filename key return nil if path.nil? if File.directory? path _load_from_directory path else _load_from_file path end end