Module: Lutaml::Model::Utils
- Defined in:
- lib/lutaml/model/utils.rb
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_method_if_not_defined(klass, method_name, &block) ⇒ Object
- .add_setter_if_not_defined(klass, attribute) ⇒ Object
- .blank?(value) ⇒ Boolean
-
.camel_case(str) ⇒ Object
Convert string to camel case.
-
.classify(str) ⇒ Object
Convert string to class name.
- .deep_dup(object) ⇒ Object
- .empty?(value) ⇒ Boolean
- .empty_collection?(collection) ⇒ Boolean
-
.fetch_with_string_or_symbol_key(hash, key) ⇒ Object
Fetch the value from the hash using the key in string or symbol format.
- .initialized?(value) ⇒ Boolean
- .present?(value) ⇒ Boolean
-
.snake_case(str) ⇒ Object
Convert string to snake case.
-
.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
104 105 106 107 |
# File 'lib/lutaml/model/utils.rb', line 104 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
109 110 111 112 |
# File 'lib/lutaml/model/utils.rb', line 109 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
120 121 122 123 124 |
# File 'lib/lutaml/model/utils.rb', line 120 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
114 115 116 117 118 |
# File 'lib/lutaml/model/utils.rb', line 114 def add_getter_if_not_defined(klass, attribute) add_method_if_not_defined(klass, attribute) do instance_variable_get(:"@__#{attribute}") end end |
.add_method_if_not_defined(klass, method_name, &block) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/lutaml/model/utils.rb', line 96 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
126 127 128 129 130 |
# File 'lib/lutaml/model/utils.rb', line 126 def add_setter_if_not_defined(klass, attribute) add_method_if_not_defined(klass, "#{attribute}=") do |value| instance_variable_set(:"@__#{attribute}", value) end end |
.blank?(value) ⇒ Boolean
51 52 53 |
# File 'lib/lutaml/model/utils.rb', line 51 def blank?(value) value.respond_to?(:empty?) ? value.empty? : value.nil? end |
.camel_case(str) ⇒ Object
Convert string to camel case
8 9 10 11 12 |
# File 'lib/lutaml/model/utils.rb', line 8 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
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lutaml/model/utils.rb', line 15 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
132 133 134 135 136 137 138 139 140 |
# File 'lib/lutaml/model/utils.rb', line 132 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
62 63 64 |
# File 'lib/lutaml/model/utils.rb', line 62 def empty?(value) value.respond_to?(:empty?) ? value.empty? : false end |
.empty_collection?(collection) ⇒ Boolean
55 56 57 58 59 60 |
# File 'lib/lutaml/model/utils.rb', line 55 def empty_collection?(collection) return false if collection.nil? return false unless [Array, Hash].include?(collection.class) collection.empty? end |
.fetch_with_string_or_symbol_key(hash, key) ⇒ Object
Fetch the value from the hash using the key in string or symbol format
88 89 90 91 92 93 94 |
# File 'lib/lutaml/model/utils.rb', line 88 def fetch_with_string_or_symbol_key(hash, key) if hash.key?(key.to_s) hash[key.to_s] elsif hash.key?(key.to_sym) hash[key.to_sym] end end |
.initialized?(value) ⇒ Boolean
37 38 39 40 41 |
# File 'lib/lutaml/model/utils.rb', line 37 def initialized?(value) return true unless value.respond_to?(:initialized?) value.initialized? end |
.present?(value) ⇒ Boolean
47 48 49 |
# File 'lib/lutaml/model/utils.rb', line 47 def present?(value) !blank?(value) end |
.snake_case(str) ⇒ Object
Convert string to snake case
27 28 29 30 31 32 33 34 35 |
# File 'lib/lutaml/model/utils.rb', line 27 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
75 76 77 |
# File 'lib/lutaml/model/utils.rb', line 75 def string_or_symbol_key?(hash, key) hash.key?(key.to_s) || hash.key?(key.to_sym) end |
.uninitialized?(value) ⇒ Boolean
43 44 45 |
# File 'lib/lutaml/model/utils.rb', line 43 def uninitialized?(value) !initialized?(value) end |