Sha256: e4372fe1054c9007401917e5c2d68700850a96c37a7d2312636b7c44648da1c0
Contents?: true
Size: 1.57 KB
Versions: 3
Compression:
Stored size: 1.57 KB
Contents
# frozen_string_literal: true module Lutaml module Model module Utils class << self # Convert string to camel case def camel_case(str) return "" if str.nil? || str.empty? str.split("/").map { |part| camelize_part(part) }.join("::") end # Convert string to class name 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 # Convert string to snake case 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 def present?(value) !blank?(value) end def blank?(value) value.respond_to?(:empty?) ? value.empty? : !value end 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 private def camelize_part(part) part.gsub(/(?:_|-|^)([a-z\d])/i) { $1.upcase } end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
lutaml-model-0.3.13 | lib/lutaml/model/utils.rb |
lutaml-model-0.3.12 | lib/lutaml/model/utils.rb |
lutaml-model-0.3.11 | lib/lutaml/model/utils.rb |