Sha256: e0fabf236e62a1069b0f5646ac12d655c80ea73fbcfffbb0ca8e0649a3bc857f

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Hari
  class Entity
    module Repository
      extend ActiveSupport::Concern

      def create_or_update
        run_callbacks(:save) { new? ? create : update }
      end

      alias save create_or_update

      def create
        run_callbacks :create do
          fail Hari::ValidationsFailed, self unless valid?

          @id ||= generate_id
          self.created_at = Time.now
          self.updated_at = Time.now
          persist
        end

        self
      end

      def update
        run_callbacks :update do
          fail Hari::ValidationsFailed, self unless valid?

          self.updated_at = Time.now.utc.iso8601
          persist
        end

        self
      end

      def persist
        Hari.redis.set id, to_json
      end

      def delete
        run_callbacks :destroy do
          Hari.redis.del id
          @destroyed = true
        end

        self
      end

      alias destroy delete

      module ClassMethods

        def create(attrs = {})
          new(attrs).save
        end

        def find(*args)
          options = args.extract_options!
          args.flatten!
          args.one? ? find_one(args[0], options) : find_many(args, options)
        end

        def find_one(id, options = {})
          from_json Hari.redis.get(id)
        end

        def find_many(ids, options = {})
          Hari.redis.mget(ids).map &method(:from_json)
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.1 lib/hari/entity/repository.rb