Module: Friendly::Document::Attributes

Extended by:
Mixin
Included in:
Friendly::Document
Defined in:
lib/friendly/document/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Mixin

included

Instance Method Details

#assign(name, value) ⇒ Object



43
44
45
# File 'lib/friendly/document/attributes.rb', line 43

def assign(name, value)
  send(:"#{name}=", value)
end

#assign_default_valuesObject



39
40
41
# File 'lib/friendly/document/attributes.rb', line 39

def assign_default_values
  self.class.attributes.values.each { |a| a.assign_default_value(self) }
end

#attribute_changed?(attribute) ⇒ Boolean

Has this attribute changed?

Parameters:

  • attribute (Symbol)

    The name of the attribute.

Returns:



68
69
70
# File 'lib/friendly/document/attributes.rb', line 68

def attribute_changed?(attribute)
  changed.include?(attribute)
end

#attribute_was(attribute) ⇒ Object

Get the original value of an attribute that has changed.

Parameters:

  • attribute (Symbol)

    The name of the attribute.



60
61
62
# File 'lib/friendly/document/attributes.rb', line 60

def attribute_was(attribute)
  instance_variable_get(:"@#{attribute}_was")
end

#attributes=(attrs) ⇒ Object



30
31
32
33
# File 'lib/friendly/document/attributes.rb', line 30

def attributes=(attrs)
  assert_no_duplicate_keys(attrs)
  attrs.each { |name, value| assign(name, value) }
end

#changedObject

Which attributes that are being tracked have changed since last reset?



80
81
82
# File 'lib/friendly/document/attributes.rb', line 80

def changed
  @changed ||= Set.new
end

#changed?Boolean

Have any of the attributes that are being tracked changed since last reset?

Returns:



74
75
76
# File 'lib/friendly/document/attributes.rb', line 74

def changed?
  !changed.empty?
end

#initialize(opts = {}) ⇒ Object



25
26
27
28
# File 'lib/friendly/document/attributes.rb', line 25

def initialize(opts = {})
  assign_default_values
  self.attributes = opts
end

#not_changed(attribute) ⇒ Object

Reset the changed-ness of one attribute.



92
93
94
95
# File 'lib/friendly/document/attributes.rb', line 92

def not_changed(attribute)
  instance_variable_set(:"@#{attribute}_was", nil)
  changed.delete(attribute)
end

#reset_changesObject

Reset all the changes to this object.



86
87
88
# File 'lib/friendly/document/attributes.rb', line 86

def reset_changes
  changed.each { |c| not_changed(c) }.clear
end

#saveObject

Override #save to reset changes afterwards



101
102
103
104
# File 'lib/friendly/document/attributes.rb', line 101

def save
  super
  reset_changes
end

#to_hashObject



35
36
37
# File 'lib/friendly/document/attributes.rb', line 35

def to_hash
  Hash[*self.class.attributes.keys.map { |n| [n, send(n)] }.flatten]
end

#will_change(attribute) ⇒ Object

Notify the object that an attribute is about to change.

Parameters:

  • attribute (Symbol)

    The name of the attribute about to change.



51
52
53
54
# File 'lib/friendly/document/attributes.rb', line 51

def will_change(attribute)
  changed << attribute
  instance_variable_set(:"@#{attribute}_was", send(attribute))
end