vidibus-versioning 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2011 Andre Pankratz
1
+ MIT License
2
+
3
+ Copyright (c) 20111-2013 Andre Pankratz
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -66,7 +66,7 @@ article.version(:next) # returns the next version of the article
66
66
  article.version(:new) # returns a new version of the article
67
67
  ```
68
68
 
69
- To apply a version on the current article itself, call `version` with a bang!:
69
+ To apply a version on the current article itself (without persisting it), call `version` with a bang!:
70
70
 
71
71
  ```ruby
72
72
  article.version!(3) # applies version 3 to the article and returns nil
data/Rakefile CHANGED
@@ -1,25 +1,17 @@
1
- require "bundler"
2
- require "rdoc/task"
3
- require "rspec"
4
- require "rspec/core/rake_task"
1
+ require 'bundler'
2
+ require 'rdoc/task'
3
+ require 'rspec'
4
+ require 'rspec/core/rake_task'
5
5
 
6
6
  Bundler::GemHelper.install_tasks
7
7
 
8
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
9
- require "vidibus/versioning/_version"
10
-
11
- RSpec::Core::RakeTask.new(:rcov) do |t|
12
- t.pattern = "spec/**/*_spec.rb"
13
- t.rcov = true
14
- t.rcov_opts = ["--exclude", "^spec,/gems/"]
15
- end
8
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
9
+ require 'vidibus/versioning/_version'
16
10
 
17
11
  Rake::RDocTask.new do |rdoc|
18
- rdoc.rdoc_dir = "rdoc"
19
- rdoc.title = "vidibus-sysinfo #{Vidibus::Versioning::VERSION}"
20
- rdoc.rdoc_files.include("README*")
21
- rdoc.rdoc_files.include("lib/**/*.rb")
22
- rdoc.options << "--charset=utf-8"
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'vidibus-sysinfo #{Vidibus::Versioning::VERSION}'
14
+ rdoc.rdoc_files.include('README*')
15
+ rdoc.rdoc_files.include('lib/**/*.rb')
16
+ rdoc.options << '--charset=utf-8'
23
17
  end
24
-
25
- task :default => :rcov
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Versioning
3
- VERSION = "0.1.2"
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
@@ -7,7 +7,7 @@ module Vidibus
7
7
  include ::Mongoid::Timestamps
8
8
  include Vidibus::Uuid::Mongoid
9
9
 
10
- has_many :versions, :as => :versioned, :class_name => "Vidibus::Versioning::Version", :dependent => :destroy
10
+ has_many :versions, :as => :versioned, :class_name => 'Vidibus::Versioning::Version', :dependent => :destroy
11
11
 
12
12
  field :version_number, :type => Integer, :default => 1
13
13
  field :version_updated_at, :type => Time
@@ -67,9 +67,15 @@ module Vidibus
67
67
  # Applies attributes of wanted version on self.
68
68
  # Stores current attributes in a new version.
69
69
  def migrate!(number = nil)
70
- raise(MigrationError, "no version given") unless number or version_cache.wanted_version_number
71
- version!(number) if number and number != version_cache.wanted_version_number
72
- raise(MigrationError, "cannot migrate to current version") if version_cache.self_version
70
+ unless number || version_cache.wanted_version_number
71
+ raise(MigrationError, 'no version given')
72
+ end
73
+ if number && number != version_cache.wanted_version_number
74
+ version!(number)
75
+ end
76
+ if version_cache.self_version
77
+ raise(MigrationError, 'cannot migrate to current version')
78
+ end
73
79
 
74
80
  set_original_version_obj
75
81
 
@@ -99,7 +105,9 @@ module Vidibus
99
105
 
100
106
  # Raises a validation error if saving fails.
101
107
  def save!(*args)
102
- raise(::Mongoid::Errors::Validations, self) unless save(*args)
108
+ unless save(*args)
109
+ raise(::Mongoid::Errors::Validations, self)
110
+ end
103
111
  end
104
112
 
105
113
  def delete
@@ -162,10 +170,17 @@ module Vidibus
162
170
  # TODO: Setting the following line will cause DelayedJob to loop endlessly. The same should happen if an embedded document is defined as versioned_attribute!
163
171
  # original_attributes.merge(version_obj.versioned_attributes).merge(version_cache.wanted_attributes.stringify_keys!)
164
172
 
