Sha256: bbb5374751b4a2d42ced1e486ef648b4281a046c540ed24eb8a58ec0c0740136

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module Gorillib

  module Hashlike
    module Serialization
      #
      # Returns a hash with each key set to its associated value
      #
      # @example
      #    my_hshlike = MyHashlike.new
      #    my_hshlike[:a] = 100; my_hshlike[:b] = 200
      #    my_hshlike.to_hash # => { :a => 100, :b => 200 }
      #
      # @return [Hash] a new Hash instance, with each key set to its associated value.
      #
      def to_wire(options={})
        {}.tap do |hsh|
          each do |attr,val|
            hsh[attr] =
              case
              when val.respond_to?(:to_wire) then val.to_wire(options)
              when val.respond_to?(:to_hash) then val.to_hash
              else val ; end
          end
        end
      end
    end
  end
end

module Gorillib::Hashlike
  include ::Gorillib::Hashlike::Serialization
end

class ::Array
  def to_wire(options={})
    map{|item| item.respond_to?(:to_wire) ? item.to_wire : item }
  end
end

class ::Hash
  include ::Gorillib::Hashlike::Serialization
end

class ::Time
  def to_wire(options={})
    self.iso8601
  end
end

class ::NilClass
  def to_wire(options={})
    nil
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gorillib-model-0.0.3 lib/gorillib/model/serialization/to_wire.rb
gorillib-model-0.0.1 lib/gorillib/model/serialization/to_wire.rb