Module: Gourami::Extensions::Changes

Defined in:
lib/gourami/extensions/changes.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

WATCH_CHANGES_VALID_RETURN_VALUES =
[true, false].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



41
42
43
# File 'lib/gourami/extensions/changes.rb', line 41

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#changes?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gourami/extensions/changes.rb', line 45

def changes?(attribute_name)
  attribute_name_sym = attribute_name.to_sym
  changed_attributes.fetch(attribute_name_sym) do
    options = self.class.attributes.fetch(attribute_name_sym, {})
    watch_changes = options.fetch(:watch_changes, false)

    return false if watch_changes

    raise NotWatchingChangesError, "`#{attribute_name}` is not being watched for changes. " \
      "Try `attribute(:#{attribute_name}, :watch_changes => true)`"
  end
end

#did_change(attribute_name, changed = true) ⇒ Object



58
59
60
# File 'lib/gourami/extensions/changes.rb', line 58

def did_change(attribute_name, changed = true)
  changed_attributes[attribute_name.to_sym] = !!changed
end