165
- a = Hash[*self.class.fields.keys.zip([]).flatten]. # ensure nil fields are included as well
166
- merge(version_obj.versioned_attributes) # add version's attributes
167
- filter_versioned_attributes(a). # take versioned attributes only
168
- merge(version_cache.wanted_attributes.stringify_keys!) # add options provided with #version call
173
+ # ensure nil fields are included as well
174
+ attributes = Hash[*self.class.fields.keys.zip([]).flatten]
175
+
176
+ # add version's attributes
177
+ attributes = attributes.merge(version_obj.versioned_attributes)
178
+
179
+ # take versioned attributes only
180
+ filtered = filter_versioned_attributes(attributes)
181
+
182
+ # add options provided with #version call
183
+ filtered.merge(version_cache.wanted_attributes.stringify_keys!)
169
184
  end
170
185
 
171
186
  # Returns versioned attributes from input by
@@ -187,14 +202,16 @@ module Vidibus
187
202
  wanted_version_number = args.first
188
203
 
189
204
  version_cache.wanted_version_number = case wanted_version_number
190
- when :new then new_version_number
191
- when :next then version_number + 1
192
- when :previous then version_number - 1
205
+ when :new, 'new' then new_version_number
206
+ when :next, 'next' then version_number + 1
207
+ when :previous, 'previous' then version_number - 1
193
208
  else
194
209
  wanted_version_number.to_i
195
210
  end
196
211
 
197
- version_cache.wanted_version_number = 1 if version_cache.wanted_version_number < 1
212
+ if version_cache.wanted_version_number < 1
213
+ version_cache.wanted_version_number = 1
214
+ end
198
215
  end
199
216
 
200
217
  # Finds or builds a version object containing the record's current attributes.
@@ -216,18 +233,20 @@ module Vidibus
216
233
  def version_obj
217
234
  version_cache.version_obj ||= begin
218
235
  if version_cache.wanted_version_number
219
- obj = versions.where(:number => version_cache.wanted_version_number).first
220
- unless obj or version_cache.self_version
236
+ obj = versions.
237
+ where(:number => version_cache.wanted_version_number).first
238
+ unless obj || version_cache.self_version
221
239
  # versions.to_a # TODO: prefetch versions before building a new one?
222
- obj = versions.build(
240
+ obj = versions.build({
223
241
  :number => version_cache.wanted_version_number,
224
242
  :versioned_attributes => versioned_attributes,
225
- :created_at => updated_at_was)
243
+ :created_at => updated_at_was
244
+ })
226
245
  end
227
246
  obj
228
247
  else
229
248
  editing_time = self.class.versioning_options[:editing_time]
230
- if !editing_time or version_updated_at <= (Time.now-editing_time.to_i) or updated_at > Time.now # allow future versions
249
+ if !editing_time || version_updated_at <= (Time.now-editing_time.to_i) || updated_at > Time.now # allow future versions
231
250
  versions.build(:created_at => updated_at_was)
232
251
  end
233
252
  end
@@ -271,11 +290,16 @@ module Vidibus
271
290
  version_cache.original_version_obj.save!
272
291
  elsif version_cache.wanted_version_number
273
292
  if version_obj
274
- saved = version_obj.update_attributes(:versioned_attributes => versioned_attributes, :created_at => updated_at)
293
+ saved = version_obj.update_attributes({
294
+ :versioned_attributes => versioned_attributes,
295
+ :created_at => updated_at
296
+ })
275
297
  end
276
298
  return saved unless version_cache.self_version
277
299
  elsif version_obj
278
- version_obj.update_attributes!(:versioned_attributes => original_attributes)
300
+ version_obj.update_attributes!({
301
+ :versioned_attributes => original_attributes
302
+ })
279
303
  self.version_number = version_obj.number + 1
280
304
  self.version_updated_at = Time.now
281
305
  end
@@ -9,7 +9,7 @@ module Vidibus
9
9
 
10
10
  field :versioned_uuid, :type => String
11
11
  field :versioned_attributes, :type => Hash, :default => {}
12
- field :number, :type => Integer, :default => nil
12
+ field :number, :type => Integer
13
13
 
14
14
  index :versioned_uuid
15
15
  index :number
@@ -33,7 +33,12 @@ module Vidibus
33
33
 
34
34
  def set_number
35
35
  return if number
36
- previous = Version.desc(:number).limit(1).first
36
+ previous = Version.
37
+ where({
38
+ :versioned_id => versioned_id,
39
+ :versioned_type => versioned_type
40
+ }).
41
+ desc(:number).limit(1).first
37
42
  self.number = previous ? previous.number + 1 : 1
