class Dynflow::Serializable

Constants

LEGACY_TIME_FORMAT
TIME_FORMAT

Public Class Methods

constantize(action_name) click to toggle source
# File lib/dynflow/serializable.rb, line 35
def self.constantize(action_name)
  Utils.constantize(action_name)
end
from_hash(hash, *args) click to toggle source
# File lib/dynflow/serializable.rb, line 9
def self.from_hash(hash, *args)
  check_class_key_present hash
  constantize(hash[:class]).new_from_hash(hash, *args)
end
new_from_hash(hash, *args) click to toggle source

@api private

# File lib/dynflow/serializable.rb, line 19
def self.new_from_hash(hash, *args)
  raise NotImplementedError
  # new ...
end

Private Class Methods

check_class_key_present(hash) click to toggle source
# File lib/dynflow/serializable.rb, line 31
def self.check_class_key_present(hash)
  raise ArgumentError, "missing :class in #{hash.inspect}" unless hash[:class]
end
check_class_matching(hash) click to toggle source
# File lib/dynflow/serializable.rb, line 24
def self.check_class_matching(hash)
  check_class_key_present hash
  unless self.to_s == hash[:class]
    raise ArgumentError, "class mismatch #{hash[:class]} != #{self}"
  end
end
hash_to_error(hash) click to toggle source
# File lib/dynflow/serializable.rb, line 82
def self.hash_to_error(hash)
  return nil if hash.nil?
  ExecutionPlan::Steps::Error.from_hash(hash)
end
string_to_time(string) click to toggle source
# File lib/dynflow/serializable.rb, line 64
def self.string_to_time(string)
  return if string.nil?
  return string if string.is_a?(Time)
  time = begin
           DateTime.strptime(string, TIME_FORMAT)
         rescue ArgumentError => _
           DateTime.strptime(string, LEGACY_TIME_FORMAT)
         end

  time.to_time.utc
end

Public Instance Methods

to_hash() click to toggle source
# File lib/dynflow/serializable.rb, line 14
def to_hash
  raise NotImplementedError
end

Private Instance Methods

recursive_to_hash(*values) click to toggle source

recursively traverses hash-array structure and converts all to hashes accepts more hashes which are then merged

# File lib/dynflow/serializable.rb, line 45
def recursive_to_hash(*values)
  if values.size == 1
    value = values.first
    case value
    when Hash
      value.inject({}) { |h, (k, v)| h.update k => recursive_to_hash(v) }
    when Array
      value.map { |v| recursive_to_hash v }
    when ->(v) { v.respond_to?(:to_msgpack) }
      value
    else
      value.to_hash
    end
  else
    values.all? { |v| Type! v, Hash, NilClass }
    recursive_to_hash(values.compact.reduce { |h, v| h.merge v })
  end
end
time_to_str(time) click to toggle source
# File lib/dynflow/serializable.rb, line 76
def time_to_str(time)
  return if time.nil?
  Type! time, Time
  time.utc.strftime(TIME_FORMAT)
end