Sha256: 676466cd4fbf37ae2f8b9148ffb0100241993f36b64c35cc58745c90a077f581

Contents?: true

Size: 1000 Bytes

Versions: 2

Compression:

Stored size: 1000 Bytes

Contents

require 'hari/entity/serialization/boolean'
require 'hari/entity/serialization/date'
require 'hari/entity/serialization/datetime'
require 'hari/entity/serialization/float'
require 'hari/entity/serialization/integer'
require 'hari/entity/serialization/string'
require 'hari/entity/serialization/time'

module Hari
  class Entity
    module Serialization
      extend ActiveSupport::Concern

      def to_json
        hash = self.class.properties.inject({}) do |buffer, prop|
          buffer.merge prop.name => prop.serialize(send(prop.name))
        end

        Yajl::Encoder.encode hash
      end

      module ClassMethods

        def from_json(source)
          return if source.blank?

          attrs = Yajl::Parser.parse(source).inject({}) do |buffer, (key, value)|
            if prop = properties.find { |p| p.name == key }
              buffer[key] = prop.desserialize(value)
            end

            buffer
          end

          new attrs
        end

      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hari-0.0.3 lib/hari/entity/serialization.rb
hari-0.0.1 lib/hari/entity/serialization.rb