Class: Origen::Utility::Collector
- Defined in:
- lib/origen/utility/collector.rb
Instance Attribute Summary collapse
-
#_hash_ ⇒ Object
readonly
Returns the collector’s hash.
-
#_methods_ ⇒ Object
readonly
List of the currently seen method names.
-
#merge_method ⇒ Object
readonly
Queries the current merge_method.
Instance Method Summary collapse
-
#initialize(options = {}) {|self| ... } ⇒ Collector
constructor
Creates a new Collector object and creates a Hash out of the methods names and values in the given block.
-
#method_missing(method, *args, &_block) ⇒ Object
Using the method name, creates a key in the Collector with argument given to the method.
-
#store ⇒ Hash
deprecated
Deprecated.
Use Ruby-centric #to_hash instead.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns the collector, as a Hash.
Constructor Details
#initialize(options = {}) {|self| ... } ⇒ Collector
Creates a new Collector object and creates a Hash out of the methods names and values in the given block.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/origen/utility/collector.rb', line 28 def initialize( = {}, &block) @merge_method = [:merge_method] || :keep_hash @fail_on_empty_args = [: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_ = .key?(:hash) ? [: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
If no args are given, the method key is set to nil
.
Using the method name, creates a key in the Collector with argument given to the method.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/origen/utility/collector.rb', line 69 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 collector’s hash. This is the same as calling #to_h
5 6 7 |
# File 'lib/origen/utility/collector.rb', line 5 def _hash_ @_hash_ end |
#_methods_ ⇒ Object (readonly)
List of the currently seen method names.
16 17 18 |
# File 'lib/origen/utility/collector.rb', line 16 def _methods_ @_methods_ end |
#merge_method ⇒ Object (readonly)
Queries the current merge_method.
8 9 10 |
# File 'lib/origen/utility/collector.rb', line 8 def merge_method @merge_method end |
Instance Method Details
#store ⇒ Hash
Use Ruby-centric #to_hash instead.
Retrieve the collector’s hash.
46 47 48 49 50 |
# File 'lib/origen/utility/collector.rb', line 46 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_hash ⇒ Hash Also known as: to_h
Returns the collector, as a Hash.
54 55 56 |
# File 'lib/origen/utility/collector.rb', line 54 def to_hash @_hash_ end |