activerecord-safe_initialize 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a817b8badef4a8323d46f2b56f5a3d9d1e4425c6
4
- data.tar.gz: 7a97aa0476ede030e0278b5a5a8c8a897ef2e89a
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OGM2ZTczZTU3NjBjMWYxNDU2OTAyZTcyN2JhMmNhYzdiNzE0ZDRhNA==
5
+ data.tar.gz: !binary |-
6
+ ZGJkYjE4YjczYWU5ZWMxZWQ2MTllZjdmYmI0YjJmZDBkMTQ4OTNiNw==
5
7
  SHA512:
6
- metadata.gz: 018e2cc671635c149e20519d257818f94e601d9a84c774fce04a652ef6f6a8b00877eafc7625e2a4973e43aa9661f6258fa488e12c0bf40a5a13a4bb22510a62
7
- data.tar.gz: 22fa1c3518284b18b80f97c404a80ea152c8e2609997ccb8beb3cfed5e05a39228eed4d92a613232f8b03482881a0f3777752f0e4421af55916af9e15e512b9f
8
+ metadata.gz: !binary |-
9
+ ZjVmZWE0ZGU1NGI0MzMyMTk1OWQzYmU2NTRhNTQ1NTMxY2U4YjBhNWU4NDky
10
+ NzhlYjI0MmUzZjg1NTAyN2IxNzY2NTQ4Y2FkNGM4ODI1ZDk4MjlmMjgwYzA3
11
+ N2Y1NDFmNjdjMGE0M2FhYWI2ZmViNjcxMTQxNGNkYWMxNDA3YzQ=
12
+ data.tar.gz: !binary |-
13
+ NTg3OTM3YzA1OWZhY2JkYWRlMWFiNjJiMDdlZjVkYjc5NjJlYTYxOWUzMTdl
14
+ YjYxZjRiM2JmOTMzMzk5MzUzZjRjMWYwNTY1ZGYwNmEyNDRmYTdkMjQwNzI0
15
+ NDY3OWQ0OTkzMDliMWJkMDVkMTIwNjUwNTVkZmJhMzQwNDQzM2I=
data/README.md CHANGED
@@ -77,6 +77,20 @@ class Employee < ActiveRecord::Base
77
77
  end
78
78
  ```
79
79
 
80
+ Options are passed through to `after_initialize`:
81
+
82
+ ```ruby
83
+ safe_initialize :uuid, with: ->{ SecureRandom.uuid }, :if => :new_record?
84
+ ```
85
+
86
+ A block can be used instead of `:with`
87
+
88
+ ```ruby
89
+ safe_initialize :uuid, :if => :new_record? do
90
+ SecureRandom.uuid
91
+ end
92
+ ```
93
+
80
94
  ## Contributing
81
95
 
82
96
  1. Fork it ( https://github.com/lyconic/activerecord-safe_initialize/fork )
@@ -12,6 +12,8 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = "https://github.com/lyconic/activerecord-safe_initialize"
13
13
  spec.license = "MIT"
14
14
 
15
+ spec.required_ruby_version = ">= 1.9.3"
16
+
15
17
  spec.files = `git ls-files -z`.split("\x0")
16
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -4,14 +4,16 @@ require 'active_support/core_ext/array/extract_options'
4
4
 
5
5
  module ActiveRecord
6
6
  module SafeInitialize
7
- def safe_initialize(*attributes)
7
+ def safe_initialize(*attributes, &block)
8
8
  options = attributes.extract_options!
9
- raise ArgumentError, "Missing initialization value" unless options[:with]
9
+ default = options.fetch(:with, block)
10
+ raise ArgumentError, "Missing initialization value" unless default
11
+ warn "Both :with and block are present; using :with value" if options[:with] && block_given?
10
12
 
11
- after_initialize do
13
+ after_initialize(options) do
12
14
  attributes.each do |attribute|
13
15
  if has_attribute?(attribute) && read_attribute(attribute).nil?
14
- value = options[:with]
16
+ value = default
15
17
  value = instance_exec(&value) if value.respond_to?(:call)
16
18
  value = self.send(value) if value.is_a?(Symbol)
17
19
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module SafeInitialize
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -35,6 +35,35 @@ describe ActiveRecord::SafeInitialize do
35
35
 
36
36
  expect( klass.first.uuid ).to eq 'baz'
37
37
  end
38
+
39
+ it "accepts a block argument in place of :with" do
40
+ klass = Class.new(Post)
41
+ klass.safe_initialize :uuid do
42
+ SecureRandom.uuid
43
+ end
44
+
45
+ expect{ klass.new }.to_not raise_error
46
+ expect( klass.new.uuid ).to_not be nil
47
+ end
48
+
49
+ it "warns if both :with and block are present" do
50
+ klass = Class.new(Post)
51
+
52
+ expect(klass).to receive(:warn)
53
+
54
+ klass.safe_initialize :uuid, with: 'Foo' do
55
+ 'Bar'
56
+ end
57
+ end
58
+
59
+ it "uses :with instead of block if both are present" do
60
+ klass = Class.new(Post)
61
+ klass.safe_initialize :uuid, with: 'Foo' do
62
+ 'Bar'
63
+ end
64
+
65
+ expect( klass.new.uuid ).to eq 'Foo'
66
+ end
38
67
  end
39
68
 
40
69
  context "existing value" do
@@ -62,4 +91,30 @@ describe ActiveRecord::SafeInitialize do
62
91
  expect( inst.uuid ).to eq 'foo'
63
92
  end
64
93
  end
94
+
95
+ context "with conditional arguments" do
96
+ it "does not run if: false" do
97
+ klass = Class.new(Post)
98
+ klass.class_eval do
99
+ safe_initialize :uuid, with: ->{ SecureRandom.uuid }, :if => :new_record?
100
+ def new_record?; false; end
101
+ end
102
+
103
+ inst = klass.new
104
+
105
+ expect( inst.uuid ).to be nil
106
+ end
107
+
108
+ it "does not run unless: true" do
109
+ klass = Class.new(Post)
110
+ klass.class_eval do
111
+ safe_initialize :uuid, with: ->{ SecureRandom.uuid }, :unless => :foo?
112
+ def foo?; true; end
113
+ end
114
+
115
+ inst = klass.new
116
+
117
+ expect( inst.uuid ).to be nil
118
+ end
119
+ end
65
120
  end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-safe_initialize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Lassek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-02 00:00:00.000000000 Z
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.7'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.7'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - ! '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description:
@@ -87,9 +87,9 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".gitignore"
91
- - ".rspec"
92
- - ".travis.yml"
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
93
  - Gemfile
94
94
  - LICENSE.txt
95
95
  - README.md
@@ -117,17 +117,17 @@ require_paths:
117
117
  - lib
118
118
  required_ruby_version: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - ">="
120
+ - - ! '>='
121
121
  - !ruby/object:Gem::Version
122
- version: '0'
122
+ version: 1.9.3
123
123
  required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  requirements:
125
- - - ">="
125
+ - - ! '>='
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
128
  requirements: []
129
129
  rubyforge_project:
130
- rubygems_version: 2.4.6
130
+ rubygems_version: 2.4.5
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: Safely initialize an ActiveRecord attribute with respect to missing columns