Module: Mspire::Mass::MassProvider
Constant Summary collapse
- DEFAULTS =
{ type: :mono, by: :symbol, case: :up, }
Instance Method Summary collapse
Instance Method Details
#masses(opts = {}) ⇒ Object
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.
53 54 55 56 |
# 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 = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |