Class: Origen::Utility::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/utility/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Collector

Returns a new instance of Collector


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/origen/utility/collector.rb', line 13

def initialize(options = {}, &block)
  @merge_method = options[:merge_method] || :keep_hash
  @fail_on_empty_args = options[:fail_on_empty_args]
  unless [:keep_hash, :keep_block, :fail].include?(@merge_method)
    fail Origen::OrigenError, "Origen::Utility::Collector cannot merge with method :#{@merge_method} (of class #{@merge_method.class}). Known merge methods are :keep_hash (default), :keep_block, or :fail"
  end

  @_hash_ = options.key?(:hash) ? options[:hash].clone : {}
  @_methods_ = []

  if block_given?
    yield self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &_block) ⇒ Object


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
# File 'lib/origen/utility/collector.rb', line 40

def method_missing(method, *args, &_block)
  key = method.to_s.sub('=', '').to_sym

  # Check that the arguments are correct
  if block_given? && !args.empty?
    # raise Origen::OrigenError, "Origen::Utility::Collector detected both the hash and block attempting to set :#{key} (merge_method set to :fail)"
    fail ArgumentError, "Origen::Utility::Collector cannot accept both an argument list and block simultaneously for :#{key}. Please use one or the other."
  elsif block_given?
    val = _block
  elsif args.size == 0
    # Set any empty argument to nil
    val = nil
  elsif args.size > 1
    fail ArgumentError, "Origen::Utility::Collector does not allow method :#{key} more than 1 argument. Received 3 arguments."
  else
    val = args.first
  end

  # Check if we've already added this key via a method
  if _methods_.include?(key)
    fail Origen::OrigenError, "Origen::Utility::Collector does not allow method :#{key} to be set more than a single time. :#{key} is set to #{_hash_[key]}, tried to set it again to #{val}"
  end

  # indicate that we've seen this method, and decide whether or not to add the new value
  _methods_ << key

  # Merge the value (or don't, depending on what is set)
  if merge_method == :keep_block || !_hash_.key?(key)
    _hash_[key] = val
  elsif merge_method == :fail
    fail Origen::OrigenError, "Origen::Utility::Collector detected both the hash and block attempting to set :#{key} (merge_method set to :fail)"
  end
  # store[key] = val if !store.key?(key) || (store.key?(key) && merge_method == :keep_block)

  # Return self instead of the key value to allow for one-line collector statements
  self
end

Instance Attribute Details

#_hash_Object (readonly)

Returns the value of attribute hash


4
5
6
# File 'lib/origen/utility/collector.rb', line 4

def _hash_
  @_hash_
end

#_methods_Object (readonly)

Need to keep a seperate methods list so we know what's been added by method missing instead of what's been added either by the hash or by method missing. Only overwriting a value in the block should cause an error. Overriding a value from the hash depends on the merge method's setting.


11
12
13
# File 'lib/origen/utility/collector.rb', line 11

def _methods_
  @_methods_
end

#merge_methodObject (readonly)

Returns the value of attribute merge_method


5
6
7
# File 'lib/origen/utility/collector.rb', line 5

def merge_method
  @merge_method
end

Instance Method Details

#storeObject

Legacy store method.


29
30
31
32
33
# File 'lib/origen/utility/collector.rb', line 29

def store
  Origen.log.deprecate 'Collector::store method was used. Please use the Ruby-centric Collector::to_h or Collector::to_hash method instead' \
                       " Called from: #{caller[0]}"
  @_hash_
end

#to_hashObject Also known as: to_h


35
36
37
# File 'lib/origen/utility/collector.rb', line 35

def to_hash
  @_hash_
end