baza_models 0.0.10 → 0.0.11

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: 1f36a0c124016cd7e0581a2c8961fd8db936ab38
4
- data.tar.gz: 2dddfd82596befd924a26845772adfb076b27460
3
+ metadata.gz: 3de3c11b6d7e028cf0bc3c1b0f8cd8b63718499a
4
+ data.tar.gz: cf06183a93093983fedf4e4adef1d1b333b626ac
5
5
  SHA512:
6
- metadata.gz: a76e9cd2918a4045554fca63c435c367416f427f3f5574e0071bd4271f3480c964f2d3ec24c93e44ce2c3fc6fb29bfc26943b4b45271957700ddb2976ad4e888
7
- data.tar.gz: cc0e83de318b00208e3f18b58ab874221c415d4dc1357845c467d591592f4637e53fd14cae6c2faf6291c5cf5f1a9db8ed2c3c2104ad66566f5a27156a94a1d6
6
+ metadata.gz: a72519f5547333647837c34d992c3f11e48eed797561a98c8c641a4fa56b67ebfe76c06979fc12ded41af6807b8c58007701a9ac5629a3ee71ace56e945cbe72
7
+ data.tar.gz: 78916b055f54b09e554b8d90a2e82e5af2b1d8f02c46a2b798997d1d085f5bb46512b94d23cb0b18dcb73c218cf5e9ccb3e849368f674945747e083841cb0b5c
data/Gemfile CHANGED
@@ -10,9 +10,7 @@ gem "html_gen", ">= 0.0.12", require: false
10
10
  gem "simple_delegate", ">= 0.0.2"
11
11
  gem "string-cases", ">= 0.0.3"
12
12
 
13
- # Add dependencies to develop your gem here.
14
- # Include everything needed to run rake, tests, features, etc.
15
- group :development do
13
+ group :development, :test do
16
14
  gem "best_practice_project", require: false, github: "kaspernj/best_practice_project"
17
15
  gem "bundler", "~> 1.0"
18
16
  gem "factory_girl"
@@ -63,8 +63,8 @@ GEM
63
63
  rake
64
64
  rdoc
65
65
  semver2
66
- json (1.8.3)
67
- json (1.8.3-java)
66
+ json (1.8.6)
67
+ json (1.8.6-java)
68
68
  jwt (1.5.6)
69
69
  mime-types (2.99.3)
70
70
  mini_portile2 (2.2.0)
@@ -147,4 +147,4 @@ DEPENDENCIES
147
147
  string-cases (>= 0.0.3)
148
148
 
149
149
  BUNDLED WITH
150
- 1.13.7
150
+ 1.16.1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: baza_models 0.0.10 ruby lib
5
+ # stub: baza_models 0.0.11 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza_models".freeze
9
- s.version = "0.0.10"
9
+ s.version = "0.0.11"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["kaspernj".freeze]
14
- s.date = "2017-06-06"
14
+ s.date = "2018-01-09"
15
15
  s.description = "ActiveRecord like models for the Baza database framework".freeze
16
16
  s.email = "k@spernj.org".freeze
17
17
  s.extra_rdoc_files = [
@@ -104,7 +104,7 @@ Gem::Specification.new do |s|
104
104
  ]
105
105
  s.homepage = "http://github.com/kaspernj/baza_models".freeze
106
106
  s.licenses = ["MIT".freeze]
107
- s.rubygems_version = "2.6.8".freeze
107
+ s.rubygems_version = "2.6.13".freeze
108
108
  s.summary = "ActiveRecord like models for the Baza database framework".freeze
109
109
 
110
110
  if s.respond_to? :specification_version then
@@ -57,6 +57,7 @@ class BazaModels::Model
57
57
  self.class.init_model
58
58
 
59
59
  reset_errors
60
+ @before_last_save = {}
60
61
  @changes = {}
61
62
 
62
63
  if args[:init]
@@ -315,6 +316,14 @@ protected
315
316
  false
316
317
  end
317
318
  end
319
+
320
+ define_method("will_save_change_to_#{column_name}?") do
321
+ will_save_change_to_attribute?(column_name)
322
+ end
323
+
324
+ define_method("#{column_name}_before_last_save") do
325
+ attribute_before_last_save(column_name)
326
+ end
318
327
  end
319
328
 
320
329
  def reset_errors
@@ -333,7 +342,7 @@ protected
333
342
  pass_args = callback_data.fetch(:args)
334
343
  pass_args = [] if method_obj.arity == 0
335
344
 
336
- __send__(callback_data[:method_name], *pass_args)
345
+ __send__(callback_data.fetch(:method_name), *pass_args)
337
346
  else
338
347
  raise "Didn't know how to perform callbacks for #{name}"
339
348
  end
@@ -374,6 +383,16 @@ protected
374
383
  new_attributes
375
384
  end
376
385
 
386
+ def attribute_before_last_save(attribute_name)
387
+ return @before_last_save.fetch(attribute_name) if @before_last_save.key?(attribute_name)
388
+ @data.fetch(attribute_name)
389
+ end
390
+
391
+ def will_save_change_to_attribute?(attribute_name)
392
+ return true if @changes.key?(attribute_name) && @changes[attribute_name] != data[attribute_name]
393
+ false
394
+ end
395
+
377
396
  def method_missing(method_name, *args, &blk)
378
397
  return @data.fetch(method_name) if @data.key?(method_name)
379
398
  super
@@ -10,9 +10,11 @@ module BazaModels::Model::Manipulation
10
10
  self.updated_at = Time.now if changed? && has_attribute?(:updated_at)
11
11
 
12
12
  if new_record
13
+ @before_last_save = @data
13
14
  status = create
14
15
  else
15
16
  fire_callbacks(:before_update)
17
+ @before_last_save = @data
16
18
  db.update(table_name, @changes, id: id) if changed?
17
19
  fire_callbacks(:after_update)
18
20
  status = true
@@ -22,6 +24,7 @@ module BazaModels::Model::Manipulation
22
24
 
23
25
  @changes = {}
24
26
  @new_record = false
27
+
25
28
  reload
26
29
 
27
30
  fire_callbacks(:after_save)
@@ -80,6 +83,10 @@ module BazaModels::Model::Manipulation
80
83
  end
81
84
 
82
85
  def update_attributes!(attributes)
86
+ update!(attributes)
87
+ end
88
+
89
+ def update!(attributes)
83
90
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless update_attributes(attributes)
84
91
  end
85
92
 
@@ -210,4 +210,22 @@ describe "BazaModels::Model" do
210
210
  user = User.new("email" => "test@example.com")
211
211
  expect(user.email).to eq "test@example.com"
212
212
  end
213
+
214
+ describe "#attribute_before_last_save" do
215
+ it "returns the value as it was before the save" do
216
+ user.save!
217
+
218
+ user.update!(email: "test2@example.com")
219
+
220
+ expect(user.email_before_last_save).to eq "test@example.com"
221
+ end
222
+ end
223
+
224
+ describe "#will_save_change_to_attribute?" do
225
+ it "returns true if the attribute will change" do
226
+ user.save!
227
+ user.email = "test2@example.com"
228
+ expect(user.will_save_change_to_email?).to eq true
229
+ end
230
+ end
213
231
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-06 00:00:00.000000000 Z
11
+ date: 2018-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator
@@ -344,7 +344,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
344
344
  version: '0'
345
345
  requirements: []
346
346
  rubyforge_project:
347
- rubygems_version: 2.6.8
347
+ rubygems_version: 2.6.13
348
348
  signing_key:
349
349
  specification_version: 4
350
350
  summary: ActiveRecord like models for the Baza database framework