Module: StatVal

Defined in:
lib/statval/statval.rb,
lib/statval/version.rb

Defined Under Namespace

Classes: StatVal

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.all_keysObject



173
# File 'lib/statval/statval.rb', line 173

def self.all_keys ; [ :avg, :std, :std_ratio, :min, :max, :num, :sum, :sq_sum, :avg_sq, :var ] end

.default_keysObject



174
# File 'lib/statval/statval.rb', line 174

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/statval/statval.rb', line 211

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



189
190
191
192
# File 'lib/statval/statval.rb', line 189

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



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/statval/statval.rb', line 177

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



199
200
201
202
203
# File 'lib/statval/statval.rb', line 199

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



171
# File 'lib/statval/statval.rb', line 171

def self.new(options = {}) ; StatVal.new(options) end

.writable_keysObject



175
# File 'lib/statval/statval.rb', line 175

def self.writable_keys ; [ :num, :min, :max, :sum, :sq_sum ] end