Sha256: ed3f0c2dc913fc4229309fc82612d702512b42d9cb6b02023b7685262dcc6e15

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

module MongoMapper
  module Plugins
    module Protected
      module ClassMethods
        def attr_protected(*attrs)
          self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
        end

        def protected_attributes
          self.read_inheritable_attribute(:attr_protected)
        end

        def key(*args)
          key = super
          attr_protected key.name.to_sym if key.options[:protected]
          key
        end
      end

      module InstanceMethods
        def assign(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def update_attributes(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def update_attributes!(attrs={})
          super(filter_protected_attrs(attrs))
        end

        def protected_attributes
          self.class.protected_attributes
        end

        protected
          def filter_protected_attrs(attrs)
            return attrs if protected_attributes.blank?
            attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
          end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
mongo_mapper-0.7.3 lib/mongo_mapper/plugins/protected.rb
numon-0.0.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.7.2 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-0.7.1 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.3.8 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.3.5 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.3.4 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.3.3 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.28 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/protected.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/protected.rb