Module: LightweightAttributes::BaseMethods

Defined in:
lib/lightweight_attributes/base_methods.rb

Instance Method Summary collapse

Instance Method Details

#_write_attribute(attr_name, value) ⇒ Object



13
14
15
16
17
# File 'lib/lightweight_attributes/base_methods.rb', line 13

def _write_attribute(attr_name, value)
  was = @attributes[attr_name]
  changed_attributes[attr_name] = was unless changed_attributes.key? attr_name
  @attributes[attr_name] = value
end

#attribute_came_from_user?(_attribute_name) ⇒ Boolean

NOTE: Should be true for user posted Time Hash, but who cares?

Returns:

  • (Boolean)


25
26
27
# File 'lib/lightweight_attributes/base_methods.rb', line 25

def attribute_came_from_user?(_attribute_name)
  false
end

#attribute_in_database(attr_name) ⇒ Object



33
34
35
# File 'lib/lightweight_attributes/base_methods.rb', line 33

def attribute_in_database(attr_name)
  changed_attributes.key?(attr_name) ? changed_attributes[attr_name] : @attributes[attr_name]
end

#attributes_before_type_castObject



5
6
7
# File 'lib/lightweight_attributes/base_methods.rb', line 5

def attributes_before_type_cast
  @attributes
end

#changed?(attr_name, **_options) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lightweight_attributes/base_methods.rb', line 53

def changed?(attr_name, **_options)
  changed_attributes.key? attr_name
end

#changed_attribute_names_to_saveObject



41
42
43
# File 'lib/lightweight_attributes/base_methods.rb', line 41

def changed_attribute_names_to_save
  changed_attributes.keys
end

#changed_attributesObject



49
50
51
# File 'lib/lightweight_attributes/base_methods.rb', line 49

def changed_attributes
  @changed_attributes ||= {}
end

#changes_to_saveObject



37
38
39
# File 'lib/lightweight_attributes/base_methods.rb', line 37

def changes_to_save
  changed_attributes.each_with_object({}) {|(k, v), h| h[k] = [v, @attributes[k]]}
end

#has_changes_to_save?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/lightweight_attributes/base_methods.rb', line 45

def has_changes_to_save?
  changed_attributes.any?
end

#read_attribute_before_type_cast(attr_name) ⇒ Object



9
10
11
# File 'lib/lightweight_attributes/base_methods.rb', line 9

def read_attribute_before_type_cast(attr_name)
  @attributes[attr_name]
end

#store_original_attributesObject

AR 5.0



65
66
67
# File 'lib/lightweight_attributes/base_methods.rb', line 65

def store_original_attributes
  forget_attribute_assignments
end

#will_save_change_to_attribute?(attr_name, **options) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/lightweight_attributes/base_methods.rb', line 29

def will_save_change_to_attribute?(attr_name, **options)
  changed_attributes.key? attr_name
end