Module: Flex::Struct::Prunable
Defined Under Namespace
Classes: Value
Constant Summary collapse
- VALUES =
[ nil, '', {}, [], false ]
Instance Method Summary collapse
-
#prune(obj, *values) ⇒ Object
prunes the branch when the leaf is Prunable and compact.flatten the Array values values are the prunable values, like VALUES or Prunable::Value, or any arbitrary value.
- #prune_blanks(obj) ⇒ Object
Instance Method Details
#prune(obj, *values) ⇒ Object
prunes the branch when the leaf is Prunable and compact.flatten the Array values values are the prunable values, like VALUES or Prunable::Value, or any arbitrary value
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/flex/struct/prunable.rb', line 24 def prune(obj, *values) case when values.include?(obj) obj when obj.is_a?(::Array) return obj if obj.empty? ar = [] obj.each do |i| pruned = prune(i, *values) next if values.include?(pruned) ar << pruned end a = ar.compact.flatten a.empty? ? values.first : a when obj.is_a?(::Hash) return obj if obj.empty? h = {} obj.each do |k, v| pruned = prune(v, *values) next if values.include?(pruned) # when a key is prunable merges the value if it is a hash (allows merging of partials) if VALUES.include?(k) h.merge!(pruned) if pruned.is_a?(::Hash) else h[k] = pruned end end h.empty? ? values.first : h else obj end end |
#prune_blanks(obj) ⇒ Object
16 17 18 |
# File 'lib/flex/struct/prunable.rb', line 16 def prune_blanks(obj) prune(obj, *VALUES) || {} end |