ar_default_values 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ar_default_values.gemspec +1 -1
- data/lib/ar_default_values.rb +1 -58
- data/lib/default_values/dsl.rb +21 -0
- data/lib/{ar_default_values → default_values}/railtie.rb +1 -1
- data/lib/{ar_default_values → default_values}/version.rb +1 -1
- data/lib/default_values.rb +41 -0
- metadata +42 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f378b412d241888efe1989d935604e0f2bf1ad0
|
4
|
+
data.tar.gz: 6568b728fa614d3d08572ba593522bfc57d6eebf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4714919c1bdc29890ac0e72bffb6e6d09a0168010f8f8543844f4538e848f15a698b768923a6eda5ee55fd6c5d942c582c079af3a8c5fc819b796a58e002fde1
|
7
|
+
data.tar.gz: 2c3d44bf0989fc5863853c4a84b883902b899fb889afa4a77d351d2a69289d2c8b9116382360cbd2c41effcbd6ec98c44ead679a3842b45576a4ce6f4be1c2e8
|
data/ar_default_values.gemspec
CHANGED
data/lib/ar_default_values.rb
CHANGED
@@ -1,58 +1 @@
|
|
1
|
-
require '
|
2
|
-
require 'ar_default_values/railtie' if defined?(Rails)
|
3
|
-
|
4
|
-
#
|
5
|
-
# = ActiveRecord::DefaultValue
|
6
|
-
# initialize with default_values
|
7
|
-
#
|
8
|
-
# == sample
|
9
|
-
# you can specify default values with hash:
|
10
|
-
#
|
11
|
-
# class Book < ActiveRecord::Base
|
12
|
-
# default_values rating: 'r18', type: 'comic'
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# book = Book.new
|
16
|
-
# book.rating # => "r18"
|
17
|
-
# book.type # => "comic"
|
18
|
-
# book.title # => nil
|
19
|
-
#
|
20
|
-
# you can use instance values with lambda:
|
21
|
-
# class Book < ActiveRecord::Base
|
22
|
-
# default_values type: 'comic', released_at: lambda { Time.now }
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# or with block:
|
26
|
-
# class Book < ActiveRecord::Base
|
27
|
-
# default_values type: 'comic' do
|
28
|
-
# t = Time.now
|
29
|
-
# {released_at: t, edition_updated_at: t}
|
30
|
-
# end
|
31
|
-
# end
|
32
|
-
#
|
33
|
-
# book1 = Book.new
|
34
|
-
# # wait 10 sec
|
35
|
-
# book2 = Book.new
|
36
|
-
# book1.release_at == book2.release_at # => false
|
37
|
-
# book1.released_at == book1.edition_updated_at # => true
|
38
|
-
#
|
39
|
-
module DefaultValues
|
40
|
-
extend ActiveSupport::Concern
|
41
|
-
|
42
|
-
module ClassMethods
|
43
|
-
def default_values(defaults = {}, &block)
|
44
|
-
define_method(:initialize_with_default_values) do |*attributes, &inner_block|
|
45
|
-
defaults = defaults.merge(block.call) if block_given?
|
46
|
-
defaults = Hash[defaults.each_pair.map { |key, value| [key, value.is_a?(Proc) ? value.call : value] }]
|
47
|
-
|
48
|
-
initialize_without_default_values(*attributes, &inner_block)
|
49
|
-
|
50
|
-
defaults.each_pair do |key, value|
|
51
|
-
write_attribute(key, value) unless attribute_present?(key)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
alias_method_chain :initialize, :default_values
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
1
|
+
require 'default_values'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DefaultValues
|
2
|
+
module DSL
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def default_values(defaults = {}, &block)
|
7
|
+
define_method(:initialize_with_default_values) do |*attributes, &inner_block|
|
8
|
+
defaults = defaults.merge(block.call) if block_given?
|
9
|
+
defaults = Hash[defaults.each_pair.map { |key, value| [key, value.is_a?(Proc) ? value.call : value] }]
|
10
|
+
|
11
|
+
initialize_without_default_values(*attributes, &inner_block)
|
12
|
+
|
13
|
+
defaults.each_pair do |key, value|
|
14
|
+
write_attribute(key, value) unless attribute_present?(key)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method_chain :initialize, :default_values
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'default_values/version'
|
2
|
+
require 'default_values/dsl'
|
3
|
+
require 'default_values/railtie' if defined?(Rails)
|
4
|
+
|
5
|
+
#
|
6
|
+
# = ActiveRecord::DefaultValue
|
7
|
+
# initialize with default_values
|
8
|
+
#
|
9
|
+
# == sample
|
10
|
+
# you can specify default values with hash:
|
11
|
+
#
|
12
|
+
# class Book < ActiveRecord::Base
|
13
|
+
# default_values rating: 'r18', type: 'comic'
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# book = Book.new
|
17
|
+
# book.rating # => "r18"
|
18
|
+
# book.type # => "comic"
|
19
|
+
# book.title # => nil
|
20
|
+
#
|
21
|
+
# you can use instance values with lambda:
|
22
|
+
# class Book < ActiveRecord::Base
|
23
|
+
# default_values type: 'comic', released_at: lambda { Time.now }
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# or with block:
|
27
|
+
# class Book < ActiveRecord::Base
|
28
|
+
# default_values type: 'comic' do
|
29
|
+
# t = Time.now
|
30
|
+
# {released_at: t, edition_updated_at: t}
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# book1 = Book.new
|
35
|
+
# # wait 10 sec
|
36
|
+
# book2 = Book.new
|
37
|
+
# book1.release_at == book2.release_at # => false
|
38
|
+
# book1.released_at == book1.edition_updated_at # => true
|
39
|
+
#
|
40
|
+
module DefaultValues
|
41
|
+
end
|
metadata
CHANGED
@@ -1,209 +1,209 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_default_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masarakki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
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: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '3.2'
|
34
34
|
type: :runtime
|
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: '3.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '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: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
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: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
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
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rspec-rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rspec-its
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: pry
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - '>='
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: pry-doc
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
|
-
- -
|
129
|
+
- - '>='
|
130
130
|
- !ruby/object:Gem::Version
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
|
-
- -
|
136
|
+
- - '>='
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: guard
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- -
|
143
|
+
- - '>='
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: '0'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
|
-
- -
|
150
|
+
- - '>='
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: guard-rspec
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- -
|
157
|
+
- - '>='
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
|
-
- -
|
164
|
+
- - '>='
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: guard-bundler
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
170
170
|
requirements:
|
171
|
-
- -
|
171
|
+
- - '>='
|
172
172
|
- !ruby/object:Gem::Version
|
173
173
|
version: '0'
|
174
174
|
type: :development
|
175
175
|
prerelease: false
|
176
176
|
version_requirements: !ruby/object:Gem::Requirement
|
177
177
|
requirements:
|
178
|
-
- -
|
178
|
+
- - '>='
|
179
179
|
- !ruby/object:Gem::Version
|
180
180
|
version: '0'
|
181
181
|
- !ruby/object:Gem::Dependency
|
182
182
|
name: guard-rubocop
|
183
183
|
requirement: !ruby/object:Gem::Requirement
|
184
184
|
requirements:
|
185
|
-
- -
|
185
|
+
- - '>='
|
186
186
|
- !ruby/object:Gem::Version
|
187
187
|
version: '0'
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
|
-
- -
|
192
|
+
- - '>='
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
- !ruby/object:Gem::Dependency
|
196
196
|
name: rdoc
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
198
198
|
requirements:
|
199
|
-
- -
|
199
|
+
- - '>='
|
200
200
|
- !ruby/object:Gem::Version
|
201
201
|
version: '0'
|
202
202
|
type: :development
|
203
203
|
prerelease: false
|
204
204
|
version_requirements: !ruby/object:Gem::Requirement
|
205
205
|
requirements:
|
206
|
-
- -
|
206
|
+
- - '>='
|
207
207
|
- !ruby/object:Gem::Version
|
208
208
|
version: '0'
|
209
209
|
description: ActiveRecord with default values
|
@@ -213,11 +213,11 @@ executables: []
|
|
213
213
|
extensions: []
|
214
214
|
extra_rdoc_files: []
|
215
215
|
files:
|
216
|
-
-
|
217
|
-
-
|
218
|
-
-
|
219
|
-
-
|
220
|
-
-
|
216
|
+
- .document
|
217
|
+
- .gitignore
|
218
|
+
- .rspec
|
219
|
+
- .rubocop.yml
|
220
|
+
- .travis.yml
|
221
221
|
- Gemfile
|
222
222
|
- Guardfile
|
223
223
|
- LICENSE.txt
|
@@ -225,8 +225,10 @@ files:
|
|
225
225
|
- Rakefile
|
226
226
|
- ar_default_values.gemspec
|
227
227
|
- lib/ar_default_values.rb
|
228
|
-
- lib/
|
229
|
-
- lib/
|
228
|
+
- lib/default_values.rb
|
229
|
+
- lib/default_values/dsl.rb
|
230
|
+
- lib/default_values/railtie.rb
|
231
|
+
- lib/default_values/version.rb
|
230
232
|
- spec/ar_default_values_spec.rb
|
231
233
|
- spec/dummy/README.rdoc
|
232
234
|
- spec/dummy/Rakefile
|
@@ -283,17 +285,17 @@ require_paths:
|
|
283
285
|
- lib
|
284
286
|
required_ruby_version: !ruby/object:Gem::Requirement
|
285
287
|
requirements:
|
286
|
-
- -
|
288
|
+
- - '>='
|
287
289
|
- !ruby/object:Gem::Version
|
288
290
|
version: '0'
|
289
291
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
290
292
|
requirements:
|
291
|
-
- -
|
293
|
+
- - '>='
|
292
294
|
- !ruby/object:Gem::Version
|
293
295
|
version: '0'
|
294
296
|
requirements: []
|
295
297
|
rubyforge_project:
|
296
|
-
rubygems_version: 2.
|
298
|
+
rubygems_version: 2.0.14
|
297
299
|
signing_key:
|
298
300
|
specification_version: 4
|
299
301
|
summary: ActiveRecord with default values
|
@@ -344,4 +346,3 @@ test_files:
|
|
344
346
|
- spec/dummy/public/favicon.ico
|
345
347
|
- spec/rails_helper.rb
|
346
348
|
- spec/spec_helper.rb
|
347
|
-
has_rdoc:
|