38
43
  end
39
44
 
metadata CHANGED
@@ -1,160 +1,166 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: vidibus-versioning
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 2
10
- version: 0.1.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Andre Pankratz
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-14 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: mongoid
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
18
+ requirements:
27
19
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 2
32
- version: "2"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: vidibus-core_extensions
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: vidibus-core_extensions
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
47
38
  type: :runtime
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: vidibus-uuid
51
39
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: vidibus-uuid
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
61
54
  type: :runtime
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: bundler
65
55
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 23
72
- segments:
73
- - 1
74
- - 0
75
- - 0
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
76
69
  version: 1.0.0
77
70
  type: :development
78
- version_requirements: *id004
79
- - !ruby/object:Gem::Dependency
80
- name: rake
81
71
  prerelease: false
82
- requirement: &id005 !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
91
86
  type: :development
92
- version_requirements: *id005
93
- - !ruby/object:Gem::Dependency
94
- name: rdoc
95
87
  prerelease: false
96
- requirement: &id006 !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ">="
100
- - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
103
- - 0
104
- version: "0"
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rdoc
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
105
102
  type: :development
106
- version_requirements: *id006
107
- - !ruby/object:Gem::Dependency
108
- name: rcov
109
103
  prerelease: false
110
- requirement: &id007 !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: simplecov
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
119
118
  type: :development
120
- version_requirements: *id007
121
- - !ruby/object:Gem::Dependency
122
- name: rspec
123
119
  prerelease: false
124
- requirement: &id008 !ruby/object:Gem::Requirement
120
+ version_requirements: !ruby/object:Gem::Requirement
125
121
  none: false
126
- requirements:
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
127
131
  - - ~>
128
- - !ruby/object:Gem::Version
129
- hash: 7
130
- segments:
131
- - 2
132
- version: "2"
132
+ - !ruby/object:Gem::Version
133
+ version: '2'
133
134
  type: :development
134
- version_requirements: *id008
135
- - !ruby/object:Gem::Dependency
136
- name: rr
137
135
  prerelease: false
138
- requirement: &id009 !ruby/object:Gem::Requirement
139
- none: false
140
- requirements:
141
- - - ">="
142
- - !ruby/object:Gem::Version
143
- hash: 3
144
- segments:
145
- - 0
146
- version: "0"
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '2'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rr
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
147
150
  type: :development
148
- version_requirements: *id009
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
149
158
  description: Versioning designed for advanced usage, like scheduling versions.
150
159
  email: andre@vidibus.com
151
160
  executables: []
152
-
153
161
  extensions: []
154
-
155
162
  extra_rdoc_files: []
156
-
157
- files:
163
+ files:
158
164
  - lib/vidibus/versioning/_version.rb
159
165
  - lib/vidibus/versioning/errors.rb
160
166
  - lib/vidibus/versioning/mongoid.rb
@@ -164,41 +170,32 @@ files:
164
170
  - LICENSE
165
171
  - README.md
166
172
  - Rakefile
167
- has_rdoc: true
168
173
  homepage: https://github.com/vidibus/vidibus-versioning
169
- licenses: []
170
-
174
+ licenses:
175
+ - MIT
171
176
  post_install_message:
172
177
  rdoc_options: []
173
-
174
- require_paths:
178
+ require_paths:
175
179
  - lib
176
- required_ruby_version: !ruby/object:Gem::Requirement
180
+ required_ruby_version: !ruby/object:Gem::Requirement
177
181
  none: false
178
- requirements:
179
- - - ">="
180
- - !ruby/object:Gem::Version
181
- hash: 3
182
- segments:
182
+ requirements:
183
+ - - ! '>='
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ segments:
183
187
  - 0
184
- version: "0"
185
- required_rubygems_version: !ruby/object:Gem::Requirement
188
+ hash: -1266597105678882758
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
190
  none: false
187
- requirements:
188
- - - ">="
189
- - !ruby/object:Gem::Version
190
- hash: 23
191
- segments:
192
- - 1
193
- - 3
194
- - 6
191
+ requirements:
192
+ - - ! '>='
193
+ - !ruby/object:Gem::Version
195
194
  version: 1.3.6
196
195
  requirements: []
197
-
198
196
  rubyforge_project: vidibus-versioning
199
- rubygems_version: 1.6.2
197
+ rubygems_version: 1.8.24
200
198
  signing_key:
201
199
  specification_version: 3
202
200
  summary: Advanced versioning for Mongoid models
203
201
  test_files: []
204
-