Module: StatVal
- Defined in:
- lib/statval/statval.rb,
lib/statval/version.rb
Defined Under Namespace
Classes: StatVal
Constant Summary collapse
- VERSION =
'0.1.3'
Class Method Summary collapse
- .all_keys ⇒ Object
- .default_keys ⇒ Object
-
.flatmap_hash(h, which_keys = nil, prefix = true, use_symbols = false) ⇒ Object
Like map_hash, but flattens converted StatVal values such there attributes get pre- or appended with their key in the outer hash.
- .key_hash(which_keys = nil) ⇒ Object
- .keys(ident = :default) ⇒ Object
-
.map_hash(h, which_keys = nil) ⇒ Object
Take hash that contains StatVal values and create new hash that is identical to it but has the StatVal values v replaced by v.to_hash(which_keys).
- .new(options = {}) ⇒ Object
- .writable_keys ⇒ Object
Class Method Details
.all_keys ⇒ Object
174 |
# File 'lib/statval/statval.rb', line 174 def self.all_keys ; [ :avg, :std, :std_ratio, :min, :max, :num, :sum, :sq_sum, :avg_sq, :var ] end |
.default_keys ⇒ Object
175 |
# File 'lib/statval/statval.rb', line 175 def self.default_keys ; [ :avg, :std, :min, :max, :num ] end |
.flatmap_hash(h, which_keys = nil, prefix = true, use_symbols = false) ⇒ Object
Like map_hash, but flattens converted StatVal values such there attributes get pre- or appended with their key in the outer hash
All symbols raises on key conflict
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/statval/statval.rb', line 212 def self.flatmap_hash(h, which_keys = nil, prefix=true, use_symbols=false) return h.to_hash(which_keys, ! use_symbols) if h.kind_of?(StatVal) flat = {} h.each_pair do |k,r| if r.kind_of? StatVal results = r.to_hash(which_keys) results.each_pair do |tag,val| new_tag = if prefix then "#{tag}_#{k}" else "#{k}_#{tag}" end new_tag = new_tag.to_sym if use_symbols raise ArgumentError if flat[new_tag] flat[new_tag] = val end else raise ArgumentError if flat[k] if k.is_a?(Symbol) && !use_symbols flat[k.to_s] = r else flat[k] = r end end end flat end |
.key_hash(which_keys = nil) ⇒ Object
190 191 192 193 |
# File 'lib/statval/statval.rb', line 190 def self.key_hash(which_keys = nil) return which_keys if which_keys.is_a?(Hash) keys(which_keys).inject({}) { |h, k| h[k] = k; h } end |
.keys(ident = :default) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/statval/statval.rb', line 178 def self.keys(ident = :default) case ident when :all then all_keys when :writable then writable_keys when :default then default_keys when nil then default_keys else return ident if ident.respond_to?(:each) return [ident] end end |
.map_hash(h, which_keys = nil) ⇒ Object
Take hash that contains StatVal values and create new hash that is identical to it but has the StatVal values v replaced by v.to_hash(which_keys)
Just copies non-StatVal entries
200 201 202 203 204 |
# File 'lib/statval/statval.rb', line 200 def self.map_hash(h, which_keys = nil) r = {} h.each_pair { |k,v| r[k] = if v.kind_of?(StatVal) then v.to_hash(which_keys) else v end } r end |
.new(options = {}) ⇒ Object
172 |
# File 'lib/statval/statval.rb', line 172 def self.new( = {}) ; StatVal.new() end |
.writable_keys ⇒ Object
176 |
# File 'lib/statval/statval.rb', line 176 def self.writable_keys ; [ :num, :min, :max, :sum, :sq_sum ] end |