lotus-model 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b91d99745af6c3080d3e83ff84b369517df2dd77
4
- data.tar.gz: cda6f8e7128dbb6db125054d0092a6f4fc83b413
3
+ metadata.gz: acf5bf11caa86301e6e5335064ca5c5454df5b86
4
+ data.tar.gz: 0782b930d37c6236c6c676017ae34caf68b1c2d2
5
5
  SHA512:
6
- metadata.gz: 1ad445fbbf969ebd9dccc1997bd92a10ccc3e4bd3f679d8691b94f9c66940d72afda02d3fcad063001a1905fcae862b5eeb2b0768bc0d85016f10fc3e5e1ef5e
7
- data.tar.gz: ed8fafcd2491178a6b9646e1a28c07110208c7d8d1dfa15420aeca8482fccc38952058b95d3f86c28e0bc8066a1a9503c11b487348853b442a2bd491e9cf8314
6
+ metadata.gz: 0299bb1a135bb8f43804f44968126425fde51be8377b035f2635312d49cc42bc2d4ab62374967f3a574007ae5c6f16e43ba5867372c495c05113fe4420126efa
7
+ data.tar.gz: a69e4eddfb3b10999487d08bf754f6bf413ac487e80ecb1b3d9b5a34f19856e9da9633854a8c987afa5a5f3d74589835dddde4f5a0265e6d0179ff1af72f33a9
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Lotus::Model
2
2
  A persistence layer for Lotus
3
3
 
4
+ ## v0.3.2 - 2015-05-22
5
+ ### Added
6
+ - [Dmitry Tymchuk & Luca Guidi] Fix for dirty tracking of attributes changed in place (eg. `book.tags << 'non-fiction'`)
7
+
4
8
  ## v0.3.1 - 2015-05-15
5
9
  ### Added
6
10
  - [Dmitry Tymchuk] Dirty tracking for entities (via `Lotus::Entity::DirtyTracking` module to include)
data/lib/lotus/entity.rb CHANGED
@@ -154,7 +154,7 @@ module Lotus
154
154
 
155
155
  # Define setter/getter methods for attributes.
156
156
  #
157
- # @params attr [Symbol] an attribute name
157
+ # @param attr [Symbol] an attribute name
158
158
  #
159
159
  # @since 0.3.1
160
160
  # @api private
@@ -3,57 +3,25 @@ module Lotus
3
3
  # Dirty tracking for entities
4
4
  #
5
5
  # @since 0.3.1
6
+ #
7
+ # @example Dirty tracking
8
+ # require 'lotus/model'
9
+ #
10
+ # class User
11
+ # include Lotus::Entity
12
+ # include Lotus::Entity::DirtyTracking
13
+ #
14
+ # attributes :name
15
+ # end
16
+ #
17
+ # article = Article.new(title: 'Generation P')
18
+ # article.changed? # => false
19
+ #
20
+ # article.title = 'Master and Margarita'
21
+ # article.changed? # => true
22
+ #
23
+ # article.changed_attributes # => {:title => "Generation P"}
6
24
  module DirtyTracking
7
- # Override setters for attributes to support dirty tracking
8
- #
9
- # @since 0.3.1
10
- #
11
- # @example Dirty tracking
12
- # require 'lotus/model'
13
- #
14
- # class User
15
- # include Lotus::Entity
16
- # include Lotus::Entity::DirtyTracking
17
- #
18
- # attributes :name
19
- # end
20
- #
21
- # article = Article.new(title: 'Generation P')
22
- # article.changed? # => false
23
- #
24
- # article.title = 'Master and Margarita'
25
- # article.changed? # => true
26
- #
27
- # article.changed_attributes # => {:title => "Generation P"}
28
- def self.included(base)
29
- base.class_eval do
30
- extend ClassMethods
31
- end
32
- end
33
-
34
- module ClassMethods
35
- # Override attribute accessors function.
36
- # Create setter methods with attribute values checking.
37
- # If the new value or a changed, added to @changed_attributes.
38
- #
39
- # @params attr [Symbol] an attribute name
40
- #
41
- # @since 0.3.1
42
- # @api private
43
- #
44
- # @see Lotus::Entity::ClassMethods#define_attr_accessor
45
- def define_attr_accessor(attr)
46
- attr_reader(attr)
47
-
48
- class_eval %{
49
- def #{ attr }=(value)
50
- _attribute_changed(:#{ attr }, @#{ attr }, value)
51
- @#{ attr } = value
52
- end
53
- }
54
- end
55
- end
56
-
57
25
  # Override initialize process.
58
26
  #
59
27
  # @param attributes [Hash] a set of attribute names and values
@@ -62,9 +30,8 @@ module Lotus
62
30
  #
63
31
  # @see Lotus::Entity#initialize
64
32
  def initialize(attributes = {})
65
- _clear_changes_information
66
33
  super
67
- _clear_changes_information
34
+ @_initial_state = Utils::Hash.new(to_h).deep_dup
68
35
  end
69
36
 
70
37
  # Getter for hash of changed attributes.
@@ -78,7 +45,7 @@ module Lotus
78
45
  # @example
79
46
  # require 'lotus/model'
80
47
  #
81
- # class User
48
+ # class Article
82
49
  # include Lotus::Entity
83
50
  # include Lotus::Entity::DirtyTracking
84
51
  #
@@ -91,7 +58,7 @@ module Lotus
91
58
  # article.title = 'Master and Margarita'
92
59
  # article.changed_attributes # => {:title => "The crime and punishment"}
93
60
  def changed_attributes
94
- @changed_attributes.dup
61
+ Hash[@_initial_state.to_a - to_h.to_a]
95
62
  end
96
63
 
97
64
  # Checks if the attributes were changed
@@ -100,26 +67,7 @@ module Lotus
100
67
  #
101
68
  # @since 0.3.1
102
69
  def changed?
103
- @changed_attributes.size > 0
104
- end
105
-
106
- private
107
-
108
- # Set changed attributes in Hash with their old values.
109
- #
110
- # @params attrs [Symbol] an attribute name
111
- #
112
- # @since 0.3.1
113
- # @api private
114
- def _attribute_changed(attr, current_value, new_value)
115
- @changed_attributes[attr] = new_value if current_value != new_value
116
- end
117
-
118
- # Clear all information about dirty data
119
- #
120
- # @since 0.3.1
121
- def _clear_changes_information
122
- @changed_attributes = {}
70
+ changed_attributes.any?
123
71
  end
124
72
  end
125
73
  end
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.3.1'.freeze
6
+ VERSION = '0.3.2'.freeze
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-15 00:00:00.000000000 Z
12
+ date: 2015-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lotus-utils