Module: ZendeskAPI::TrackChanges::InstanceMethods

Defined in:
lib/zendesk_api/track_changes.rb

Instance Method Summary collapse

Instance Method Details

#changed?(key = nil) ⇒ Boolean Also known as: dirty?

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/zendesk_api/track_changes.rb', line 74

def changed?(key = nil)
  if key.nil?
    !changes.empty? || any? {|_, v| v.respond_to?(:changed?) && v.changed?}
  else
    changes.key?(key)
  end
end

#changesObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zendesk_api/track_changes.rb', line 62

def changes
  (@changes ||= self.class.superclass.new).tap do |changes|
    each do |k, v|
      if v.respond_to?(:changed?) && v.changed?
        changes[k] = v.changes
      elsif v.is_a?(Array) && v.any? {|val| val.respond_to?(:changed?) && val.changed?}
        changes[k] = v
      end
    end
  end
end

#clearObject



43
44
45
# File 'lib/zendesk_api/track_changes.rb', line 43

def clear
  keys.each{|key| delete key}
end

#clear_changesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zendesk_api/track_changes.rb', line 22

def clear_changes
  each do |k, v|
    if v.respond_to?(:clear_changes)
      v.clear_changes
    elsif v.is_a?(Array)
      v.each do |val|
        if val.respond_to?(:clear_changes)
          val.clear_changes
        end
      end
    end
  end

  changes.clear
end

#delete(key) ⇒ Object



56
57
58
59
60
# File 'lib/zendesk_api/track_changes.rb', line 56

def delete(key)
  super.tap do |value|
    changes[key] = nil
  end
end

#regular_writer(key, value) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/zendesk_api/track_changes.rb', line 47

def regular_writer(key, value)
  if self.has_key?(key) && self[key] == value
    value
  else
    changes[key] = value
    defined?(_store) ? _store(key, value) : super(key, value)
  end
end

#replace(other) ⇒ Object



38
39
40
41
# File 'lib/zendesk_api/track_changes.rb', line 38

def replace(other)
  clear
  merge! other
end