MongoMapper 的只读属性/键

发布于 2025-01-02 17:56:22 字数 854 浏览 1 评论 0 原文

我想知道是否有办法使属性键只读。这意味着它只能在创建对象时分配

UPDATE:我希望能够使用 update_attributes 之类的东西,并且确保该方法只会更新可以覆盖的密钥。例如,如果我有

class User 
    include MongoMapper::Document

    key :firstName, String, :required => true
    key :lastName,  String, :required => true
    key :username,  String, :required => true, :unique => true, :readonly => true
    key :password,  String, :required => true

end

(只读验证是伪代码,我希望存在类似的东西)

那么我希望以下代码会引发错误或失败

user = User.find_by_username("foo")
user.update_attributes({:username => "bar"})
puts "You cannot change the username" unless user.valid?

我也想要这样的东西,但这是一个单独的东西

user.update_attributes({:unwantedKey => "fail!"})
puts "You cannot add keys that are not in the User scheme" unless user.valid?

I would like to know if there is anyway to make an attribute key readonly. Meaning that it can only be assigned when the object is being created

UPDATE: I want to be able to use something like update_attributes, and be safe that that method will only update the key that can be overwritten. For example if I have

class User 
    include MongoMapper::Document

    key :firstName, String, :required => true
    key :lastName,  String, :required => true
    key :username,  String, :required => true, :unique => true, :readonly => true
    key :password,  String, :required => true

end

(The readonly validation is pseudo-code, I would like something like that to exist)

Then I would expect the following code to raise an error or fail

user = User.find_by_username("foo")
user.update_attributes({:username => "bar"})
puts "You cannot change the username" unless user.valid?

I also would like something like this, but is a separate thing

user.update_attributes({:unwantedKey => "fail!"})
puts "You cannot add keys that are not in the User scheme" unless user.valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2025-01-09 17:56:22

我会重新审视您的要求,您需要通过验证来完成此操作,而不是使用自定义控制器过滤或 attr_accessible 来控制 可访问性

如果验证确实是正确的解决方案,请自行推出三个建议是个好主意,这里有一些身份映射安全代码来检查数据库:

validate :username_unchanged, :only_existing_keys, :on => :update

def db_version
  # drop to the driver to get around the identity map
  # (identity map is off by default)
  collection.find_one(:_id => self.id)
end

def username_unchanged
  unless username == db_version['username']
    errors.add(:username, 'cannot be changed')
  end
end

def only_existing_keys
  extra_keys = to_mongo.keys - db_version.keys
  unless extra_keys.size == 0
    errors.add(:base, 'You cannot add keys to the schema')
  end
end

但是要小心! MongoMapper 不存储值为 nil 的键。这将破坏上面的 only_existing_keys 方法,因此您可能必须在某处存储有效密钥的数组。

希望这是一个足够的起点。

I would revisit your requirement that you need to do this with validations rather than using custom controller filtering or attr_accessible to control accessibilty.

If validations really are the correct solution, rolling your own like three suggests is a good idea, here's some identity-map-safe code to check against the database:

validate :username_unchanged, :only_existing_keys, :on => :update

def db_version
  # drop to the driver to get around the identity map
  # (identity map is off by default)
  collection.find_one(:_id => self.id)
end

def username_unchanged
  unless username == db_version['username']
    errors.add(:username, 'cannot be changed')
  end
end

def only_existing_keys
  extra_keys = to_mongo.keys - db_version.keys
  unless extra_keys.size == 0
    errors.add(:base, 'You cannot add keys to the schema')
  end
end

But watch out! MongoMapper does not store keys whose value is nil. That's going to foil the only_existing_keys method above, so you may have to store an array of valid keys somewhere.

Hopefully that's a sufficient starting point.

记忆で 2025-01-09 17:56:22

您可以引入这样的自定义验证:

before_update :check_username
validate :read_only

def check_username
  @temp_username = self.username
end

def read_only
  false if self.username != self.temp_username
end

不确定这是否有效,但您有回调和验证,并且可以使用两者来确保没有任何更改。

http://mongomapper.com/documentation/plugins/callbacks.html
http://mongomapper.com/documentation/plugins/validations.html

you can introduce a custom validation like this:

before_update :check_username
validate :read_only

def check_username
  @temp_username = self.username
end

def read_only
  false if self.username != self.temp_username
end

not sure if this will work but you have callbacks and validation and you can use both to make sure nothing is changed.

http://mongomapper.com/documentation/plugins/callbacks.html
http://mongomapper.com/documentation/plugins/validations.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文