Sha256: 076be93006bb283ec372de9407ec43cb22c87ab5983b6840306d1afc245b1d24

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module LightweightAttributes
  module BaseMethods
    def attributes_before_type_cast
      @attributes
    end

    def read_attribute_before_type_cast(attr_name)
      @attributes[attr_name]
    end

    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

    # AR 5.0
    private def write_attribute_with_type_cast(attr_name, value, _should_type_cast)
      _write_attribute attr_name, value
    end

    #NOTE: Should be true for user posted Time Hash, but who cares?
    def attribute_came_from_user?(_attribute_name)
      false
    end

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

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

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

    def changed_attribute_names_to_save
      changed_attributes.keys
    end

    def has_changes_to_save?
      changed_attributes.any?
    end

    def changed_attributes
      @changed_attributes ||= {}
    end

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

    private def forget_attribute_assignments
      @changed_attributes.each do |k, v|
        @attributes[k] = v
      end
      @changed_attributes.clear
    end

    # AR 5.0
    def store_original_attributes
      forget_attribute_assignments
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lightweight_attributes-0.2.0 lib/lightweight_attributes/base_methods.rb