class KUtil::DataHelper

Helper methods attached to the namespace for working with Data

Public Instance Methods

basic_type?(value) click to toggle source

Is the value a basic (aka primitive) type

# File lib/k_util/data_helper.rb, line 81
def basic_type?(value)
  value.is_a?(String) ||
    value.is_a?(Symbol) ||
    value.is_a?(FalseClass) ||
    value.is_a?(TrueClass) ||
    value.is_a?(Integer) ||
    value.is_a?(Float)
end
clean_symbol(value) click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/k_util/data_helper.rb, line 55
def clean_symbol(value)
  return value if value.nil?

  value.is_a?(Symbol) ? value.to_s : value
end
deep_symbolize_keys(input) click to toggle source
# File lib/k_util/data_helper.rb, line 61
def deep_symbolize_keys(input)
  return input if input.nil?

  return input unless input.is_a?(Hash)

  input.each_with_object({}) do |key_value, new_hash|
    key, value = key_value
    value = deep_symbolize_keys(value)                  if value.is_a?(Hash)
    value = value.map { |v| deep_symbolize_keys(v) }    if value.is_a?(Array)
    new_hash[key.to_sym] = value
  end
end
hash_convertible?(value) click to toggle source

Is the value a complex container type, but not a regular class.

# File lib/k_util/data_helper.rb, line 91
def hash_convertible?(value)
  # Nil is a special case, it responds to :to_h but generally
  # you only want to convert nil to {} in specific scenarios
  return false if value.nil?

  value.is_a?(Array) ||
    value.is_a?(Hash) ||
    value.is_a?(Struct) ||
    value.is_a?(OpenStruct) ||
    value.respond_to?(:to_h)
end
json_parse(json, as: :hash)
Alias for: parse_json
parse_json(json, as: :hash) click to toggle source

Convert JSON string into to_open_struct but designed to work with a JSON string

KUtil.data.parse_json(json, as: :symbolize_keys) docs.ruby-lang.org/en/master/JSON.html rubocop:disable Naming/MethodParameterName

# File lib/k_util/data_helper.rb, line 12
def parse_json(json, as: :hash)
  log.block(%i[hash hash_symbolized open_struct], title: 'Help as: ?') if as == :help

  case as
  when :hash
    JSON.parse(json)
  when :hash_symbolized, :symbolize_names, :symbolize_keys
    JSON.parse(json, symbolize_names: true)
  when :open_struct
    JSON.parse(json, object_class: OpenStruct)
  end
end
Also aliased as: json_parse
to_hash(data) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity Convert data to hash and deal with mixed data types such as Struct and OpenStruct

# File lib/k_util/data_helper.rb, line 41
def to_hash(data)
  # This nil check is only for the root object
  return {} if data.nil?

  return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)

  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)

  data.each_pair.with_object({}) do |(key, value), hash|
    hash[key] = hash_convertible?(value) ? to_hash(value) : value
  end
end
to_open_struct(data) click to toggle source

Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct

or an array of deep nested OpenStruct

# File lib/k_util/data_helper.rb, line 30
def to_open_struct(data)
  return OpenStruct.new(data.transform_values { |v| to_open_struct(v) })  if data.is_a?(Hash)
  return data.map { |o| to_open_struct(o) }                               if data.is_a?(Array)
  return to_open_struct(data.to_h)                                        if hash_convertible?(data)

  # Some primitave type: String, True/False or an ObjectStruct
  data
end