Sha256: 3f4402bbd38c37cde63efe47ba1421c4ae6dbf2bd5d293e746f168934cbd838e

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

require 'ripple/associations/proxy'
require 'ripple/associations/many'

module Ripple
  module Associations
    class ManyStoredKeyProxy < Proxy
      include Many

      def count
        keys.size
      end

      def <<(value)
        @reflection.verify_type!([value], @owner)

        raise "Unable to append if the document isn't first saved." if value.new_record?
        load_target
        @target << value
        keys << value.key

        self
      end

      def replace(value)
        @reflection.verify_type!(value, @owner)

        reset_owner_keys
        value.each { |doc| self << doc }
        @target = value
        loaded
      end

      def delete(value)
        keys.delete(value.key)
        self
      end

      def keys
        if @owner.send(keys_name).nil?
          reset_owner_keys
        end

        @owner.send(keys_name)
      end

      def reset
        super
        self.owner_keys = @owner.robject.data ? @owner.robject.data[keys_name] : []
      end

      def include?(document)
        return false unless document.respond_to?(:robject)
        return false unless document.robject.bucket.name == @reflection.bucket_name
        keys.include?(document.key)
      end

      def reset_owner_keys
        self.owner_keys = []
      end


      protected
      def find_target
        klass.find(keys.to_a)
      end

      def keys_name
        "#{@reflection.name.to_s.singularize}_keys"
      end

      def owner_keys=(new_keys)
        @owner.send("#{keys_name}=", @owner.class.properties[keys_name].type.new(new_keys))
      end
    end
  end
end

Version data entries

6 entries across 5 versions & 4 rubygems

Version Path
wyngle-ripple-0.1.0 lib/ripple/associations/many_stored_key_proxy.rb
better-ripple-1.0.0 lib/ripple/associations/many_stored_key_proxy.rb
ripple-1.0.0.beta2 lib/ripple/associations/#many_stored_key_proxy.rb#
ripple-1.0.0.beta2 lib/ripple/associations/many_stored_key_proxy.rb
seomoz-ripple-1.0.0.pre lib/ripple/associations/many_stored_key_proxy.rb
ripple-1.0.0.beta lib/ripple/associations/many_stored_key_proxy.rb