lib/ohm.rb in ohm-2.0.0 vs lib/ohm.rb in ohm-2.0.1

- old
+ new

@@ -609,11 +609,11 @@ end end end # Anytime you filter a set with more than one requirement, you - # internally use a `MultiSet`. `MutiSet` is a bit slower than just + # internally use a `MultiSet`. `MultiSet` is a bit slower than just # a `Set` because it has to `SINTERSTORE` all the keys prior to # retrieving the members, size, etc. # # Example: # @@ -773,11 +773,11 @@ def self.redis defined?(@redis) ? @redis : Ohm.redis end def self.mutex - @mutex ||= Mutex.new + @@mutex ||= Mutex.new end def self.synchronize(&block) mutex.synchronize(&block) end @@ -1072,25 +1072,38 @@ # The bread and butter macro of all models. Basically declares # persisted attributes. All attributes are stored on the Redis # hash. # - # Example: # class User < Ohm::Model # attribute :name # end # - # # It's the same as: + # user = User.new(name: "John") + # user.name + # # => "John" # - # class User < Ohm::Model - # def name - # @attributes[:name] - # end + # user.name = "Jane" + # user.name + # # => "Jane" # - # def name=(name) - # @attributes[:name] = name - # end + # A +lambda+ can be passed as a second parameter to add + # typecasting support to the attribute. + # + # class User < Ohm::Model + # attribute :age, ->(x) { x.to_i } # end + # + # user = User.new(age: 100) + # + # user.age + # # => 100 + # + # user.age.kind_of?(Integer) + # # => true + # + # Check http://rubydoc.info/github/cyx/ohm-contrib#Ohm__DataTypes + # to see more examples about the typecasting feature. # def self.attribute(name, cast = nil) attributes << name unless attributes.include?(name) if cast