class Dynflow::Serializable

Public Class Methods

constantize(action_name) click to toggle source
# File lib/dynflow/serializable.rb, line 29
def self.constantize(action_name)
  Utils.constantize(action_name)
end
from_hash(hash, *args) click to toggle source
# File lib/dynflow/serializable.rb, line 3
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 13
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 25
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 18
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 71
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 58
def self.string_to_time(string)
  return if string.nil?
  _, year, month, day, hour, min, sec =
      */(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.match(string)
  Time.utc(year.to_i, month.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i)
end

Public Instance Methods

to_hash() click to toggle source
# File lib/dynflow/serializable.rb, line 8
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 39
def recursive_to_hash(*values)
  if values.size == 1
    value = values.first
    case value
    when String, Numeric, Symbol, TrueClass, FalseClass, NilClass
      value
    when Hash
      value.inject({}) { |h, (k, v)| h.update k => recursive_to_hash(v) }
    when Array
      value.map { |v| recursive_to_hash v }
    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 65
def time_to_str(time)
  return if time.nil?
  Type! time, Time
  time.utc.strftime '%Y-%m-%d %H:%M:%S'
end