Recipe 8.6. Validating and Modifying Attribute ValuesProblemYou want to let outside code set your objects' SolutionDefine your own setter method for each instance variable you want to control. The setter method for an instance variable quantity would be called quantity=. When a user issues a statement like object.quantity = 10, the method object#quantity= is called with the argument 10. It's up to the quantity= method to decide whether the instance variable quantity should actually take the value 10. A setter method is free to raise an ArgumentException if it's passed an invalid value. It may also modify the provided value, massaging it into the canonical form used by the class. If it can get an acceptable value, its last act should be to modify the instance variable. I'll define a class that keeps track of peoples' first and last names. It uses setter
I've written the Name class so that the rules are enforced both in the constructor and after the object has been created:
DiscussionRuby never lets one object Similarly, when you set a new value for object.my_var, you're actually passing that value into a setter method called my_var=. That method might go ahead and stick your new value into the instance variable my_var. It might accept your value, but silently clean it up, convert it to another format, or otherwise modify it. It might be picky and reject your value altogether by raising an ArgumentError. When you're defining a class, you can have Ruby generate a setter method for one of your instance variables by calling Module#atttr_writer or Module#attr_accessor on the symbol for that variable. This saves you from having to write code, but the default setter method lets anyone set the instance variable to any value at all:
A lot of the time, this kind of informality is just fine. But sometimes you don't trust the data coming in through the setter methods. That's when you can define your own methods to stop bad data before it infects your objects. Within a class, you have direct access to the instance variables. You can simply assign to an instance variable and the setter method won't be triggered. If you do want to trigger the setter method, you'll have to call it explicitly. Note how, in the Name#initialize method above, I call the first= and last= methods instead of assigning to @first and @last. This makes sure the validation code gets run for the initial values of every Name object. I can't just say first = first, because first is a variable name in that method. See Also
|
Tuesday, December 15, 2009
Recipe 8.6. Validating and Modifying Attribute Values
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment