enum_ext 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +26 -14
- data/lib/enum_ext/version.rb +1 -1
- data/lib/enum_ext.rb +11 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73fe8bd90c315b4dd9137b4d9b3781fa22e57285
|
4
|
+
data.tar.gz: eb4ad8bd68d1372af63768d434ed8c1162ee6bcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fdf5c58af028ad822e668490496b04180877ad0409d513d00fdfbe6ccf6516a6e4a41caa21050cbc734711fe0492383a7d34cc625670d863f5a5e416edf6ddd
|
7
|
+
data.tar.gz: 547c4903f962ae891c3495c24a1c17090e14d78007c6a74a57c305aae36b838020c9ecfaa270a045f7d048c7234788679cc9c3f5790be3d546bdf53d33b246d7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -19,8 +19,17 @@ Or install it yourself as:
|
|
19
19
|
$ gem install enum_ext
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
|
23
|
-
|
22
|
+
To use enum extension extend main model class with EnumExt module, and extend enum the way you need:
|
23
|
+
|
24
|
+
class SomeModel
|
25
|
+
extend EnumExt
|
26
|
+
|
27
|
+
localize_enum ...
|
28
|
+
ext_enum_sets ...
|
29
|
+
mass_assign_enum ...
|
30
|
+
end
|
31
|
+
|
32
|
+
Let's assume that we have model Request representing some buying requests with enum **status**, and we have model Order with requests, representing single purchase, like this:
|
24
33
|
|
25
34
|
class Request
|
26
35
|
extend EnumExt
|
@@ -43,9 +52,12 @@ Or install it yourself as:
|
|
43
52
|
#locale dependent example ( it dynamically use current locale ):
|
44
53
|
in_cart: -> { I18n.t("request.status.in_cart") },
|
45
54
|
|
46
|
-
#locale dependent example with internal pluralization:
|
55
|
+
#locale dependent example with internal pluralization and lambda:
|
47
56
|
payed: -> (t_self) { I18n.t("request.status.payed", count: t_self.sum ) }
|
48
|
-
|
57
|
+
|
58
|
+
#locale dependent example with internal pluralization and proc:
|
59
|
+
payed: proc { I18n.t("request.status.payed", count: sum ) }
|
60
|
+
|
49
61
|
#locale independent:
|
50
62
|
ready_for_shipment: "Ready to go!"
|
51
63
|
}
|
@@ -68,7 +80,7 @@ If you need some substitution you can go like this:
|
|
68
80
|
request.delivered!
|
69
81
|
request.t_status % {date: Time.now.to_s} >> Delivered at: 05.02.2016
|
70
82
|
|
71
|
-
If you need select status on form
|
83
|
+
If you need select status on form:
|
72
84
|
|
73
85
|
f.select :status, Request.t_statuses.invert.to_a
|
74
86
|
|
@@ -83,15 +95,15 @@ If you need select status on form and so:
|
|
83
95
|
|
84
96
|
class Request
|
85
97
|
...
|
86
|
-
#instance: non_payed?, delivery_set?, in_warehouse?
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#class: non_payed_statuses, delivery_set_statuses ( = [:in_cart, :waiting_for_payment], [:ready_for_shipment, :on_delivery, :delivered].. )
|
98
|
+
#instance methods: non_payed?, delivery_set?, in_warehouse?
|
99
|
+
#scopes: non_payed, delivery_set, in_warehouse
|
100
|
+
#scopes: with_statuses, without_statuses
|
101
|
+
#class methods: non_payed_statuses, delivery_set_statuses ( = [:in_cart, :waiting_for_payment], [:ready_for_shipment, :on_delivery, :delivered].. )
|
90
102
|
|
91
103
|
ext_enum_sets :status, {
|
92
104
|
non_payed: [:in_cart, :waiting_for_payment],
|
93
|
-
delivery_set: [:ready_for_shipment, :on_delivery, :delivered] for shipping department for example
|
94
|
-
in_warehouse: [:ready_for_shipment] it's just for example below
|
105
|
+
delivery_set: [:ready_for_shipment, :on_delivery, :delivered] #for shipping department for example
|
106
|
+
in_warehouse: [:ready_for_shipment] #it's just for example below
|
95
107
|
}
|
96
108
|
end
|
97
109
|
|
@@ -142,7 +154,7 @@ You can call ext_enum_sets more than one time defining a superposition of alread
|
|
142
154
|
request1.payed? # >> true
|
143
155
|
request2.payed? # >> true
|
144
156
|
request1.updated_at # >> ~ Time.now
|
145
|
-
|
157
|
+
defined?(Request::MassAssignEnum) # >> true
|
146
158
|
|
147
159
|
|
148
160
|
order.requests.already_payed.count # >> N
|
@@ -168,11 +180,11 @@ You can call ext_enum_sets more than one time defining a superposition of alread
|
|
168
180
|
|
169
181
|
association_relation: true - Order.first.requests.scope.new_stat! - works
|
170
182
|
|
171
|
-
**but it wouldn't
|
183
|
+
**but it wouldn't work without 'scope' part!** If you want to use it without 'scope' you may do it this way:
|
172
184
|
|
173
185
|
class Request
|
174
186
|
...
|
175
|
-
mass_assign_enum( :status, association_relation: false )
|
187
|
+
mass_assign_enum( :status, relation: true, association_relation: false )
|
176
188
|
end
|
177
189
|
|
178
190
|
class Order
|
data/lib/enum_ext/version.rb
CHANGED
data/lib/enum_ext.rb
CHANGED
@@ -20,8 +20,11 @@ module EnumExt
|
|
20
20
|
# #locale dependent example ( it dynamically use current locale ):
|
21
21
|
# in_cart: -> { I18n.t("request.status.in_cart") },
|
22
22
|
|
23
|
-
# #locale dependent example with pluralization:
|
23
|
+
# #locale dependent example with pluralization and lambda:
|
24
24
|
# payed: -> (t_self) { I18n.t("request.status.payed", count: t_self.sum ) }
|
25
|
+
|
26
|
+
# #locale dependent example with pluralization and proc:
|
27
|
+
# payed: proc{ I18n.t("request.status.payed", count: self.sum ) }
|
25
28
|
#
|
26
29
|
# #locale independent:
|
27
30
|
# ready_for_shipment: "Ready to go!"
|
@@ -55,7 +58,13 @@ module EnumExt
|
|
55
58
|
end
|
56
59
|
define_method "t_#{enum_name}" do
|
57
60
|
t = localizations[send(enum_name)]
|
58
|
-
|
61
|
+
if t.lambda?
|
62
|
+
t.try(:arity) == 1 && t.call( self ) || t.try(:call)
|
63
|
+
elsif t.is_a?(Proc)
|
64
|
+
instance_eval(&t)
|
65
|
+
else
|
66
|
+
t
|
67
|
+
end.to_s
|
59
68
|
end
|
60
69
|
end
|
61
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alekseyl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|