enum_ext 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +42 -0
- data/LICENSE.txt +21 -0
- data/README.md +200 -0
- data/Rakefile +2 -0
- data/enum_ext.gemspec +27 -0
- data/lib/enum_ext/version.rb +3 -0
- data/lib/enum_ext.rb +2 -0
- metadata +60 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28a9c98466010b9ab1ce952a1adbeb9359967189
|
4
|
+
data.tar.gz: 39445759f6344b716081517f51e5072fe966cc5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f13183dbbd1e2a91f083c1d75dcffe0096069766d85f10ee6d0a23808b9dfa56713c30050f486199efa993549ad3c5770cb28366c1dfcc45fdd816b729b4df51
|
7
|
+
data.tar.gz: b06cec1f9fd1a3fdffc35257b0538e3a8b30d49c257a856ca088ffd180bee84a28744b3b4300c8cb6514ac7c8f7f8d2d77f1f6040b41fb8e9e71a4ff7c9f9511
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.idea/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
enum_ext (0.1.1)
|
5
|
+
activerecord (>= 4.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (4.2.5)
|
11
|
+
activesupport (= 4.2.5)
|
12
|
+
builder (~> 3.1)
|
13
|
+
activerecord (4.2.5)
|
14
|
+
activemodel (= 4.2.5)
|
15
|
+
activesupport (= 4.2.5)
|
16
|
+
arel (~> 6.0)
|
17
|
+
activesupport (4.2.5)
|
18
|
+
i18n (~> 0.7)
|
19
|
+
json (~> 1.7, >= 1.7.7)
|
20
|
+
minitest (~> 5.1)
|
21
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
22
|
+
tzinfo (~> 1.1)
|
23
|
+
arel (6.0.3)
|
24
|
+
builder (3.2.2)
|
25
|
+
i18n (0.7.0)
|
26
|
+
json (1.8.3)
|
27
|
+
minitest (5.8.3)
|
28
|
+
rake (10.4.2)
|
29
|
+
thread_safe (0.3.5)
|
30
|
+
tzinfo (1.2.2)
|
31
|
+
thread_safe (~> 0.1)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
bundler (~> 1.11)
|
38
|
+
enum_ext!
|
39
|
+
rake (~> 10.0)
|
40
|
+
|
41
|
+
BUNDLED WITH
|
42
|
+
1.11.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 alekseyl
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
# EnumExt
|
2
|
+
|
3
|
+
EnumExt extends rails enum adding localization template, mass-assign on scopes with bang and some sets logic over existing enum.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'enum_ext'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install enum_ext
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Let's assume we have model Request with enum *status*, and we have model Order with requests like this:
|
24
|
+
|
25
|
+
class Request
|
26
|
+
extend EnumExt
|
27
|
+
belongs_to :order
|
28
|
+
enum status: [ :in_cart, :waiting_for_payment, :payed, :ready_for_shipment, :on_delivery, :delivered ]
|
29
|
+
end
|
30
|
+
|
31
|
+
class Order
|
32
|
+
has_many :requests
|
33
|
+
end
|
34
|
+
|
35
|
+
Now let's review some examples of possible enum extensions
|
36
|
+
|
37
|
+
### Localization (localize_enum)
|
38
|
+
|
39
|
+
class Request
|
40
|
+
...
|
41
|
+
localize_enum :status, {
|
42
|
+
|
43
|
+
#locale dependent example ( it dynamically use current locale ):
|
44
|
+
in_cart: -> { I18n.t("request.status.in_cart") },
|
45
|
+
|
46
|
+
#locale dependent example with internal pluralization:
|
47
|
+
payed: -> (t_self) { I18n.t("request.status.payed", count: t_self.sum ) }
|
48
|
+
|
49
|
+
#locale independent:
|
50
|
+
ready_for_shipment: "Ready to go!"
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
Console:
|
55
|
+
|
56
|
+
request.sum = 3
|
57
|
+
request.payed!
|
58
|
+
request.status # >> payed
|
59
|
+
request.t_status # >> "Payed 3 dollars"
|
60
|
+
Request.t_statuses # >> { in_cart: -> { I18n.t("request.status.in_cart") }, .... }
|
61
|
+
|
62
|
+
If you need some substitution you can go like this:
|
63
|
+
|
64
|
+
localize_enum :status, {
|
65
|
+
..
|
66
|
+
delivered: "Delivered at: %{date}"
|
67
|
+
}
|
68
|
+
request.delivered!
|
69
|
+
request.t_status % {date: Time.now.to_s} >> Delivered at: 05.02.2016
|
70
|
+
|
71
|
+
If you need select status on form and so:
|
72
|
+
|
73
|
+
f.select :status, Request.t_statuses.invert.to_a
|
74
|
+
|
75
|
+
|
76
|
+
### Enum Sets (ext_enum_sets)
|
77
|
+
|
78
|
+
This method intended for creating and using some sets of enum values with similar to original enum syntax.
|
79
|
+
|
80
|
+
**Use-case** For example you have pay bills of different types, and you want to group some types in debit and credit "super-types", and have scope PayBill.debit, instance method with question mark as usual enum does pay_bill.debit? and so.
|
81
|
+
|
82
|
+
You can do this with method **ext_enum_sets**, it creates: scopes for subsets like enum did, instance method with ? similar to enum methods, and so...
|
83
|
+
|
84
|
+
I strongly recommend you to create the comment near method call, to remember what methods will be defined
|
85
|
+
|
86
|
+
class Request
|
87
|
+
...
|
88
|
+
#instance non_payed?, delivery_set?, in_warehouse?
|
89
|
+
#class scopes: non_payed, delivery_set, in_warehouse
|
90
|
+
#class scopes: with_statuses, without_statuses
|
91
|
+
#class non_payed_statuses, delivery_set_statuses ( = [:in_cart, :waiting_for_payment], [:ready_for_shipment, :on_delivery, :delivered].. )
|
92
|
+
|
93
|
+
ext_enum_sets :status, {
|
94
|
+
non_payed: [:in_cart, :waiting_for_payment],
|
95
|
+
delivery_set: [:ready_for_shipment, :on_delivery, :delivered] for shipping department for example
|
96
|
+
in_warehouse: [:ready_for_shipment] it's just for example below
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
Console:
|
101
|
+
|
102
|
+
request.waiting_for_payment!
|
103
|
+
request.non_payed? # >> true
|
104
|
+
|
105
|
+
Request.non_payed.exists?(request) # >> true
|
106
|
+
Request.delivery_set.exists?(request) # >> false
|
107
|
+
|
108
|
+
Request.non_payed_statuses # >> [:in_cart, :waiting_for_payment]
|
109
|
+
|
110
|
+
Request.with_statuses( :payed, :in_cart ) # >> scope for all in_cart and payed requests
|
111
|
+
Request.without_statuses( :payed ) # >> scope for all requests with statuses not eq to payed
|
112
|
+
Request.without_statuses( :payed, :non_payed ) # >> scope all requests with statuses not eq to payed and in_cart + waiting_for_payment
|
113
|
+
|
114
|
+
|
115
|
+
#### Rem:
|
116
|
+
|
117
|
+
You can call ext_enum_sets more than one time defining a superposition of already defined sets:
|
118
|
+
|
119
|
+
class Request
|
120
|
+
...
|
121
|
+
ext_enum_sets (... first time you call ext_enum_sets )
|
122
|
+
ext_enum_sets :status, {
|
123
|
+
already_payed: ( [:payed] | delivery_set_statuses ),
|
124
|
+
outside_wharehouse: ( delivery_set_statuses - in_warehouse_statuses )... # any other array operations like &, + and so can be used
|
125
|
+
}
|
126
|
+
|
127
|
+
### Mass-assign ( mass_assign_enum )
|
128
|
+
|
129
|
+
Syntax sugar for mass-assigning enum values.
|
130
|
+
|
131
|
+
**Use-case:** it's often case when I need bulk update without callbacks, so it's gets frustrating to repeat: some_scope.update_all(status: Request.statuses[:new_status], update_at: Time.now)
|
132
|
+
If you need callbacks you can do like this: some_scope.each(&:new_stat!) but if you don't need callbacks and you has hundreds and thousands of records to change at once you need update_all
|
133
|
+
|
134
|
+
class Request
|
135
|
+
...
|
136
|
+
mass_assign_enum( :status )
|
137
|
+
end
|
138
|
+
|
139
|
+
Console:
|
140
|
+
|
141
|
+
request1.in_cart!
|
142
|
+
request2.waiting_for_payment!
|
143
|
+
Request.non_payed.payed!
|
144
|
+
request1.payed? # >> true
|
145
|
+
request2.payed? # >> true
|
146
|
+
request1.updated_at # >> ~ Time.now
|
147
|
+
Request.respond_to?('::MassAssignEnum') # >> true
|
148
|
+
|
149
|
+
order.requests.already_payed.all?(&:already_payed?) # >> true
|
150
|
+
order.requests.already_payed.delivered!
|
151
|
+
order.requests.map(&:status).uniq # >> [:delivered]
|
152
|
+
|
153
|
+
|
154
|
+
####Rem:
|
155
|
+
|
156
|
+
**mass_assign_enum** accepts additional options as last argument. Calling
|
157
|
+
|
158
|
+
mass_assign_enum( :status )
|
159
|
+
|
160
|
+
actually is equal to call:
|
161
|
+
|
162
|
+
mass_assign_enum( :status, { relation: true, association_relation: true } )
|
163
|
+
|
164
|
+
###### Meaning:
|
165
|
+
|
166
|
+
relation: true - Request.some_scope.payed! - works
|
167
|
+
|
168
|
+
association_relation: true - Order.first.requests.scope.new_stat! - works
|
169
|
+
|
170
|
+
**but it wouldn't works without 'scope' part!** If you want to use it without 'scope' you may do it this way:
|
171
|
+
|
172
|
+
class Request
|
173
|
+
...
|
174
|
+
mass_assign_enum( :status, association_relation: false )
|
175
|
+
end
|
176
|
+
|
177
|
+
class Order
|
178
|
+
has_many :requests, extend: Request::MassAssignEnum
|
179
|
+
end
|
180
|
+
|
181
|
+
Order.first.requests.respond_to?(:in_cart!) # >> true
|
182
|
+
|
183
|
+
#### Rem2:
|
184
|
+
You can mass-assign more than one enum ::MassAssignEnum module will contain mass assign for both. It will break nothing since all enum name must be uniq across model
|
185
|
+
|
186
|
+
## Tests
|
187
|
+
Right now goes without automated tests :(
|
188
|
+
|
189
|
+
## Development
|
190
|
+
|
191
|
+
|
192
|
+
## Contributing
|
193
|
+
|
194
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alekseyl/enum_ext or by email: leshchuk@gmail.com
|
195
|
+
|
196
|
+
|
197
|
+
## License
|
198
|
+
|
199
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
200
|
+
|
data/Rakefile
ADDED
data/enum_ext.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_ext/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enum_ext"
|
8
|
+
spec.version = EnumExt::VERSION
|
9
|
+
spec.authors = ["alekseyl"]
|
10
|
+
spec.email = ["leshchuk@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enum extension, ads enum sets, mass-assign for them, localization for them.}
|
13
|
+
spec.description = %q{Enum extension, ads enum sets, mass-assign for them, localization for them.}
|
14
|
+
spec.homepage = "https://github.com/alekseyl/enum_ext"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "activerecord", ">=4.1"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
data/lib/enum_ext.rb
CHANGED
metadata
CHANGED
@@ -1,23 +1,75 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- alekseyl
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
11
|
date: 2016-02-22 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Enum extension, ads enum sets, mass-assign for them, localization for
|
56
|
+
them.
|
57
|
+
email:
|
58
|
+
- leshchuk@gmail.com
|
15
59
|
executables: []
|
16
60
|
extensions: []
|
17
61
|
extra_rdoc_files: []
|
18
62
|
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- enum_ext.gemspec
|
19
70
|
- lib/enum_ext.rb
|
20
|
-
|
71
|
+
- lib/enum_ext/version.rb
|
72
|
+
homepage: https://github.com/alekseyl/enum_ext
|
21
73
|
licenses:
|
22
74
|
- MIT
|
23
75
|
metadata: {}
|
@@ -40,5 +92,5 @@ rubyforge_project:
|
|
40
92
|
rubygems_version: 2.5.1
|
41
93
|
signing_key:
|
42
94
|
specification_version: 4
|
43
|
-
summary: Enum
|
95
|
+
summary: Enum extension, ads enum sets, mass-assign for them, localization for them.
|
44
96
|
test_files: []
|