Module: Enumerable

Included in:
Origen::Pins::PinCollection, Origen::Utility::BlockArgs
Defined in:
lib/origen/core_ext/enumerable.rb

Constant Summary collapse

PRIMATIVES =
[TrueClass, FalseClass, NilClass, String, Symbol, Regexp, Numeric]

Instance Method Summary collapse

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(options = {})
  options = {
    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(options)
  list_array ||= []
  [:ignore, :select, :flatten].each do |opt|
    options[opt] = [options[opt]] unless options[opt].is_a? Array
  end
  unless options[:flatten].empty? || options[:select].empty?
    fail "Cannot have the same arguments for 'flatten' and 'select' options" unless (options[:flatten] & options[:select]).empty?
  end
  unless options[:flatten].empty? || options[:ignore].empty?
    fail "Cannot have the same arguments for 'flatten' and 'ignore' options" unless (options[:flatten] & options[:ignore]).empty?
  end
  unless options[:ignore].empty? || options[:select].empty?
    fail "Cannot have the same arguments for 'ignore' and 'select' options" unless (options[:ignore] & options[:select]).empty?
  end
  if respond_to?(:empty?) && empty?
    list_array << self if options[: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 options[:flatten].include? item.class
        if item.respond_to?(:empty?)
          next if item.empty? && options[:nil_or_empty] == false
        end
        debug "Adding current enumerable #{klass} to list as a flat object, will not enumerate through it..."
        if options[:to_s] == true
          list_array << "#{superklass}::#{klass}"
        else
          list_array << item
        end
        next
      else
        next unless options[:select].empty? || options[:select].include?(item.class) || PRIMATIVES.any? { |klass| item.is_a?(klass) }
        next if options[:ignore].include?(item.class)

        case item
        when NilClass
          list_array << item if options[:nil_or_empty]
        when Hash, Array, Range
          # debugger if item == [] && $bac == 1
          if item.empty?
            list_array << [] if options[:nil_or_empty]
          else
            list_array += item.list(options)
          end
        when Struct
          list_array += item.list(options)
        when String
          next if item.empty? && options[:nil_or_empty] == false

          list_array << item
        else
          list_array << item
        end
      end
    end
  end
  list_array
end