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



46
47
48
# File 'lib/friendly/document/attributes.rb', line 46

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

#assign_default_valuesObject



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

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:



71
72
73
# File 'lib/friendly/document/attributes.rb', line 71

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.



63
64
65
# File 'lib/friendly/document/attributes.rb', line 63

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?



83
84
85
# File 'lib/friendly/document/attributes.rb', line 83

def changed
  @changed ||= Set.new
end

#changed?Boolean

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

Returns:



77
78
79
# File 'lib/friendly/document/attributes.rb', line 77

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.



95
96
97
98
# File 'lib/friendly/document/attributes.rb', line 95

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

#reset_changesObject

Reset all the changes to this object.



89
90
91
# File 'lib/friendly/document/attributes.rb', line 89

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

#saveObject

Override #save to reset changes afterwards



104
105
106
107
# File 'lib/friendly/document/attributes.rb', line 104

def save
  super
  reset_changes
end

#to_hashObject



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

def to_hash
  self.class.attributes.keys.inject({}) do |memo, key|
    memo[key] = send(key)
    memo
  end
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.



54
55
56
57
# File 'lib/friendly/document/attributes.rb', line 54

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