Sha256: 4c2ef438302e2eb4f0537f73f54ab09aabb0add98d4de9c3a469f85adb36b798

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

module CouchPotato
  module Persistence
    module Json
      def self.included(base) #:nodoc:
        base.class_eval do
          extend ClassMethods
          attr_accessor :_document
        end
      end
      
      # returns a JSON representation of a model in order to store it in CouchDB
      def to_json(*args)
        to_hash.to_json(*args)
      end
      
      # returns all the attributes, the ruby class and the _id and _rev of a model as a Hash
      def to_hash
        (self.class.properties).inject({}) do |props, property|
          property.serialize(props, self)
          props
        end.merge(JSON.create_id => 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
        
        # creates a model instance from JSON
        def json_create(json)
          return if json.nil?
          instance = self.new
          instance._id = json[:_id] || json['_id']
          instance._rev = json[:_rev] || json['_rev']
          instance._document = HashWithIndifferentAccess.new(json)
          instance
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
couch_potato-0.5.7 lib/couch_potato/persistence/json.rb
couch_potato-rails2-0.5.10 lib/couch_potato/persistence/json.rb
couch_potato-rails2-0.5.9 lib/couch_potato/persistence/json.rb
couch_potato-rails2-0.5.8 lib/couch_potato/persistence/json.rb
couch_potato-rails2-0.5.7 lib/couch_potato/persistence/json.rb
couch_potato-rails2-0.5.6 lib/couch_potato/persistence/json.rb
couch_potato-0.5.6 lib/couch_potato/persistence/json.rb
couch_potato-0.5.5 lib/couch_potato/persistence/json.rb
couch_potato-0.5.4 lib/couch_potato/persistence/json.rb
couch_potato-0.5.3 lib/couch_potato/persistence/json.rb
couch_potato-0.5.2 lib/couch_potato/persistence/json.rb
couch_potato-0.5.1 lib/couch_potato/persistence/json.rb
couch_potato-0.5.0 lib/couch_potato/persistence/json.rb