Module: Lutaml::Model::Utils

Defined in:
lib/lutaml/model/utils.rb

Constant Summary collapse

UNINITIALIZED =
Lutaml::Model::UninitializedClass.instance

Class Method Summary collapse

Class Method Details

.add_accessor_if_not_defined(klass, attribute) ⇒ Object



126
127
128
129
# File 'lib/lutaml/model/utils.rb', line 126

def add_accessor_if_not_defined(klass, attribute)
  add_getter_if_not_defined(klass, attribute)
  add_setter_if_not_defined(klass, attribute)
end

.add_boolean_accessor_if_not_defined(klass, attribute) ⇒ Object



131
132
133
134
# File 'lib/lutaml/model/utils.rb', line 131

def add_boolean_accessor_if_not_defined(klass, attribute)
  add_boolean_getter_if_not_defined(klass, attribute)
  add_setter_if_not_defined(klass, attribute)
end

.add_boolean_getter_if_not_defined(klass, attribute) ⇒ Object



142
143
144
145
146
# File 'lib/lutaml/model/utils.rb', line 142

def add_boolean_getter_if_not_defined(klass, attribute)
  add_method_if_not_defined(klass, "#{attribute}?") do
    !!instance_variable_get(:"@__#{attribute}")
  end
end

.add_getter_if_not_defined(klass, attribute) ⇒ Object



136
137
138
139
140
# File 'lib/lutaml/model/utils.rb', line 136

def add_getter_if_not_defined(klass, attribute)
  add_method_if_not_defined(klass, attribute) do
    instance_variable_get(:"@__#{attribute}")
  end
end

.add_if_present(hash, key, value) ⇒ Object



76
77
78
# File 'lib/lutaml/model/utils.rb', line 76

def add_if_present(hash, key, value)
  hash[key] = value if value
end

.add_method_if_not_defined(klass, method_name, &block) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/lutaml/model/utils.rb', line 118

def add_method_if_not_defined(klass, method_name, &block)
  unless klass.method_defined?(method_name)
    klass.class_eval do
      define_method(method_name, &block)
    end
  end
end

.add_setter_if_not_defined(klass, attribute) ⇒ Object



148
149
150
151
152
# File 'lib/lutaml/model/utils.rb', line 148

def add_setter_if_not_defined(klass, attribute)
  add_method_if_not_defined(klass, "#{attribute}=") do |value|
    instance_variable_set(:"@__#{attribute}", value)
  end
end

.add_singleton_method_if_not_defined(instance, method_name, &block) ⇒ Object



112
113
114
115
116
# File 'lib/lutaml/model/utils.rb', line 112

def add_singleton_method_if_not_defined(instance, method_name, &block)
  return if instance.respond_to?(method_name)

  instance.define_singleton_method(method_name, &block)
end

.base_class_name(klass) ⇒ Object

Extract the base name of the class



40
41
42
# File 'lib/lutaml/model/utils.rb', line 40

def base_class_name(klass)
  klass.to_s.split("::").last
end

.base_class_snake_case(klass) ⇒ Object

Convert the extracted base class to snake case format



45
46
47
# File 'lib/lutaml/model/utils.rb', line 45

def base_class_snake_case(klass)
  snake_case(base_class_name(klass))
end

.blank?(value) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lutaml/model/utils.rb', line 61

def blank?(value)
  value.respond_to?(:empty?) ? value.empty? : value.nil?
end

.camel_case(str) ⇒ Object

Convert string to camel case



10
11
12
13
14
# File 'lib/lutaml/model/utils.rb', line 10

def camel_case(str)
  return "" if str.nil? || str.empty?

  str.split("/").map { |part| camelize_part(part) }.join("::")
end

.classify(str) ⇒ Object

Convert string to class name



17
18
19
20
21
22
23
24
25
26
# File 'lib/lutaml/model/utils.rb', line 17

def classify(str)
  str = str.to_s.delete(".")
  str = str.sub(/^[a-z\d]*/) { |match| camel_case(match) || match }

  str.gsub("::", "/").gsub(%r{(?:_|-|(/))([a-z\d]*)}i) do
    word = Regexp.last_match(2)
    substituted = camel_case(word) || word
    Regexp.last_match(1) ? "::#{substituted}" : substituted
  end
end

.deep_dup(object) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/lutaml/model/utils.rb', line 154

def deep_dup(object)
  return object if object.nil?

  case object
  when Hash then deep_dup_hash(object)
  when Array then deep_dup_array(object)
  else deep_dup_object(object)
  end
end

.empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/lutaml/model/utils.rb', line 72

def empty?(value)
  value.respond_to?(:empty?) ? value.empty? : false
end

.empty_collection?(collection) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/lutaml/model/utils.rb', line 65

def empty_collection?(collection)
  return false if collection.nil?
  return false unless [Array, Hash].include?(collection.class)

  collection.empty?
end

.fetch_str_or_sym(hash, key, default = nil) ⇒ Object

Fetch the value from the hash using the key in string or symbol format

Examples:

hash = { "key" => "value" }
fetch_str_or_sym(hash, "key") # => "value"
fetch_str_or_sym(hash, :key) # => "value"
fetch_str_or_sym(hash, "invalid_key") # => nil

Parameters:

  • hash (Hash)

    the hash to fetch the value from

  • key (String, Symbol)

    the key to fetch the value for

Returns:

  • (Object)

    the value associated with the key



102
103
104
105
106
107
108
109
110
# File 'lib/lutaml/model/utils.rb', line 102

def fetch_str_or_sym(hash, key, default = nil)
  if hash.key?(key.to_s)
    hash[key.to_s]
  elsif hash.key?(key.to_sym)
    hash[key.to_sym]
  else
    default
  end
end

.initialized?(value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/lutaml/model/utils.rb', line 49

def initialized?(value)
  !value.equal?(UNINITIALIZED)
end

.present?(value) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lutaml/model/utils.rb', line 57

def present?(value)
  !blank?(value)
end

.snake_case(str) ⇒ Object

Convert string to snake case



29
30
31
32
33
34
35
36
37
# File 'lib/lutaml/model/utils.rb', line 29

def snake_case(str)
  str = str.to_s.tr(".", "_")
  return str unless /[A-Z-]|::/.match?(str)

  str.gsub("::", "/")
    .gsub(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { "#{$1 || $2}_" }
    .tr("-", "_")
    .downcase
end

.string_or_symbol_key?(hash, key) ⇒ Boolean

Check if the hash contains the given key in string or symbol format

Examples:

hash = { "key" => "value" }
string_or_symbol_key?(hash, "key") # => true
string_or_symbol_key?(hash, :key) # => true
string_or_symbol_key?(hash, "invalid_key") # => false

Parameters:

  • hash (Hash)

    the hash to check

  • key (String, Symbol)

    the key to check

Returns:

  • (Boolean)

    true if the hash contains the key, false otherwise



89
90
91
# File 'lib/lutaml/model/utils.rb', line 89

def string_or_symbol_key?(hash, key)
  hash.key?(key.to_s) || hash.key?(key.to_sym)
end

.uninitialized?(value) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lutaml/model/utils.rb', line 53

def uninitialized?(value)
  value.equal?(UNINITIALIZED)
end