Module: Lotus::Entity::DirtyTracking

Defined in:
lib/lotus/entity/dirty_tracking.rb

Overview

Dirty tracking for entities

Examples:

Dirty tracking

require 'lotus/model'

class User
  include Lotus::Entity
  include Lotus::Entity::DirtyTracking

  attributes :name
end

article = Article.new(title: 'Generation P')
article.changed? # => false

article.title = 'Master and Margarita'
article.changed? # => true

article.changed_attributes # => {:title => "Generation P"}

Since:

  • 0.3.1

Instance Method Summary collapse

Instance Method Details

#changed?TrueClass, FalseClass

Checks if the attributes were changed

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.3.1



69
70
71
# File 'lib/lotus/entity/dirty_tracking.rb', line 69

def changed?
  changed_attributes.any?
end

#changed_attributes::Hash

Getter for hash of changed attributes. Return empty hash, if there is no changes Getter for hash of changed attributes. Value in it is the previous one.

Examples:

require 'lotus/model'

class Article
  include Lotus::Entity
  include Lotus::Entity::DirtyTracking

  attributes :title
end

article = Article.new(title: 'The crime and punishment')
article.changed_attributes # => {}

article.title = 'Master and Margarita'
article.changed_attributes # => {:title => "The crime and punishment"}

Returns:

  • (::Hash)

    the changed attributes

Since:

  • 0.3.1



60
61
62
# File 'lib/lotus/entity/dirty_tracking.rb', line 60

def changed_attributes
  Hash[@_initial_state.to_a - to_h.to_a]
end

#initialize(attributes = {}) ⇒ Object

Override initialize process.

Parameters:

  • attributes (Hash) (defaults to: {})

    a set of attribute names and values

See Also:

Since:

  • 0.3.1



32
33
34
35
# File 'lib/lotus/entity/dirty_tracking.rb', line 32

def initialize(attributes = {})
  super
  @_initial_state = Utils::Hash.new(to_h).deep_dup
end