Module: Enumerable
- Included in:
- Origen::Pins::PinCollection, Origen::Utility::BlockArgs
- Defined in:
- lib/origen/core_ext/enumerable.rb
Constant Summary collapse
Instance Method Summary collapse
- #debug(msg) ⇒ Object
-
#list(options = {}) ⇒ Object
Returns a list of primitives and/or complex objects found within an enumerable object It ignores empty or nil values by default but is configurable The method is recusive and will run until all enumerable objects have been examined.
Instance Method Details
#debug(msg) ⇒ Object
4 5 6 |
# File 'lib/origen/core_ext/enumerable.rb', line 4 def debug(msg) Origen.log.debug(msg) end |
#list(options = {}) ⇒ Object
Returns a list of primitives and/or complex objects found within an enumerable object It ignores empty or nil values by default but is configurable The method is recusive and will run until all enumerable objects have been examined
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/origen/core_ext/enumerable.rb', line 11 def list( = {}) = { nil_or_empty: false, flatten: [], # Can be a single class or an array of classes to 'flatten' when enumerating (treated as a single value) select: [], # Select certain complex data types for enumeration ignore: [], # Ignore certain complex data types for enumeration to_s: false # If set to true this will convert a complex object called with 'flatten' option to a String }.update() list_array ||= [] [:ignore, :select, :flatten].each do |opt| [opt] = [[opt]] unless [opt].is_a? Array end unless [:flatten].empty? || [:select].empty? fail "Cannot have the same arguments for 'flatten' and 'select' options" unless ([:flatten] & [:select]).empty? end unless [:flatten].empty? || [:ignore].empty? fail "Cannot have the same arguments for 'flatten' and 'ignore' options" unless ([:flatten] & [:ignore]).empty? end unless [:ignore].empty? || [:select].empty? fail "Cannot have the same arguments for 'ignore' and 'select' options" unless ([:ignore] & [:select]).empty? end if respond_to?(:empty?) && empty? list_array << self if [:nil_or_empty] else each do |k, v| is_a?(Hash) ? item = v : item = k klass = item.class.to_s superklass = item.class.superclass.to_s if [:flatten].include? item.class if item.respond_to?(:empty?) next if item.empty? && [:nil_or_empty] == false end debug "Adding current enumerable #{klass} to list as a flat object, will not enumerate through it..." if [:to_s] == true list_array << "#{superklass}::#{klass}" else list_array << item end next else next unless [:select].empty? || [:select].include?(item.class) || PRIMATIVES.any? { |klass| item.is_a?(klass) } next if [:ignore].include?(item.class) case item when NilClass list_array << item if [:nil_or_empty] when Hash, Array, Range # debugger if item == [] && $bac == 1 if item.empty? list_array << [] if [:nil_or_empty] else list_array += item.list() end when Struct list_array += item.list() when String next if item.empty? && [:nil_or_empty] == false list_array << item else list_array << item end end end end list_array end |