Sha256: 79cf96532079e16955c54683e15d41f5a29bfb8ac2e978827982a05324baf5d6

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Hari
  class Entity
    module Repository
      extend ActiveSupport::Concern

      def create_or_update
        run_callbacks(:save) { new? ? create : update }.tap do
          @changed_attributes.clear
        end
      end

      alias :save :create_or_update

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

          @id ||= generate_id
          @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
        source = to_json
        @previously_changed = changes
        Hari.redis.set id, source
      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!
          return if args.empty?

          args = args.map { |a| a.to_s.gsub(/^hari\:/, '') }
          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 = {})
          return [] if ids.empty?

          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.5 lib/hari/entity/repository.rb