enum_ext 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f27d12cb86814ebce65a02477dd28e5ca255fef
4
- data.tar.gz: 5cdcca46d7df13ab534352b74f70f0d8899446db
3
+ metadata.gz: 73fe8bd90c315b4dd9137b4d9b3781fa22e57285
4
+ data.tar.gz: eb4ad8bd68d1372af63768d434ed8c1162ee6bcd
5
5
  SHA512:
6
- metadata.gz: c89e5517d0cc30afb3c754bc29e6aebc32eda62437f440f17cc32da3daa5abf797c7cdd605a8ec87121c8fd8daaa773b5fd64f6d48d0d7ec899d9e4a605add4e
7
- data.tar.gz: 1023069a48375dd626f6aa63b9a9b53d40e7dadd7ba71add171447274bfa76b51d55f0af90baf2d4baffd182d8b7043789ff3261dbe6db4326d1bb2b871c624b
6
+ metadata.gz: 3fdf5c58af028ad822e668490496b04180877ad0409d513d00fdfbe6ccf6516a6e4a41caa21050cbc734711fe0492383a7d34cc625670d863f5a5e416edf6ddd
7
+ data.tar.gz: 547c4903f962ae891c3495c24a1c17090e14d78007c6a74a57c305aae36b838020c9ecfaa270a045f7d048c7234788679cc9c3f5790be3d546bdf53d33b246d7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enum_ext (0.1.1)
4
+ enum_ext (0.1.2)
5
5
  activerecord (>= 4.1)
6
6
 
7
7
  GEM
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
- Let's assume we have model Request with enum *status*, and we have model Order with requests like this:
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 and so:
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
- #class scopes: non_payed, delivery_set, in_warehouse
88
- #class scopes: with_statuses, without_statuses
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
- Request.respond_to?('::MassAssignEnum') # >> true
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 works without 'scope' part!** If you want to use it without 'scope' you may do it this way:
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
@@ -1,3 +1,3 @@
1
1
  module EnumExt
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
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
- ( t.try(:arity) == 1 && t.call( self ) || t.try(:call) || t).to_s
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.2
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-02-22 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord