Sha256: 6568ddc5f05b8dd52bc8a56cedd5bdb79be8f63efb29bc6dfbfdb4e8e22c4113

Contents?: true

Size: 1.63 KB

Versions: 19

Compression:

Stored size: 1.63 KB

Contents

module CouchPotato
  module Persistence
    class DeepTrackedProperty < SimpleProperty

      def initialize(owner_clazz, name, options = {})
        super
        define_accessors deep_accessors_module_for(owner_clazz), name, options
      end

      private

      def deep_accessors_module_for(clazz)
        module_for(clazz, "DeepAccessorMethods")
      end

      def define_accessors(base, name, options)
        base.class_eval do
          define_method "#{name}=" do |value|
            typecasted_value = type_caster.cast(value, options[:type])
            self.instance_variable_set("@#{name}", typecasted_value)
          end

          define_method "#{name}_changed?" do
            if self.class.doc_array_type?(options[:type])
              doc_array_changed?(name)
            elsif self.class.simple_array_type?(options[:type])
              simple_array_changed?(name)
            elsif self.class.doc_type?(options[:type])
              doc_changed?(name)
            else
              super()
            end
          end

          define_method "#{name}_was" do
            @original_deep_values[name] if send("#{name}_changed?")
          end

          define_method "#{name}_change" do
            if !send("#{name}_changed?")
              nil
            elsif self.class.doc_array_type?(options[:type])
              doc_array_change(name)
            elsif self.class.simple_array_type?(options[:type])
              simple_array_change(name)
            elsif self.class.doc_type?(options[:type])
              doc_change(name)
            else
              super()
            end
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
couch_potato-1.17.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.16.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.15.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.14.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.13.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.12.1 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.12.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.11.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.10.1 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.10.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.9.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.7.1 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.7.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.6.5 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.6.4 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.6.3 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.4.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.3.0 lib/couch_potato/persistence/deep_tracked_property.rb
couch_potato-1.2.0 lib/couch_potato/persistence/deep_tracked_property.rb