Sha256: 6a0061199feca2def92769c9b9f7ee471b10c47d229f1e8e9c8c7caf6a2a4891

Contents?: true

Size: 1.2 KB

Versions: 11

Compression:

Stored size: 1.2 KB

Contents

module MongoMapper
  module Plugins
    module Keys
      module Static
        class MissingKeyError < StandardError; end

        extend ActiveSupport::Concern

        module ClassMethods
          attr_writer :static_keys

          def static_keys
            @static_keys || false
          end
        end

        def read_key(name)
          if !self.class.static_keys || self.class.key?(name)
            super
          else
            raise MissingKeyError, "Tried to read the key #{name.inspect}, but no key was defined. Either define key :#{name} or set self.static_keys = false"
          end
        end

      private

        def write_key(name, value)
          if !self.class.static_keys || self.class.key?(name)
            super
          else
            raise MissingKeyError, "Tried to write the key #{name.inspect}, but no key was defined. Either define key :#{name} or set self.static_keys = false"
          end
        end

        def load_from_database(attrs, with_cast = false)
          return super if !self.class.static_keys || !attrs.respond_to?(:each)

          attrs = attrs.select { |key, _| self.class.key?(key) }

          super(attrs, with_cast)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mongo_mapper-0.17.0 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/keys/static.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/keys/static.rb