Module: ImageFilterDsl::Serializers::Common
- Extended by:
- Common
- Included in:
- Common, Intermediate, Json, MsgPack, Yaml
- Defined in:
- lib/image_filter_dsl/serializers/common.rb
Overview
Common helper methods for serializers; Include in serializer modules with ‘include Common`
Instance Method Summary collapse
-
#fix_hash(hash, keys = []) ⇒ Hash
‘Fix’ hash, converting keys to symbols (if keys provided) and converting values/arrays of values so any strings are normalized and cast to symbols using #normalize_sym.
-
#normalize_sym(val) ⇒ Symbol|Number|Array
Normalize symbol/value, stripping out any colons and converting to symbol; Non strings are returned unaltered; if array, logic is applied to all items.
Instance Method Details
#fix_hash(hash, keys = []) ⇒ Hash
‘Fix’ hash, converting keys to symbols (if keys provided) and converting values/arrays of values so any strings are normalized and cast to symbols using #normalize_sym
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/image_filter_dsl/serializers/common.rb', line 35 def fix_hash(hash,keys=[]) h2=hash.dup unless keys.length == 0 keys.each do |k| if !h2.has_key?(k) && h2.has_key?(k.to_s) h2[k] = h2[k.to_s] h2.delete k.to_s end end end h2.keys.each do |k| if h2[k].is_a?(Array) h2[k] = h2[k].map{|v|normalize_sym(v)} else h2[k] = normalize_sym(h2[k]) end end return h2 end |
#normalize_sym(val) ⇒ Symbol|Number|Array
Normalize symbol/value, stripping out any colons and converting to symbol; Non strings are returned unaltered; if array, logic is applied to all items
18 19 20 21 22 23 24 25 26 |
# File 'lib/image_filter_dsl/serializers/common.rb', line 18 def normalize_sym(val) if val.is_a?(String) val.gsub(':','').to_sym elsif val.is_a?(Array) val.map{|v|normalize_sym(v)} else val end end |