Module: OCI::Internal::Util
- Defined in:
- lib/oci/internal/util.rb
Overview
Utility methods for internal use only.
Constant Summary collapse
- BLANK_STRING_REGEX =
/\A[[:space:]]*\z/.freeze
Class Method Summary collapse
- .blank_string?(str) ⇒ Boolean
-
.convert_to_type(type, data) ⇒ Mixed
Convert data to the given type.
Class Method Details
.blank_string?(str) ⇒ Boolean
9 10 11 |
# File 'lib/oci/internal/util.rb', line 9 def self.blank_string?(str) str.nil? || str.empty? || !BLANK_STRING_REGEX.match(str).nil? end |
.convert_to_type(type, data) ⇒ Mixed
Convert data to the given type. This is part of the conversion from strings representing json, where the data that is passed in should already have been converted using JSON.parse. That will give us arrays and hashes, but will not set the correct data types.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/oci/internal/util.rb', line 20 def self.convert_to_type(type, data) return nil if data.nil? case type.downcase.to_sym when :datetime DateTime.parse(data) when :date Date.parse(data) when :string data.to_s when :integer data.to_i when :float data.to_f when :boolean if data.to_s =~ /^(true|yes|1)$/i true else false end when :object # generic object, return directly data else # The remaining cases might need to match a specific model class, so don't ignore case. case type.to_sym when /\AArray<(?<inner_type>.+)>\z/ inner_type = Regexp.last_match[:inner_type] data.map { |v| convert_to_type(inner_type, v) } when /\AHash<(?<k_type>.+?), (?<v_type>.+?)>\z/ k_type = Regexp.last_match[:k_type] v_type = Regexp.last_match[:v_type] {}.tap do |hash| data.each do |k, v| hash[convert_to_type(k_type, k)] = convert_to_type(v_type, v) end end else # Type must be a model. type = OCI.const_get(type.to_s) if defined? type.get_subtype # The return type may be an abstract type, in which case # we need to use the discriminator value to get the correct subtype. type = OCI.const_get(type.get_subtype(data)) end type.new.tap do |model| model.build_from_hash data end end end end |