Class: KUtil::DataHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/k_util/data_helper.rb

Overview

Helper methods attached to the namespace for working with Data

Instance Method Summary collapse

Instance Method Details

#basic_type?(value) ⇒ Boolean

Is the value a basic (aka primitive) type

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
# File 'lib/k_util/data_helper.rb', line 83

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) ⇒ Object

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



57
58
59
60
61
# File 'lib/k_util/data_helper.rb', line 57

def clean_symbol(value)
  return value if value.nil?

  value.is_a?(Symbol) ? value.to_s : value
end

#deep_symbolize_keys(input) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/k_util/data_helper.rb', line 63

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) ⇒ Boolean

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

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/k_util/data_helper.rb', line 93

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

#parse_json(json, as: :hash) ⇒ Object Also known as: json_parse

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



12
13
14
15
16
17
18
19
20
21
22
23
# 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

#to_hash(data) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/k_util/data_helper.rb', line 43

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) ⇒ Object

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

KUtil.data.to_open_struct(data) or an array of deep nested OpenStruct



31
32
33
34
35
36
37
38
# File 'lib/k_util/data_helper.rb', line 31

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