Module: Lutaml::Model::Utils
- Defined in:
- lib/lutaml/model/utils.rb
Constant Summary collapse
- UNINITIALIZED =
Lutaml::Model::UninitializedClass.instance
- CACHE_SIZE_LIMIT =
Cache size limit to prevent memory leaks
1000
Class Method Summary collapse
- .add_accessor_if_not_defined(klass, attribute) ⇒ Object
- .add_boolean_accessor_if_not_defined(klass, attribute) ⇒ Object
- .add_boolean_getter_if_not_defined(klass, attribute) ⇒ Object
- .add_getter_if_not_defined(klass, attribute) ⇒ Object
- .add_if_present(hash, key, value) ⇒ Object
- .add_method(instance, method_name, &block) ⇒ Object
- .add_method_if_not_defined(klass, method_name, &block) ⇒ Object
- .add_setter_if_not_defined(klass, attribute) ⇒ Object
- .add_singleton_method_if_not_defined(instance, method_name) ⇒ Object
-
.base_class_name(klass) ⇒ Object
Extract the base name of the class.
-
.base_class_snake_case(klass) ⇒ Object
Convert the extracted base class to snake case format.
- .blank?(value) ⇒ Boolean
-
.camel_case(str) ⇒ Object
Convert string to camel case (cached).
-
.classify(str) ⇒ Object
Convert string to class name.
-
.clear_caches ⇒ Object
Clear all caches (call between schema compilations).
- .deep_dup(object) ⇒ Object
- .empty?(value) ⇒ Boolean
- .empty_collection?(collection) ⇒ Boolean
-
.fetch_str_or_sym(hash, key, default = nil) ⇒ Object
Fetch the value from the hash using the key in string or symbol format.
-
.immutable?(object) ⇒ Boolean
Check if object is immutable and should not be duplicated.
-
.immutable_range?(range) ⇒ Boolean
Check if Range has immutable bounds.
- .initialized?(value) ⇒ Boolean
-
.pluralize(str) ⇒ Object
Simple English pluralization for method names.
- .present?(value) ⇒ Boolean
-
.resolve_child_register(child_class, parent_register) ⇒ Symbol?
deprecated
Deprecated.
Use Register.resolve_for_child instead. This method will be removed in a future version.
-
.safe_load(file, constant) ⇒ Boolean
Safely attempts to require a file and check for a constant.
-
.snake_case(str) ⇒ Object
Convert string to snake case (cached).
-
.string_or_symbol_key?(hash, key) ⇒ Boolean
Check if the hash contains the given key in string or symbol format.
- .uninitialized?(value) ⇒ Boolean
Class Method Details
.add_accessor_if_not_defined(klass, attribute) ⇒ Object
225 226 227 228 |
# File 'lib/lutaml/model/utils.rb', line 225 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
230 231 232 233 |
# File 'lib/lutaml/model/utils.rb', line 230 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
241 242 243 244 245 |
# File 'lib/lutaml/model/utils.rb', line 241 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
235 236 237 238 239 |
# File 'lib/lutaml/model/utils.rb', line 235 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
139 140 141 |
# File 'lib/lutaml/model/utils.rb', line 139 def add_if_present(hash, key, value) hash[key] = value if value end |
.add_method(instance, method_name, &block) ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/lutaml/model/utils.rb', line 188 def add_method(instance, method_name, &block) if instance.is_a?(Class) instance.class_eval do define_method(method_name, &block) end else instance.define_singleton_method(method_name, &block) end end |
.add_method_if_not_defined(klass, method_name, &block) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/lutaml/model/utils.rb', line 217 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
247 248 249 250 251 |
# File 'lib/lutaml/model/utils.rb', line 247 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) ⇒ Object
198 199 200 201 202 |
# File 'lib/lutaml/model/utils.rb', line 198 def add_singleton_method_if_not_defined(instance, method_name, &) return if instance.singleton_class.method_defined?(method_name, false) instance.define_singleton_method(method_name, &) end |
.base_class_name(klass) ⇒ Object
Extract the base name of the class
96 97 98 |
# File 'lib/lutaml/model/utils.rb', line 96 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
101 102 103 |
# File 'lib/lutaml/model/utils.rb', line 101 def base_class_snake_case(klass) snake_case(base_class_name(klass)) end |
.blank?(value) ⇒ Boolean
117 118 119 120 121 122 123 |
# File 'lib/lutaml/model/utils.rb', line 117 def blank?(value) case value when ::String, ::Array, ::Hash then value.empty? when ::NilClass then true else false end end |
.camel_case(str) ⇒ Object
Convert string to camel case (cached)
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lutaml/model/utils.rb', line 34 def camel_case(str) return "" if str.nil? || str.empty? @camel_case_cache ||= {} cached = @camel_case_cache[str] return cached if cached result = str.split("/").map { |part| camelize_part(part) }.join("::") # Evict oldest entry if cache is full @camel_case_cache.shift if @camel_case_cache.size >= CACHE_SIZE_LIMIT @camel_case_cache[str] = result end |
.classify(str) ⇒ Object
Convert string to class name
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lutaml/model/utils.rb', line 49 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 |
.clear_caches ⇒ Object
Clear all caches (call between schema compilations)
13 14 15 16 |
# File 'lib/lutaml/model/utils.rb', line 13 def clear_caches @camel_case_cache = {} @snake_case_cache = {} end |
.deep_dup(object) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/lutaml/model/utils.rb', line 253 def deep_dup(object) return object if object.nil? return object if immutable?(object) 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
132 133 134 135 136 137 |
# File 'lib/lutaml/model/utils.rb', line 132 def empty?(value) case value when ::String, ::Array, ::Hash then value.empty? else false end end |
.empty_collection?(collection) ⇒ Boolean
125 126 127 128 129 130 |
# File 'lib/lutaml/model/utils.rb', line 125 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/lutaml/model/utils.rb', line 165 def fetch_str_or_sym(hash, key, default = nil) case key when String if hash.key?(key) hash[key] else (hash.key?(key.to_sym) ? hash[key.to_sym] : default) end when Symbol if hash.key?(key) hash[key] else (hash.key?(key.to_s) ? hash[key.to_s] : default) end else if hash.key?(key.to_s) hash[key.to_s] else (hash.key?(key.to_sym) ? hash[key.to_sym] : default) end end end |
.immutable?(object) ⇒ Boolean
Check if object is immutable and should not be duplicated
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/lutaml/model/utils.rb', line 265 def immutable?(object) object.is_a?(Symbol) || object.is_a?(TrueClass) || object.is_a?(FalseClass) || object.is_a?(Numeric) || object.is_a?(Class) || object.is_a?(Module) || object.is_a?(Proc) || object.is_a?(Method) || (object.is_a?(Range) && immutable_range?(object)) end |
.immutable_range?(range) ⇒ Boolean
Check if Range has immutable bounds
278 279 280 |
# File 'lib/lutaml/model/utils.rb', line 278 def immutable_range?(range) immutable?(range.begin) && (range.end.nil? || immutable?(range.end)) end |
.initialized?(value) ⇒ Boolean
105 106 107 |
# File 'lib/lutaml/model/utils.rb', line 105 def initialized?(value) !value.equal?(UNINITIALIZED) end |
.pluralize(str) ⇒ Object
Simple English pluralization for method names. Handles common cases; not a full inflector.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/lutaml/model/utils.rb', line 81 def pluralize(str) str = str.to_s return str if str.empty? if str.end_with?("s", "x", "z", "ch", "sh") "#{str}es" elsif str.end_with?("y") && str.length > 1 && !%w[a e i o u].include?(str[-2]) "#{str[0..-2]}ies" else "#{str}s" end end |
.present?(value) ⇒ Boolean
113 114 115 |
# File 'lib/lutaml/model/utils.rb', line 113 def present?(value) !blank?(value) end |
.resolve_child_register(child_class, parent_register) ⇒ Symbol?
Use Register.resolve_for_child instead. This method will be removed in a future version.
Determines the appropriate register for a child model during deserialization.
212 213 214 215 |
# File 'lib/lutaml/model/utils.rb', line 212 def resolve_child_register(child_class, parent_register) Lutaml::Model::Register.resolve_for_child(child_class, parent_register) end |
.safe_load(file, constant) ⇒ Boolean
Safely attempts to require a file and check for a constant
23 24 25 26 27 28 29 30 31 |
# File 'lib/lutaml/model/utils.rb', line 23 def safe_load(file, constant) return true if Object.const_defined?(constant) require file Object.const_defined?(constant) rescue LoadError false end |
.snake_case(str) ⇒ Object
Convert string to snake case (cached)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lutaml/model/utils.rb', line 61 def snake_case(str) str = str.to_s.tr(".", "_") return str unless /[A-Z-]|::/.match?(str) @snake_case_cache ||= {} cached = @snake_case_cache[str] return cached if cached result = str.gsub("::", "/") .gsub(/([A-Z][A-Z]*)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { "#{$1 || $2}_" } .tr("-", "_") .downcase # Evict oldest entry if cache is full @snake_case_cache.shift if @snake_case_cache.size >= CACHE_SIZE_LIMIT @snake_case_cache[str] = result end |
.string_or_symbol_key?(hash, key) ⇒ Boolean
Check if the hash contains the given key in string or symbol format
152 153 154 |
# File 'lib/lutaml/model/utils.rb', line 152 def string_or_symbol_key?(hash, key) hash.key?(key.to_s) || hash.key?(key.to_sym) end |
.uninitialized?(value) ⇒ Boolean
109 110 111 |
# File 'lib/lutaml/model/utils.rb', line 109 def uninitialized?(value) value.equal?(UNINITIALIZED) end |