Module: CouchFoo::AttributeMethods::ClassMethods

Defined in:
lib/couch_foo/attribute_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute_method_suffix(*suffixes) ⇒ Object

Declares a method available for all attributes with the given suffix. Uses method_missing and respond_to? to rewrite the method

#{attr}#{suffix}(*args, &block)

to

attribute#{suffix}(#{attr}, *args, &block)

An attribute#{suffix} instance method must exist and accept at least the attr argument.

For example:

class Person < ActiveRecord::Base
  attribute_method_suffix '_changed?'

  private
    def attribute_changed?(attr)
      ...
    end
end

person = Person.find(1)
person.name_changed?    # => false
person.name = 'Hubert'
person.name_changed?    # => true


43
44
45
46
# File 'lib/couch_foo/attribute_methods.rb', line 43

def attribute_method_suffix(*suffixes)
  attribute_method_suffixes.concat suffixes
  rebuild_attribute_method_regexp
end

#cache_attribute?(attr_name) ⇒ Boolean

Returns true if the provided attribute is being cached.

Returns:



107
108
109
# File 'lib/couch_foo/attribute_methods.rb', line 107

def cache_attribute?(attr_name)
  cached_attributes.include?(attr_name)
end

#cache_attributes(*attribute_names) ⇒ Object

cache_attributes allows you to declare which converted attribute values should be cached. Usually caching only pays off for attributes with expensive conversion methods, like time related columns (e.g. created_at, updated_at).



95
96
97
# File 'lib/couch_foo/attribute_methods.rb', line 95

def cache_attributes(*attribute_names)
  attribute_names.each {|attr| cached_attributes << attr.to_s}
end

#cached_attributesObject

Returns the attributes which are cached. By default time related columns with datatype :datetime, :timestamp, :time, :date are cached.



101
102
103
104
# File 'lib/couch_foo/attribute_methods.rb', line 101

def cached_attributes
  @cached_attributes ||=
    property_types.select{|k,v| attribute_types_cached_by_default.include?(v)}.map{|e| e.first.to_s}.to_set
end

#define_attribute_methodsObject Also known as: define_read_methods

Generates accessors, mutators and query methods for registered properties



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/couch_foo/attribute_methods.rb', line 64

def define_attribute_methods
  return if generated_methods?
  property_names.each do |name|
    unless instance_method_already_implemented?(name)
      define_read_method(name.to_sym)
    end
    unless instance_method_already_implemented?("#{name}=")
      define_write_method(name.to_sym)
    end
    unless instance_method_already_implemented?("#{name}?")
      define_question_method(name)
    end
  end
end

#generated_methodsObject

Contains the names of the generated attribute methods.



55
56
57
# File 'lib/couch_foo/attribute_methods.rb', line 55

def generated_methods #:nodoc:
  @generated_methods ||= Set.new
end

#generated_methods?Boolean

Returns:



59
60
61
# File 'lib/couch_foo/attribute_methods.rb', line 59

def generated_methods?
  !generated_methods.empty?
end

#instance_method_already_implemented?(method_name) ⇒ Boolean

Checks whether the method is defined in the model or any of its subclasses that also derive from Couch Foo. Raises DangerousAttributeError if the method is defined by Couch Foo.

Returns:

Raises:



83
84
85
86
87
88
89
90
# File 'lib/couch_foo/attribute_methods.rb', line 83

def instance_method_already_implemented?(method_name)
  method_name = method_name.to_s
  return true if method_name =~ /^id(=$|\?$|$)/
  @_defined_class_methods         ||= ancestors.first(ancestors.index(CouchFoo::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map(&:to_s).to_set
  @@_defined_couchfoo_methods ||= (CouchFoo::Base.public_instance_methods(false) | CouchFoo::Base.private_instance_methods(false) | CouchFoo::Base.protected_instance_methods(false)).map(&:to_s).to_set
  raise DangerousAttributeError, "#{method_name} is defined by CouchFoo" if @@_defined_couchfoo_methods.include?(method_name)
  @_defined_class_methods.include?(method_name)
end

#match_attribute_method?(method_name) ⇒ Boolean

Returns MatchData if method_name is an attribute method.

Returns:



49
50
51
52
# File 'lib/couch_foo/attribute_methods.rb', line 49

def match_attribute_method?(method_name)
  rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp
  @@attribute_method_regexp.match(method_name)
end