module Framed::Utils

Public Instance Methods

flattened_hash(h, namespace = '', init = {}) click to toggle source
# File lib/framed/utils.rb, line 42
def flattened_hash(h, namespace = '', init = {})
  h.reduce(init) do |memo, (key, value)|
    value = value.to_h if value.respond_to?(:to_h)
    if value.instance_of?(Hash)
      memo.merge!(flattened_hash(value, "#{namespace}#{key}_", memo))
    else
      memo["#{namespace}#{key}"] = value
    end
    memo
  end
end
serialize_date(dt) click to toggle source
# File lib/framed/utils.rb, line 38
def serialize_date(dt)
  dt.utc.iso8601
end
try(o, *a, &b) click to toggle source

Adapted from Rails in case it isn't available.

# File lib/framed/utils.rb, line 22
def try(o, *a, &b)
  try!(o, *a, &b) if a.empty? || o.respond_to?(a.first)
end
try!(o, *a) { |o| ... } click to toggle source
# File lib/framed/utils.rb, line 26
def try!(o, *a, &b)
  if a.empty? && block_given?
    if b.arity.zero?
      o.instance_eval(&b)
    else
      yield o
    end
  else
    o.public_send(*a, &b)
  end
end
uuid() click to toggle source
# File lib/framed/utils.rb, line 13
def uuid
  begin
    UUID.new.generate
  rescue NameError
    SecureRandom.uuid
  end
end