Sha256: 5a071d37116f2a660ecce0411d2a46c285a79bca03494f938654813eb3b9cc26

Contents?: true

Size: 1004 Bytes

Versions: 5

Compression:

Stored size: 1004 Bytes

Contents

module CouchPotato
  module Persistence
    module Json
      def self.included(base)
        base.extend ClassMethods
      end
      
      def to_json(*args)
        to_hash.to_json(*args)
      end
      
      def to_hash
        (self.class.properties).inject({}) do |props, property|
          property.serialize(props, self)
          props
        end.merge('ruby_class' => self.class.name).merge(id_and_rev_json)
      end
      
      private
      
      def id_and_rev_json
        ['_id', '_rev', '_deleted'].inject({}) do |hash, key|
          hash[key] = self.send(key) unless self.send(key).nil?
          hash
        end
      end
      
      module ClassMethods
        def json_create(json)
          instance = self.new
          instance._id = json[:_id] || json['_id']
          instance._rev = json[:_rev] || json['_rev']
          properties.each do |property|
            property.build(instance, json)
          end
          instance
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
langalex-couch_potato-0.2.0 lib/couch_potato/persistence/json.rb
langalex-couch_potato-0.2.1 lib/couch_potato/persistence/json.rb
langalex-couch_potato-0.2.2 lib/couch_potato/persistence/json.rb
speedmax-couch_potato-0.2.0 lib/couch_potato/persistence/json.rb
speedmax-couch_potato-0.2.2 lib/couch_potato/persistence/json.rb