attribute-defaults 0.5.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +3 -1
- data/lib/attribute_defaults.rb +15 -18
- metadata +41 -33
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 38c546a1c23c35b30ed286726bbf47f7202708cbf1fcce9614766e98bee3af60
|
4
|
+
data.tar.gz: 8ec2f406c28b50e74ddcc1650ed9e7aded34b47f1787b9eb313e7a000caf59fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5aee2bd1dad485a2aa2333a2a47654ae56c8e25880a242420abe3d06792120c02e8f8e07f115cc2ba0b071ea1744f496e1731bb62db05cd65c1f8cbdf2620bfe
|
7
|
+
data.tar.gz: a2d5ea51c06d1b56c4281d512531ab2bf2d5cdd08c267b765be2ea37019eb338f881a183c14a8151fb9ca179cb9bef32b65fb30bb3b99ef1b0c43521f6ab9fd9
|
data/README.rdoc
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
= Attribute Defaults (attribute-defaults)
|
2
2
|
|
3
|
+
{<img src="https://travis-ci.org/bsm/attribute-defaults.png?branch=master" alt="Build Status" />}[https://travis-ci.org/bsm/attribute-defaults]
|
4
|
+
|
3
5
|
Simple ActiveRecord plugin that allows to specify default values for attributes.
|
4
6
|
|
5
7
|
== Installation
|
6
8
|
|
7
|
-
Just add <tt>gem attribute-defaults</tt> to your Gemfile.
|
9
|
+
Just add <tt>gem 'attribute-defaults'</tt> to your Gemfile.
|
8
10
|
|
9
11
|
Alternatively, run <tt>sudo gem install attribute-defaults</tt> and add
|
10
12
|
<tt>require 'attribute_defaults'</tt> to your app.
|
data/lib/attribute_defaults.rb
CHANGED
@@ -6,16 +6,15 @@ module ActiveRecord
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
module ClassMethods
|
9
|
-
|
10
9
|
# Set defaults. Examples:
|
11
10
|
# attr_default :age, 12
|
12
11
|
# attr_default :description, '(none)'
|
13
12
|
# attr_default :age do |record|
|
14
13
|
# Time.zone.year - record.dob.year
|
15
14
|
# end
|
16
|
-
# attr_default :description, '(none)', :
|
17
|
-
# attr_default :description, '(none)', :
|
18
|
-
# attr_default :description, :
|
15
|
+
# attr_default :description, '(none)', if: :blank
|
16
|
+
# attr_default :description, '(none)', persisted: false
|
17
|
+
# attr_default :description, default: '(none)', persisted: false
|
19
18
|
def attr_default(sym, *args, &block)
|
20
19
|
options = args.extract_options!
|
21
20
|
default = options.delete(:default) || args.shift
|
@@ -23,38 +22,36 @@ module ActiveRecord
|
|
23
22
|
|
24
23
|
evaluator = "__eval_attr_default_for_#{sym}".to_sym
|
25
24
|
setter = "__set_attr_default_for_#{sym}".to_sym
|
26
|
-
block ||= default.is_a?(Proc) ? default :
|
25
|
+
block ||= default.is_a?(Proc) ? default : -> { default }
|
27
26
|
|
28
27
|
after_initialize setter.to_sym
|
29
28
|
define_method(evaluator, &block)
|
30
29
|
|
31
30
|
module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
|
32
|
-
def #{setter}
|
33
|
-
#{'return if persisted?' if options[:persisted] == false}
|
34
|
-
return unless
|
35
|
-
value = #{evaluator}#{'(self)' unless block.arity.zero?}
|
36
|
-
self.#{sym} = value.duplicable? ? value.dup : value
|
37
|
-
rescue ActiveModel::MissingAttributeError
|
38
|
-
end
|
31
|
+
def #{setter} # def __set_attr_default_for_age
|
32
|
+
#{'return if persisted?' if options[:persisted] == false} # return if persisted?
|
33
|
+
return unless #{sym}.send(:#{options[:if] || 'nil?'}) # return unless age.nil?
|
34
|
+
value = #{evaluator}#{'(self)' unless block.arity.zero?} # value = __eval_attr_default_for_age(self)
|
35
|
+
self.#{sym} = value.duplicable? ? value.dup : value # self.age = value.duplicable? ? value.dup : value
|
36
|
+
rescue ActiveModel::MissingAttributeError # rescue ActiveModel::MissingAttributeError
|
37
|
+
end # end
|
39
38
|
EVAL
|
40
39
|
private evaluator, setter
|
41
40
|
end
|
42
41
|
|
43
42
|
# Default mass-assignment. Examples:
|
44
|
-
# attr_defaults
|
45
|
-
# attr_defaults
|
46
|
-
# attr_defaults
|
43
|
+
# attr_defaults description: '(none)', age: 12
|
44
|
+
# attr_defaults description: '(none)', age: ->r { Time.zone.year - r.dob.year }
|
45
|
+
# attr_defaults description: '(none)', age: { default: 12, persisted: false }
|
47
46
|
def attr_defaults(pairs)
|
48
47
|
pairs.each do |pair|
|
49
|
-
attr_default
|
48
|
+
attr_default(*pair)
|
50
49
|
end
|
51
50
|
end
|
52
|
-
|
53
51
|
end
|
54
52
|
end
|
55
53
|
|
56
54
|
class Base
|
57
55
|
include AttributesWithDefaults
|
58
56
|
end
|
59
|
-
|
60
57
|
end
|
metadata
CHANGED
@@ -1,78 +1,89 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute-defaults
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.9.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Dimitrij Denissenko
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-03-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: 5.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 7.0.0
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
25
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 5.0.0
|
30
|
+
- - "<"
|
28
31
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
32
|
+
version: 7.0.0
|
30
33
|
- !ruby/object:Gem::Dependency
|
31
34
|
name: rake
|
32
35
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
36
|
requirements:
|
35
|
-
- -
|
37
|
+
- - ">="
|
36
38
|
- !ruby/object:Gem::Version
|
37
39
|
version: '0'
|
38
40
|
type: :development
|
39
41
|
prerelease: false
|
40
42
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
43
|
requirements:
|
43
|
-
- -
|
44
|
+
- - ">="
|
44
45
|
- !ruby/object:Gem::Version
|
45
46
|
version: '0'
|
46
47
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
48
|
+
name: rspec
|
48
49
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
57
|
requirements:
|
59
|
-
- -
|
58
|
+
- - ">="
|
60
59
|
- !ruby/object:Gem::Version
|
61
60
|
version: '0'
|
62
61
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
62
|
+
name: rubocop-bsm
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: sqlite3
|
64
77
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
78
|
requirements:
|
67
|
-
- -
|
79
|
+
- - ">="
|
68
80
|
- !ruby/object:Gem::Version
|
69
81
|
version: '0'
|
70
82
|
type: :development
|
71
83
|
prerelease: false
|
72
84
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
85
|
requirements:
|
75
|
-
- -
|
86
|
+
- - ">="
|
76
87
|
- !ruby/object:Gem::Version
|
77
88
|
version: '0'
|
78
89
|
description: ActiveRecord plugin that allows to specify default values for attributes
|
@@ -87,27 +98,24 @@ files:
|
|
87
98
|
- lib/attribute_defaults.rb
|
88
99
|
homepage: https://github.com/bsm/attribute-defaults
|
89
100
|
licenses: []
|
101
|
+
metadata: {}
|
90
102
|
post_install_message:
|
91
103
|
rdoc_options: []
|
92
104
|
require_paths:
|
93
105
|
- lib
|
94
106
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
-
none: false
|
96
107
|
requirements:
|
97
|
-
- -
|
108
|
+
- - ">="
|
98
109
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
110
|
+
version: '2.6'
|
100
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
112
|
requirements:
|
103
|
-
- -
|
113
|
+
- - ">="
|
104
114
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
115
|
+
version: '0'
|
106
116
|
requirements: []
|
107
|
-
|
108
|
-
rubygems_version: 1.8.24
|
117
|
+
rubygems_version: 3.1.4
|
109
118
|
signing_key:
|
110
|
-
specification_version:
|
119
|
+
specification_version: 4
|
111
120
|
summary: Specify default values for ActiveRecord attributes
|
112
121
|
test_files: []
|
113
|
-
has_rdoc:
|