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
-
.last_of_split(field) ⇒ Object
Return the final segment of a
prefix:localstyle qualified name, e.g. -
.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
231 232 233 234 |
# File 'lib/lutaml/model/utils.rb', line 231 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
236 237 238 239 |
# File 'lib/lutaml/model/utils.rb', line 236 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
247 248 249 250 251 |
# File 'lib/lutaml/model/utils.rb', line 247 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
241 242 243 244 245 |
# File 'lib/lutaml/model/utils.rb', line 241 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
145 146 147 |
# File 'lib/lutaml/model/utils.rb', line 145 def add_if_present(hash, key, value) hash[key] = value if value end |
.add_method(instance, method_name, &block) ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/lutaml/model/utils.rb', line 194 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
223 224 225 226 227 228 229 |
# File 'lib/lutaml/model/utils.rb', line 223 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
253 254 255 256 257 |
# File 'lib/lutaml/model/utils.rb', line 253 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
204 205 206 207 208 |
# File 'lib/lutaml/model/utils.rb', line 204 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
123 124 125 126 127 128 129 |
# File 'lib/lutaml/model/utils.rb', line 123 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
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/lutaml/model/utils.rb', line 259 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
138 139 140 141 142 143 |
# File 'lib/lutaml/model/utils.rb', line 138 def empty?(value) case value when ::String, ::Array, ::Hash then value.empty? else false end end |
.empty_collection?(collection) ⇒ Boolean
131 132 133 134 135 136 |
# File 'lib/lutaml/model/utils.rb', line 131 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/lutaml/model/utils.rb', line 171 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
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/lutaml/model/utils.rb', line 271 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
284 285 286 |
# File 'lib/lutaml/model/utils.rb', line 284 def immutable_range?(range) immutable?(range.begin) && (range.end.nil? || immutable?(range.end)) end |
.initialized?(value) ⇒ Boolean
111 112 113 |
# File 'lib/lutaml/model/utils.rb', line 111 def initialized?(value) !value.equal?(UNINITIALIZED) end |
.last_of_split(field) ⇒ Object
Return the final segment of a prefix:local style qualified name,
e.g. "xs:string" -> "string". Returns nil for nil input.
107 108 109 |
# File 'lib/lutaml/model/utils.rb', line 107 def last_of_split(field) field&.split(":")&.last 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
119 120 121 |
# File 'lib/lutaml/model/utils.rb', line 119 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.
218 219 220 221 |
# File 'lib/lutaml/model/utils.rb', line 218 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
158 159 160 |
# File 'lib/lutaml/model/utils.rb', line 158 def string_or_symbol_key?(hash, key) hash.key?(key.to_s) || hash.key?(key.to_sym) end |
.uninitialized?(value) ⇒ Boolean
115 116 117 |
# File 'lib/lutaml/model/utils.rb', line 115 def uninitialized?(value) value.equal?(UNINITIALIZED) end |