Class: Humanized::Source
Overview
A source lets you lookup,store and load data needed for humanization.
Constant Summary collapse
- VALUE_KEY =
:_- NOT_NIL_LAMBDA =
lambda{|x| !x.nil? }
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Stores the given data on the base.
-
#get(query, options = {}) ⇒ String, ...
Retrieves data.
-
#initialize(data = {}) ⇒ Source
constructor
A new instance of Source.
- #inspect ⇒ Object
-
#load(path, opts = {}) ⇒ Object
Loads a data-file or a dir of data-files.
-
#package(name) {|self| ... } ⇒ Object
This is method which will help you loading data once.
-
#store(path, data) ⇒ Object
Stores data at the path.
Constructor Details
#initialize(data = {}) ⇒ Source
Returns a new instance of Source.
26 27 28 29 30 31 |
# File 'lib/humanized/source.rb', line 26 def initialize(data = {}) #TODO: data should maybe deep-copied @source = data @sync = Sync.new @loaded = Set.new end |
Instance Method Details
#<<(data) ⇒ Object
Stores the given data on the base.
71 72 73 |
# File 'lib/humanized/source.rb', line 71 def <<(data) store([],data) end |
#get(query, options = {}) ⇒ String, ...
Retrieves data
100 101 102 103 104 105 106 107 108 |
# File 'lib/humanized/source.rb', line 100 def get(query, = {}) default = [:default] accepts = .fetch(:accepts,NOT_NIL_LAMBDA ) query.each do |path| v = @source[path] return v if accepts.call(v) end return default end |
#inspect ⇒ Object
134 135 136 |
# File 'lib/humanized/source.rb', line 134 def inspect "#<#{self.class.name}:#{self.object_id.to_s} | #{@source.size} key(s); loaded: #{@loaded.inspect}>" end |
#load(path, opts = {}) ⇒ Object
Loads a data-file or a dir of data-files.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/humanized/source.rb', line 40 def load(path,opts ={}) = {:query => Query::Root, :grep => '**/*.*'}.update(opts) if File.directory?(path) f = File.join(path,[:grep]) package('grep:' + f) do Dir[f].each do |file| package('file:'+file) do data = self.read_file(file) if data xpath = file[path.size..(-1-File.extname(file).size)].split('/') xpath.shift if xpath.first == '' xquery = [:query]._(*xpath.map(&:to_sym)) self.store(xquery.first,data) end end end end else package('file:'+path) do data = self.read_file(path) if data self.store([:query].first,data) end end end return self end |
#package(name) {|self| ... } ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/humanized/source.rb', line 88 def package(name) return nil if @loaded.include? name @sync.synchronize(Sync::EX){ return nil if @loaded.include? name yield(self) @loaded << name } end |
#store(path, data) ⇒ Object
Stores data at the path
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/humanized/source.rb', line 113 def store(path ,data) @sync.synchronize(Sync::EX){ if data.kind_of? Hash data.each do |key, value| if key.kind_of? Array store( path + key, value) next end key = key.to_sym if key == VALUE_KEY store( path, value ) else store( path + [key], value) end end else @source[path] = data end } end |