class Origen::Utility::Collector

Attributes

_hash_[R]

Returns the collector’s hash. This is the same as calling {#to_h}

_methods_[R]

List of the currently seen method names.

merge_method[R]

Queries the current merge_method.

Public Class Methods

new(options = {}) { |self| ... } click to toggle source

Creates a new Collector object and creates a Hash out of the methods names and values in the given block. @see origen-sdk.org/origen/guides/misc/utilities/#Collector @example Create a collector to transform a block into a Hash

Origen::Utility::Collector.new { |c| c.my_param 'My Parameter'}.to_h #=> {my_param: 'My Parameter'}

@yield [self] Passes the collector to the given block. @param options [Hash] Customization options. @option options [Hash] :hash Input, or starting, values that are set in the output hash prior to calling the given block. @option options [Symbol] :merge_method (:keep_hash) Indicates how arguments that exist in both the input hash and in the block should be handled.

Accpeted values are :keep_hash, :keep_block, :fail.

@raise [Origen::OrigenError] Raised when an unknown merge method is used.

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

Public Instance Methods

method_missing(method, *args, &_block) click to toggle source

Using the method name, creates a key in the Collector with argument given to the method. @see origen-sdk.org/origen/guides/misc/utilities/#Collector @note If no args are given, the method key is set to nil. @raise [ArgumentError] Raised when a method attempts to use both arguments and a block in the same line.

E.g.: <code>collector.my_param 'my_param' { 'MyParam' }</code>

@raise [ArgumentError] Raised when a method attempts to use multiple input values.

E.g.: <code>collector.my_param 'my_param', 'MyParam'</code>

@raise [Origen::OrigenError] Raised when a method is set more than once. I.e., overriding values are not allowed.

E.g.: <code>collector.my_param 'my_param'; collector.my_param 'MyParam'</code>

@raise [Origen::OrigenError] Raised when the input hash and the block attempt to set the same method name and the merge method is set to :fail.

# 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
store() click to toggle source

Retrieve the collector’s hash. @deprecated Use Ruby-centric {#to_hash} instead. @return [Hash] Hash representation of the collector.

# 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_h()
Alias for: to_hash
to_hash() click to toggle source

Returns the collector, as a Hash. @return [Hash] Hash representation of the collector.

# File lib/origen/utility/collector.rb, line 54
def to_hash
  @_hash_
end
Also aliased as: to_h