Module: CouchFoo::AttributeMethods
- Defined in:
- lib/couch_foo/attribute_methods.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEFAULT_SUFFIXES =
%w(= ? _before_type_cast)
- ATTRIBUTE_TYPES_CACHED_BY_DEFAULT =
[Time, DateTime, Date]
- JSON_DATETIME_FORMAT =
"%Y/%m/%d %H:%M:%S +0000"
Class Method Summary collapse
Instance Method Summary collapse
-
#method_missing(method_id, *args, &block) ⇒ Object
Allows access to the object attributes, which are held in the
@attributes
hash, as though they were first-class methods. - #query_attribute(attr_name) ⇒ Object
-
#read_attribute(attr_name) ⇒ Object
Returns the value of the attribute identified by
attr_name
after it has been typecast (for example, “2004-12-12” in a data type is cast to a date object, like Date.new(2004, 12, 12)). - #read_attribute_before_type_cast(attr_name) ⇒ Object
- #respond_to?(method, include_priv = false) ⇒ Boolean
-
#respond_to_without_attributes? ⇒ Object
A Person object with a name attribute can ask
person.respond_to?("name")
,person.respond_to?("name=")
, andperson.respond_to?("name?")
which will all returntrue
. -
#write_attribute(attr_name, value) ⇒ Object
Updates the attribute identified by
attr_name
with the specifiedvalue
.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args, &block) ⇒ Object
Allows access to the object attributes, which are held in the @attributes
hash, as though they were first-class methods. So a Person class with a name attribute can use Person#name and Person#name= and never directly use the attributes hash – except for multiple assigns with CouchFoo#attributes=. A Milestone class can also ask Milestone#completed? to test that the completed attribute is not nil
or 0.
It’s also possible to instantiate related objects, so a Client class belonging to the clients table with a master_id
foreign key can instantiate master through Client#master.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/couch_foo/attribute_methods.rb', line 165 def method_missing(method_id, *args, &block) method_name = method_id.to_s # Make sure methods are generated if !self.class.generated_methods? self.class.define_attribute_methods if self.class.generated_methods.include?(method_name) return self.send(method_id, *args, &block) end end # Unchangeable properties are called directly, not through generated methods if self.class.unchangeable_property_names.include?(method_id) send(method_id, *args, &block) elsif md = self.class.match_attribute_method?(method_name) attribute_name, method_type = md.pre_match, md.to_s if @attributes.include?(attribute_name) __send__("attribute#{method_type}", attribute_name, *args, &block) else super end elsif attributes.include?(method_name) read_attribute(method_name) else super end end |
Class Method Details
.included(base) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/couch_foo/attribute_methods.rb', line 7 def self.included(base) base.extend ClassMethods base.attribute_method_suffix(*DEFAULT_SUFFIXES) base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false base.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT end |
Instance Method Details
#query_attribute(attr_name) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/couch_foo/attribute_methods.rb', line 211 def query_attribute(attr_name) unless value = read_attribute(attr_name) false else column_type = type_for_property(attr_name) if column_type.nil? if Numeric === value || value !~ /[^0-9]/ !value.to_i.zero? else !value.blank? end elsif column_type == Integer || column_type == Float !value.zero? else !value.blank? end end end |
#read_attribute(attr_name) ⇒ Object
Returns the value of the attribute identified by attr_name
after it has been typecast (for example, “2004-12-12” in a data type is cast to a date object, like Date.new(2004, 12, 12)).
195 196 197 |
# File 'lib/couch_foo/attribute_methods.rb', line 195 def read_attribute(attr_name) convert_to_type(@attributes[attr_name.to_s], type_for_property(attr_name.to_sym)) end |
#read_attribute_before_type_cast(attr_name) ⇒ Object
199 200 201 |
# File 'lib/couch_foo/attribute_methods.rb', line 199 def read_attribute_before_type_cast(attr_name) @attributes[attr_name] end |
#respond_to?(method, include_priv = false) ⇒ Boolean
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/couch_foo/attribute_methods.rb', line 234 def respond_to?(method, include_priv = false) method_name = method.to_s if super return true elsif !self.class.generated_methods? self.class.define_attribute_methods if self.class.generated_methods.include?(method_name) return true end end if @attributes.nil? return super elsif @attributes.include?(method_name) return true elsif md = self.class.match_attribute_method?(method_name) return true if @attributes.include?(md.pre_match) end super end |
#respond_to_without_attributes? ⇒ Object
A Person object with a name attribute can ask person.respond_to?("name")
, person.respond_to?("name=")
, and person.respond_to?("name?")
which will all return true
.
233 |
# File 'lib/couch_foo/attribute_methods.rb', line 233 alias :respond_to_without_attributes? :respond_to? |
#write_attribute(attr_name, value) ⇒ Object
Updates the attribute identified by attr_name
with the specified value
. Empty strings for fixnum and float types are turned into nil
.
205 206 207 208 209 |
# File 'lib/couch_foo/attribute_methods.rb', line 205 def write_attribute(attr_name, value) attr_name = attr_name.to_s @attributes_cache.delete(attr_name) @attributes[attr_name] = convert_to_json(value, type_for_property(attr_name.to_sym)) end |