module Mspire::Mass::MassProvider
Constants
- DEFAULTS
Public Instance Methods
masses(opts={})
click to toggle source
options:
<default> type: :mono | :avg case: :up | :down | :both by: :symbol | :string | :both
By default will use the module’s <type>_STRING constant but this can be overridden with the :hash option which accetps a hash with symbol or string keys in uppercase or mixed case form (the :up merely respects the given case, while :down actively downcases. Accepts a hash with symbol or string keys. The keys should be in uppercase or mixed case to begin with.
# File lib/mspire/mass/mass_provider.rb, line 53 def masses(opts={}) opt = DEFAULTS.merge(opts) prepare_hash(opt[:hash] || self.const_get(opt[:type].to_s.upcase << "_STRING"), opt) end
prepare_hash(hash, opts={})
click to toggle source
# File lib/mspire/mass/mass_provider.rb, line 10 def prepare_hash(hash, opts={}) opt = Mspire::Mass::MassProvider::DEFAULTS.merge(opts) newhash = {} (upcase, downcase, symbol, string) = ['case'.to_sym, :up, 'case'.to_sym, :down, :by, :symbol, :by, :string].each_slice(2).map do |opt_key, unique| [:both, unique].include?(opt[opt_key]) end hash.each do |k,v| keys = [] if upcase keys << k.to_s # <= assumes they start in proper up or mixed case end if downcase keys << k.to_s.downcase end if symbol final_keys = keys.map(&:to_sym) end if string string_keys = keys.map(&:to_s) final_keys ||= [] final_keys.push(*string_keys) end final_keys.each {|key| newhash[key] = v } end newhash end