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)


20
21
22
# File 'lib/lightweight_attributes/base_methods.rb', line 20

def attribute_came_from_user?(_attribute_name)
  false
end

#attribute_in_database(attr_name) ⇒ Object



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

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)


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

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

#changed_attribute_names_to_saveObject



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

def changed_attribute_names_to_save
  changed_attributes.keys
end

#changed_attributesObject



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

def changed_attributes
  @changed_attributes ||= {}
end

#changes_to_saveObject



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

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)


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

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

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

Returns:

  • (Boolean)


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

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