Module: Zm::Model::AttributeChangeObserver::ClassMethods

Defined in:
lib/zm/modules/zm_model.rb

Instance Method Summary collapse

Instance Method Details

#define_changed_attribute(attr_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zm/modules/zm_model.rb', line 15

def define_changed_attribute(attr_name)
  attr_reader attr_name

  generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{attr_name}_changed
      @#{attr_name}_changed ||= false
    end

    def #{attr_name}_changed?
      #{attr_name}_changed
    end

    def #{attr_name}=(value)
      return @#{attr_name} if value == @#{attr_name}
      @#{attr_name}_changed = true
      @#{attr_name} = value
    end
  RUBY
end

#define_changed_attributes(*attr_names) ⇒ Object



11
12
13
# File 'lib/zm/modules/zm_model.rb', line 11

def define_changed_attributes(*attr_names)
  attr_names.flatten.each { |attr_name| define_changed_attribute(attr_name) }
end