# File lib/couchrest/mixins/properties.rb, line 49 def property(name, *options, &block) opts = { } type = options.shift if type.class != Hash opts[:type] = type opts.merge!(options.shift || {}) else opts.update(type) end existing_property = self.properties.find{|p| p.name == name.to_s} if existing_property.nil? || (existing_property.default != opts[:default]) define_property(name, opts, &block) end end
defines the getter for the property (and optional aliases)
# File lib/couchrest/mixins/properties.rb, line 86 def create_property_getter(property) # meth = property.name class_eval " def #{property.name} read_attribute('#{property.name}') end ", __FILE__, __LINE__ + 1 if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase) class_eval " def #{property.name}? value = read_attribute('#{property.name}') !(value.nil? || value == false) end ", __FILE__, __LINE__ end if property.alias class_eval " alias #{property.alias.to_sym} #{property.name.to_sym} ", __FILE__, __LINE__ + 1 end end
defines the setter for the property (and optional aliases)
# File lib/couchrest/mixins/properties.rb, line 111 def create_property_setter(property) property_name = property.name class_eval <<-EOS def #{property_name}=(value) write_attribute('#{property_name}', value) end EOS if property.alias class_eval <<-EOS alias #{property.alias.to_sym}= #{property_name.to_sym}= EOS end end
This is not a thread safe operation, if you have to set new properties at runtime make sure a mutex is used.
# File lib/couchrest/mixins/properties.rb, line 68 def define_property(name, options={}, &block) # check if this property is going to casted type = options.delete(:type) || options.delete(:cast_as) if block_given? type = Class.new(Hash) do include CastedModel end type.class_eval { yield type } type = [type] # inject as an array end property = CouchRest::Property.new(name, type, options) create_property_getter(property) create_property_setter(property) unless property.read_only == true properties << property property end