module PIM::Utils
Constants
- INVALID_FILENAME_CHARACTERS
- INVALID_FILENAME_CHARACTER_REPLACEMENT
- NON_JAVAIFY_CLASSES
Public Instance Methods
add_to_collection(collection, key, value = nil)
click to toggle source
Helper method to add a key and a value to either a Hash or Array/Set.
If ‘key’ is nil, nothing will be added.
If collection is a Hash, then ‘key’ and ‘value’ are set.
If collection is an Array or Set and ‘value’ is not nil, a two-element Array with ‘key’ and ‘value’ is added. Otherwise, only ‘key’ is added.
# File pim.rb, line 1690 def add_to_collection collection, key, value = nil return if key.nil? if collection.respond_to?(:<<) if value.nil? collection << key else collection << [key, value] end elsif collection.respond_to?(:[]=) collection[key] = value end end
add_to_hashed_array(hashed_array, key, value)
click to toggle source
# File pim.rb, line 1644 def add_to_hashed_array hashed_array, key, value hashed_array[key] ||= [] hashed_array[key] << value unless hashed_array[key].include?(value) end
array_from_json(json, location)
click to toggle source
# File pim.rb, line 1020 def array_from_json json, location array = case when is_empty?(json) [] when json.is_a?(String) JSON.parse(json) when is_array?(json) json else raise "Value #{value_msg(json)}#{location} cannot be transformed into an array" end end
as_json(value)
click to toggle source
# File pim.rb, line 924 def as_json value as_json = case when value.nil? nil when value.respond_to?(:as_json) value.as_json when is_array?(value) array = [] value.each { |v| array << as_json(v) } array when is_hash?(value) hash = {} value.each { |k,v| hash[k] = as_json(v) } hash when is_range?(value) value.to_s when is_set?(value) array = [] value.each { |v| array << as_json(v) } array else value end end
calculate_checksum(value)
click to toggle source
# File pim.rb, line 1773 def calculate_checksum value checksum = 0 multiply_3 = true value.chop.reverse.each_char do |c| checksum += c.to_i * (if multiply_3 then 3 else 1 end) multiply_3 = !multiply_3 end checksum = (10 - (checksum % 10)).to_s[-1,1] end
camelize(value)
click to toggle source
# File pim.rb, line 949 def camelize value case when is_array?(value) value.map { |v| camelize(v) } when is_hash?(value) Hash[value.map { |k, v| [camelize(k).to_sym, v] }] when is_set?(value) value.map { |v| camelize(v) } else value.to_s.split('_').inject([]) { |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join end end
check_class(value, check_class)
click to toggle source
# File pim.rb, line 1783 def check_class value, check_class return true if value.nil? or value.is_a?(PIM::ErrorValue) # Check if value needs to be normalized if not value.is_a?(check_class) begin if check_class < Numeric value = to_numeric(value, check_class) || value elsif check_class <= DateTime value = normalize_value(value) { |v| parse_datetime(v) } elsif check_class <= Date value = normalize_value(value) { |v| Date.parse(v.to_s) } end rescue end end return true if value.is_a?(check_class) return false end
check_task_retry_exception(exception, &block)
click to toggle source
# File pim.rb, line 1767 def check_task_retry_exception exception, &block if PIM::Services.is_services_available? PIM::Services::ProcessTaskService.check_task_retry_exception(exception, &block) end end
cleanup_filename(filename)
click to toggle source
# File pim.rb, line 1745 def cleanup_filename filename filename.tr INVALID_FILENAME_CHARACTERS, INVALID_FILENAME_CHARACTER_REPLACEMENT end
compact_args(*args)
click to toggle source
Compacts the specified args array (removing any ‘nil’ elements). If it only contains one element and that element is an array or a hash. that array or hash value is returned. Returns either a compacted array or a hash, but never nil
# File pim.rb, line 1398 def compact_args *args args = args.compact if args.size == 1 arg = args[0] return arg.compact if is_array?(arg) return arg if is_hash?(arg) end args end
constantize(camel_cased_word)
click to toggle source
# File pim.rb, line 971 def constantize camel_cased_word names = camel_cased_word.split('::') # Trigger a built-in NameError exception including the ill-formed constant in the message. if names.empty? return nil if camel_cased_word.nil? or camel_cased_word.empty? Object.const_get(camel_cased_word) if names.empty? end # Remove the first blank element in case of '::ClassName' notation. names.shift if names.size > 1 && names.first.empty? names.inject(Object) do |constant, name| if constant == Object constant.const_get(name) else candidate = constant.const_get(name) next candidate if constant.const_defined?(name, false) next candidate unless Object.const_defined?(name) # Go down the ancestors to check if it is owned directly. The check # stops when we reach Object or the end of ancestors tree. constant = constant.ancestors.inject do |const, ancestor| break const if ancestor == Object break ancestor if ancestor.const_defined?(name, false) const end # owner is in Object, so raise constant.const_get(name, false) end end end
create_trafo_content_stream(items, content_type, params = {})
click to toggle source
# File pim.rb, line 1749 def create_trafo_content_stream items, content_type, params = {} PIM::Services::CommunicationPlanService.create_trafo_content_stream(items, content_type, params) end
date_after?(date, min_date)
click to toggle source
# File pim.rb, line 1551 def date_after? date, min_date return false if date.nil? return true if min_date.nil? return date > min_date end
date_before?(date, max_date)
click to toggle source
# File pim.rb, line 1557 def date_before? date, max_date return false if date.nil? return true if max_date.nil? return date < max_date end
date_between?(date, min_date, max_date)
click to toggle source
# File pim.rb, line 1547 def date_between? date, min_date, max_date return date_after?(date, min_date) && date_before?(date, max_date) end
deconstantize(path)
click to toggle source
# File pim.rb, line 1006 def deconstantize path path = path.to_s path[0, path.rindex('::') || 0] end
deep_compact(value)
click to toggle source
Compacts a array or hash value, i.e. recursively removes any ‘nil’ elements. Also returns nil for any value considered ‘empty?’.
# File pim.rb, line 1411 def deep_compact value if is_array?(value) value = value.map { |v| deep_compact(v) }.compact elsif is_hash?(value) value = value.map { |k, v| [k, deep_compact(v)] }.to_h.compact elsif value.respond_to?(:compact) value = value.compact end return is_empty?(value) ? nil : value end
deep_merge_if_absent(value1, value2)
click to toggle source
# File pim.rb, line 1046 def deep_merge_if_absent value1, value2 if is_hash?(value1) and is_hash?(value2) value2.each do |k2, v2| if is_hash?(v2) and !value1.has_key?(k2) value1[k2] = {} # force deep copy for hashes end end return value1.merge(value2) { |k, v1, v2| deep_merge_if_absent(v1, v2) } end return value1 unless value1.nil? return value2 end
demodulize(path)
click to toggle source
# File pim.rb, line 1011 def demodulize path path = path.to_s if i = path.rindex("::") path[(i + 2)..-1] else path end end
dup_value(value)
click to toggle source
# File pim.rb, line 1649 def dup_value value case when value.nil? nil when value.is_a?(ErrorValue) value.error_value.to_s when value.is_a?(Struct) value.dup when value.is_a?(MultiDimensional) value.dup when is_array?(value) array = [] value.each { |v| array << dup_value(v) } array when is_hash?(value) hash = {} value.each { |k,v| hash[k] = dup_value(v) } hash when is_range?(value) value.dup when is_set?(value) array = [] value.each { |v| array << dup_value(v) } array when is_date?(value) value.dup else value end end
escape_html(s)
click to toggle source
# File pim.rb, line 1706 def escape_html s s.gsub!(/</, '<') s.gsub!(/>/, '>') s.gsub!(/\n/, '<br>') s.gsub!(/\t/, ' ') s end
execute_on_parent(object, &block)
click to toggle source
# File pim.rb, line 1338 def execute_on_parent object, &block names = object.to_s.split("::") if names.length == 1 parent = Object const = object.to_s.to_sym else parent = nil const = nil names.each do |name| parent = (parent and const) ? parent.const_get(const) : Object const = name.to_sym end end parent.module_exec(const, &block) end
fix_decimal_separator(value)
click to toggle source
# File pim.rb, line 1209 def fix_decimal_separator value return value unless is_string?(value) # Scan for any grouping or decimal separator separator_scan = value.scan(/[.,']/) if separator_scan.length == 1 and separator_scan.first == ',' # Single separator is most likely a decimal separator return value.tr(separator_scan.first, '.') elsif separator_scan.length > 1 # We also want to fix thousands grouping separators! # However, in this case, we need to properly match grouped values # and only remove those "correctly" set separators! # Otherwise, we might accidentally fix an otherwise broken value. # Note: The underscore character "_" is treated by Ruby Float and Integer parsing already! # Thus, values like "1_23_56" or "123_456" are ACTUALLY valid, while "_12" or "12__34" are not. # For now, we will not deal with these, because the current regular expression is already complex enough! # This regular expression creates multiple named parts (?<name>) # and uses "backreference" (\k<name>) and "negative backreference" (?!\k<name>) # to ensure that the grouping chars are unique and different from the decimal char, if available. # # sign: optional + or - # lead: must not start with 0, up to 3 digits # group: packages of 'group_char' plus 3 digits # group_char: either of the possible group chars # decimal: must start with 'decimal_char' but not with group_char, any number of digits # decimal_char: either . or , match_data = value.match(/^(?<sign>[+-])?(?<lead>[1-9]\d{,2})(?<group>(?<group_char>[.,'])\d\d\d(\k<group_char>\d\d\d)*)(?<decimal>(?!\k<group_char>)(?<decimal_char>[.,])\d+)?$/) unless match_data.nil? sign = (match_data[:sign] || '') lead = match_data[:lead] group = match_data[:group].delete(match_data[:group_char]) decimal = match_data[:decimal].nil? ? '' : '.' + match_data[:decimal][1..-1] value = sign + lead + group + decimal end end value end
flattened_compacted_array(value)
click to toggle source
# File pim.rb, line 1190 def flattened_compacted_array value return [] if value.nil? value = value.to_a if value.respond_to?(:to_a) return value if value.empty? return [*value].flatten.compact end
get_hash_key(hash, key, default_value = nil)
click to toggle source
# File pim.rb, line 1467 def get_hash_key hash, key, default_value = nil return default_value if is_empty?(hash) key_sym = to_sym(key) return key_sym if hash.key?(key_sym) key_s = key.to_s return key_s if hash.key?(key_s) return default_value end
get_hash_value(hash, key, default_value = nil)
click to toggle source
# File pim.rb, line 1457 def get_hash_value hash, key, default_value = nil key_sym = to_sym(key) return hash[key_sym] if hash.key?(key_sym) key_s = key.to_s return hash[key_s] if hash.key?(key_s) value = hash[key_sym] || hash[key_s] value = hash[key_sym] = default_value if value.nil? and not default_value.nil? value end
get_method(object, name)
click to toggle source
# File pim.rb, line 1452 def get_method object, name method_name = to_sym(name) return object.method(method_name) if is_symbol?(method_name) and object.respond_to?(method_name) end
get_method_value(object, key)
click to toggle source
# File pim.rb, line 1447 def get_method_value object, key method = get_method(object, key) value = method.call() unless method.nil? or method.arity > 0 end
get_opt(opts, option, default = nil)
click to toggle source
# File pim.rb, line 1485 def get_opt opts, option, default = nil return default if is_empty?(opts) if opts.include?(option) return opts[option] elsif opts.include?(option.to_s) return opts[option.to_s] else return default end end
get_service_name(service)
click to toggle source
# File pim.rb, line 1714 def get_service_name service return nil if is_empty?(service) return service.service_name if PIM.has_module?(service) return service.to_s end
get_value(object, *keys)
click to toggle source
# File pim.rb, line 1427 def get_value object, *keys return nil if object.nil? value = nil keys.each do |key| if not is_struct?(object) and is_hash?(object) value = get_hash_value(object, key) if value.nil? and is_meta_attribute?(key) and is_java_item?(object) method_name = snakeize(key.to_s.delete_suffix('__')) value = get_method_value(object, method_name) end elsif is_array?(object) and key.is_a?(Integer) value = object[key] else value = get_method_value(object, key) end break unless value.nil? end return value end
has_hash_key?(hash, key)
click to toggle source
# File pim.rb, line 1476 def has_hash_key? hash, key return false if is_empty?(hash) key_sym = to_sym(key) return true if hash.key?(key_sym) key_s = key.to_s return true if hash.key?(key_s) return false end
has_java?()
click to toggle source
# File pim.rb, line 912 def has_java? return Module.const_defined?('Java') end
has_opt?(opts, option, compare_value, default = nil)
click to toggle source
# File pim.rb, line 1496 def has_opt? opts, option, compare_value, default = nil opt_value = get_opt(opts, option, default) return true if opt_value == compare_value or opt_value.to_s == compare_value.to_s return false end
hash_from_json(json, location)
click to toggle source
# File pim.rb, line 1033 def hash_from_json json, location hash = case when is_empty?(json) {} when json.is_a?(String) JSON.parse(json) when is_hash?(json) json else raise "Value #{value_msg(json)}#{location} cannot be transformed into a hash" end end
hash_value_for_key(hash, *keys)
click to toggle source
# File pim.rb, line 1328 def hash_value_for_key hash, *keys keys.each do |key| return hash[key] if hash.has_key?(key) if is_string?(key) key = key.to_sym return hash[] if hash.has_key?(key) end end end
includes_value?(value, values)
click to toggle source
# File pim.rb, line 1720 def includes_value? value, values return false if is_empty?(values) return true if values.include?(value) if value.respond_to?(:to_sym) return true if values.include?(value.to_sym) end if value.respond_to?(:to_s) return true if values.include?(value.to_s) end return false end
is_array?(value)
click to toggle source
# File pim.rb, line 1060 def is_array? value return value.respond_to?(:to_ary) end
is_blank?(value)
click to toggle source
# File pim.rb, line 1121 def is_blank? value return true if value.nil? return value.empty? if value.respond_to?(:empty?) return value.to_s.nil? || value.to_s.empty? end
is_collection?(value)
click to toggle source
# File pim.rb, line 1064 def is_collection? value return is_array?(value) || is_set?(value) end
is_date?(value)
click to toggle source
# File pim.rb, line 1104 def is_date? value return value.respond_to?(:to_date) end
is_empty?(value)
click to toggle source
# File pim.rb, line 1108 def is_empty? value return true if is_error?(value) self.is_blank? value end
is_equal?(this, that)
click to toggle source
# File pim.rb, line 1133 def is_equal? this, that return true if this.__id__ == that.__id__ return true if is_empty?(this) and is_empty?(that) return false if is_empty?(this) != is_empty?(that) return this == that end
is_error?(value)
click to toggle source
# File pim.rb, line 1113 def is_error? value return value.is_a?(PIM::ErrorValue) end
is_hash?(value)
click to toggle source
# File pim.rb, line 1068 def is_hash? value return value.respond_to?(:to_hash) end
is_java_item?(object)
click to toggle source
# File pim.rb, line 920 def is_java_item? object has_java? and defined?(com.lansa.lax.pim::Item) == 'constant' and object.is_a?(com.lansa.lax.pim::Item) end
is_java_object?(object)
click to toggle source
# File pim.rb, line 916 def is_java_object? object has_java? and defined?(java.lang::Object) == 'constant' and object.is_a?(java.lang::Object) end
is_meta_attribute?(attribute)
click to toggle source
# File pim.rb, line 1127 def is_meta_attribute? attribute return is_meta_attribute?(attribute.name) if attribute.is_a?(PIM::Attribute) return false if !attribute attribute.to_s.end_with?('__') end
is_non_javaify_class?(value)
click to toggle source
# File pim.rb, line 1272 def is_non_javaify_class? value value_class = value.class if has_java? return true if value_class <= java.lang.Object end NON_JAVAIFY_CLASSES.each do |non_javaify_class| return true if value_class <= non_javaify_class end return false end
is_not_empty?(value)
click to toggle source
# File pim.rb, line 1117 def is_not_empty? value return !is_empty?(value) end
is_opt?(opts, option, default = false)
click to toggle source
# File pim.rb, line 1502 def is_opt? opts, option, default = false return has_opt?(opts, option, true, default) end
is_proc?(value)
click to toggle source
# File pim.rb, line 1072 def is_proc? value return value.class <= Proc end
is_range?(value)
click to toggle source
# File pim.rb, line 1076 def is_range? value return value.class <= Range end
is_regexp?(value)
click to toggle source
# File pim.rb, line 1080 def is_regexp? value return value.is_a?(Regexp) end
is_set?(value)
click to toggle source
# File pim.rb, line 1084 def is_set? value return value.respond_to?(:to_set) end
is_string?(value)
click to toggle source
# File pim.rb, line 1088 def is_string? value return value.respond_to?(:to_str) end
is_struct?(value)
click to toggle source
# File pim.rb, line 1092 def is_struct? value return value.class <= Struct end
is_symbol?(value)
click to toggle source
# File pim.rb, line 1096 def is_symbol? value return value.class <= Symbol end
is_symbol_or_string?(value)
click to toggle source
# File pim.rb, line 1100 def is_symbol_or_string? value return is_symbol?(value) || is_string?(value) end
javaify_array(array)
click to toggle source
# File pim.rb, line 1296 def javaify_array array return nil if array.nil? return array if not is_array?(array) if has_java? new_array = array.class <= java.lang.Object ? array : java.util.LinkedList.new array.each_with_index { |v, i| new_array[i] = javaify_value(v) } else new_array = array.map { |v| javaify_value(v) }.to_a end new_array end
Also aliased as: javaified_array
javaify_hash(hash)
click to toggle source
# File pim.rb, line 1309 def javaify_hash hash return nil if hash.nil? return hash if not is_hash?(hash) if has_java? new_hash = hash.class <= java.lang.Object ? hash : java.util.LinkedHashMap.new hash.each_pair { |k,v| new_hash.put(javaify_value(k), javaify_value(v)) } else new_hash = hash.map { |k,v| [javaify_value(k), javaify_value(v)] }.to_h end new_hash end
Also aliased as: javaified_hash
javaify_value(value)
click to toggle source
# File pim.rb, line 1283 def javaify_value value return case when value.nil? then nil when value.respond_to?(:as_json) then javaify_value(value.as_json) when is_array?(value) then javaify_array(value) when is_hash?(value) then javaify_hash(value) when is_non_javaify_class?(value) then value else value.to_s end end
Also aliased as: javaify, javaified_value
length(value)
click to toggle source
# File pim.rb, line 1140 def length value return 0 if !value return value.length if value.respond_to?(:length) value.to_s.length end
parse_datetime(value)
click to toggle source
# File pim.rb, line 1515 def parse_datetime value return nil if is_empty?(value) if value.is_a?(Time) or value.is_a?(Date) date = value elsif value.is_a?(Numeric) # Milliseconds must be specified as fraction value = value / 1000 date = Time.at(value).to_datetime else # DateTime is normally specified as 'Unix Time' value int_value = value.to_s.to_i if int_value.to_s == value.to_s return parse_datetime(int_value) end # Try to parse the string as good as possible date = DateTime.parse(value.to_s) end # Correct time zone offset local_zone = Time.now.zone date = date.new_offset(local_zone) date end
remove_const_on_parent(object)
click to toggle source
# File pim.rb, line 1322 def remove_const_on_parent object execute_on_parent(object) do |const| remove_const(const) if const_defined?(const) end end
safe_reference_replace(string, params, missing_replacement = "?")
click to toggle source
# File pim.rb, line 1732 def safe_reference_replace string, params, missing_replacement = "?" begin string % params rescue KeyError => e # Try to extract the missing key missing_key = e.message.gsub(/.*\{(.*)\}.*/, '\1') if not is_empty?(missing_key) and not params.include?(missing_key.to_sym) params[missing_key.to_sym] = missing_replacement retry end end end
snakeize(name)
click to toggle source
# File pim.rb, line 962 def snakeize name name.to_s.strip. gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z][A-Z\d]*)/,'\1_\2'). tr("-", "_"). gsub(/_+/, '_'). downcase end
stringified_array(array)
click to toggle source
# File pim.rb, line 1158 def stringified_array array return nil if is_empty?(array) return array.to_a if not is_array?(array) and array.respond_to?(:to_a) return [array] if not is_array?(array) array.map{ |v| stringified_value(v) } end
stringified_compact_unique_array(value)
click to toggle source
# File pim.rb, line 1172 def stringified_compact_unique_array value value = flattened_compacted_array(value) return value if value.empty? return value.map(&:to_s).uniq end
stringified_hash(hash)
click to toggle source
# File pim.rb, line 1165 def stringified_hash hash return nil if is_empty?(hash) return hash.to_h if not is_hash?(hash) and hash.respond_to?(:to_h) return hash if not is_hash?(hash) Hash[hash.map{ |k,v| [stringified_value(k), stringified_value(v)] }] end
stringified_non_empty_set(value)
click to toggle source
# File pim.rb, line 1184 def stringified_non_empty_set value value = flattened_compacted_array(value) return Set.new() if value.empty? return Set.new(value.map(&:to_s).reject(&:empty?)) end
stringified_non_empty_unique_array(value)
click to toggle source
# File pim.rb, line 1178 def stringified_non_empty_unique_array value value = flattened_compacted_array(value) return value if value.empty? return value.map(&:to_s).reject(&:empty?).uniq end
stringified_value(value)
click to toggle source
# File pim.rb, line 1146 def stringified_value value return case when is_empty?(value) then nil when is_array?(value) then stringified_array(value) when is_hash?(value) then stringified_hash(value) when value.is_a?(Numeric) then value when value.is_a?(String) then value else value.to_s end end
Also aliased as: stringify
symbolized_array(array)
click to toggle source
# File pim.rb, line 1358 def symbolized_array array return nil if !array new_array = [] array.each { |value| new_array << value.to_s.strip.to_sym } new_array end
symbolized_hash(hash)
click to toggle source
# File pim.rb, line 1365 def symbolized_hash hash new_hash = {} return new_hash if !hash hash.each { |key, value| new_hash[key.to_s.strip.to_sym] = value } new_hash end
symbolized_snakeized_hash(hash)
click to toggle source
# File pim.rb, line 1386 def symbolized_snakeized_hash hash new_hash = {} return new_hash if !hash hash.each { |key, value| new_hash[snakeize(key).to_sym] = value } new_hash end
timed(group: nil, key: nil, results: nil, result_handler: nil, message: nil, filterable: false, &block)
click to toggle source
# File pim.rb, line 1955 def timed group: nil, key: nil, results: nil, result_handler: nil, message: nil, filterable: false, &block # Either explicitly define 'results' or 'result_handler', # or use 'group' to determine the value from a global variable unless group.nil? or ($timed_results.nil? and $timed_result_handlers.nil?) results ||= PIM.get_value($timed_results, group) result_handler ||= PIM.get_value($timed_result_handlers, group) end return block.call if key.nil? or (results.nil? and result_handler.nil?) require 'benchmark' return_value = nil exception = nil duration = Benchmark.realtime do begin return_value = block.call rescue => e exception = e end end message = message.call if is_proc?(message) message = message % { key: key } unless message.nil? result = TimedResult.new key: key, duration: duration, message: message, filterable: filterable results << PIM.stringified_hash(result.to_h) unless results.nil? # Store as 'stringified' Hash result_handler.call(result) unless result_handler.nil? raise exception unless exception.nil? return return_value end
to_date(value)
click to toggle source
# File pim.rb, line 1506 def to_date value begin parse_datetime value rescue Exception => e log_error("Could not parse date from value '#{value}'", e) return nil end end
to_float(value, exception: true, fix_decimal_separator: false)
click to toggle source
# File pim.rb, line 1197 def to_float value, exception: true, fix_decimal_separator: false return value if is_empty?(value) value = fix_decimal_separator(value) if fix_decimal_separator Float(value, exception: exception) end
to_gtin(value)
click to toggle source
# File pim.rb, line 1563 def to_gtin value if (value && value.respond_to?(:rjust)) return value.rjust(14, '0') else return value end end
to_instance_variable_name(value)
click to toggle source
# File pim.rb, line 1571 def to_instance_variable_name value to_sym("@#{value}") end
to_integer(value, base = 0, exception: true, fix_decimal_separator: false)
click to toggle source
# File pim.rb, line 1203 def to_integer value, base = 0, exception: true, fix_decimal_separator: false return value if is_empty?(value) value = fix_decimal_separator(value) if fix_decimal_separator Integer(value, base, exception: exception) end
to_java_exception(exception)
click to toggle source
# File pim.rb, line 1753 def to_java_exception exception return nil unless has_java? # Convert to java throwable, if necessary and possible exception = exception.to_java if exception.respond_to?(:to_java) exception = exception.to_throwable if exception.respond_to?(:to_throwable) # Ensure, exception is a Java exception return exception if exception.class <= java.lang.Throwable return nil end
to_key_value_list(hash)
click to toggle source
# File pim.rb, line 1422 def to_key_value_list hash return hash unless is_hash?(hash) hash.map { |k,v| { :key => k, :value => v } } end
to_numeric(value, default_numeric_class = Float)
click to toggle source
# File pim.rb, line 1586 def to_numeric value, default_numeric_class = Float return nil if is_empty?(value) # Handle Physical attribute value value = value.values.first if value.is_a?(Hash) # Try to convert to Float, unless value is already numeric if not value.is_a?(Numeric) and default_numeric_class < Numeric begin value = Object.send(default_numeric_class.to_s, value) rescue value = nil end end value end
to_string(object)
click to toggle source
# File pim.rb, line 1580 def to_string object return object if object.is_a?(String) return '' if object.nil? object.to_s || '' end
to_sym(object)
click to toggle source
# File pim.rb, line 1575 def to_sym object return object if object.is_a?(Class) or !object.respond_to?(:to_sym) object.to_sym end
unsymbolized_camelized_hash(hash)
click to toggle source
# File pim.rb, line 1379 def unsymbolized_camelized_hash hash new_hash = {} return new_hash if !hash hash.each { |key, value| new_hash[camelize(key)] = value } new_hash end
unsymbolized_hash(hash)
click to toggle source
# File pim.rb, line 1372 def unsymbolized_hash hash new_hash = {} return new_hash if !hash hash.each { |key, value| new_hash[key.to_s] = value } new_hash end
value_msg(value)
click to toggle source
# File pim.rb, line 1606 def value_msg value # Add the class name, unless it is String or Symbol class_msg = (not [String, Symbol].include?(value.class)) ? "of class #{value.class} " : "" value_msg = case when value.nil? # Indicate that value is 'null' "<NULL> " when (value == true or value == false or value.is_a?(Numeric)) # Show value as-is "#{value} " when (is_array?(value) or is_hash?(value) or value.is_a?(Struct)) # Only show class "#{class_msg}" when to_string(value).length > 20 # Show only up to 20 characters "'#{to_string(value).slice(0..14)}(...)' #{class_msg}" else # Show quoted value "'#{value}' #{class_msg}" end value_msg end