Module: Collins::Util

Includes:
Logging
Included in:
Address, Api, Api::Logging, Api::Tag, Asset, AssetState, AssetType, Client, Ipmi, Power, PowerUnit, Profile, SimpleCallback
Defined in:
lib/collins/util.rb,
lib/collins/logging.rb

Overview

General purpose util methods

Methods in Util can be accessed by.…

* `Collins::Util.method_name`
* `ClassIncludingCollinsUtil.method_name`
* `instance_of_class_including_collins_util.method_name`

Other than #get_asset_or_tag most of these methods are not collins specific

Defined Under Namespace

Modules: Logging

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#get_logger

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/collins/util.rb', line 18

def self.included base
  base.extend(Collins::Util)
end

Instance Method Details

#deep_copy_hash(hash) ⇒ Hash

Note:

All keys and values must be serializable, Proc for instance will fail

Create a deep copy of a hash

This is useful for copying a hash that will be mutated

Parameters:

  • hash (Hash)

    the hash to copy

Returns:

  • (Hash)


28
29
30
31
# File 'lib/collins/util.rb', line 28

def deep_copy_hash hash
  require_that(hash.is_a?(Hash), "deep_copy_hash requires a hash be specified, got #{hash.class}")
  Marshal.load Marshal.dump(hash)
end

#get_asset_or_tag(asset_or_tag) ⇒ Collins::Asset

Note:

This is perhaps the only collins specific method in Util

Resolve an asset from a string tag or collins asset

Parameters:

Returns:

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/collins/util.rb', line 80

def get_asset_or_tag asset_or_tag
  asset =
    case asset_or_tag
      when Collins::Asset then asset_or_tag
      when String then Collins::Asset.new(asset_or_tag)
      when Symbol then Collins::Asset.new(asset_or_tag.to_s)
    else
      error_message = "Expected Collins::Asset, String or Symbol. Got #{asset_or_tag.class}"
      raise ExpectationFailedError.new(error_message)
    end
  if asset.nil? || asset.tag.nil? then
    raise ExpectationFailedError.new("Empty asset tag, but a tag is required")
  end
  asset
end

#require_non_empty(value, message, return_value = false) ⇒ NilClass, Object

Require that a value not be empty

If the value is a string, ensure that once stripped it’s not empty. If the value responds to ‘:empty?`, ensure that it’s not. Otherwise ensure the value isn’t nil.

Parameters:

  • value (Object)

    the value to check

  • message (String)

    the exception message to use if the value is empty

  • return_value (Boolean, Object) (defaults to: false)

    If true, returns value. If not false, returns the object

Returns:

  • (NilClass, Object)

    NilClass, or respecting return_value

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/collins/util.rb', line 43

def require_non_empty value, message, return_value = false
  guard_value = if return_value == true then
                  value
                elsif return_value != false then
                  return_value
                else
                  false
                end
  if value.is_a?(String) then
    require_that(!value.strip.empty?, message, guard_value)
  elsif value.respond_to?(:empty?) then
    require_that(!value.empty?, message, guard_value)
  else
    require_that(!value.nil?, message, guard_value)
  end
end

#require_that(guard, message, return_guard = false) ⇒ Object

Require that a guard condition passes

Simply checks that the guard is truthy, and throws an error otherwise

See Also:



64
65
66
67
68
69
70
71
72
73
# File 'lib/collins/util.rb', line 64

def require_that guard, message, return_guard = false
  if not guard then
    raise ExpectationFailedError.new(message)
  end
  if return_guard == true then
    guard
  elsif return_guard != false then
    return_guard
  end
end

#stringify_hash(hash, options = {}) ⇒ Object

Given a hash, convert all keys to strings

See Also:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/collins/util.rb', line 121

def stringify_hash hash, options = {}
  (raise ExpectationFailedError.new("stringify_hash called without a hash")) unless hash.is_a?(Hash)
  hash.inject({}) do |result, (k,v)|
    key = options[:downcase] ? k.to_s.downcase : k.to_s
    if v.is_a?(Hash) then
      result[key] = stringify_hash(v)
    elsif v.is_a?(Regexp) && options[:rewrite_regex] then
      result[key] = v.inspect[1..-2]
    else
      result[key] = v
    end
    result
  end
end

#symbolize_hash(hash, options = {}) ⇒ Object

Given a hash, rewrite keys to symbols

Parameters:

  • hash (Hash)

    the hash to symbolize

  • options (Hash) (defaults to: {})

    specify how to process the hash

Options Hash (options):

  • :rewrite_regex (Boolean)

    if the value is a regex and this is true, convert it to a string

  • :downcase (Boolean)

    if true, downcase the keys as well

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/collins/util.rb', line 103

def symbolize_hash hash, options = {}
  return {} if (hash.nil? or hash.empty?)
  (raise ExpectationFailedError.new("symbolize_hash called without a hash")) unless hash.is_a?(Hash)
  hash.inject({}) do |result, (k,v)|
    key = options[:downcase] ? k.to_s.downcase.to_sym : k.to_s.to_sym
    if v.is_a?(Hash) then
      result[key] = symbolize_hash(v)
    elsif v.is_a?(Regexp) && options[:rewrite_regex] then
      result[key] = v.inspect[1..-2]
    else
      result[key] = v
    end
    result
  end
end