Sha256: 9d1887bb8209018ef2e61bcda219ed9b2a7d140d5e49c26da143f1c29ebf3713

Contents?: true

Size: 893 Bytes

Versions: 1

Compression:

Stored size: 893 Bytes

Contents

module SimpleSegment
  module Utils
    def self.included(klass)
      klass.extend(self)
    end

    def symbolize_keys(hash)
      hash.each_with_object({}) do |(key, value), result|
        result[key.to_sym] = value
      end
    end

    # public: Converts all the date values in the into iso8601 strings in place
    #
    def isoify_dates!(hash)
      hash.replace isoify_dates hash
    end

    # public: Returns a new hash with all the date values in the into iso8601
    #         strings
    #
    def isoify_dates(hash)
      hash.each_with_object({}) do |(k, v), memo|
        memo[k] = maybe_datetime_in_iso8601(v)
      end
    end

    def maybe_datetime_in_iso8601(prop)
      case prop
      when Time
        prop.iso8601(3)
      when DateTime
        prop.to_time.iso8601(3)
      when Date
        prop.strftime('%F')
      else
        prop
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_segment-0.3.0 lib/simple_segment/utils.rb