enum_ext 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enum_ext/version.rb +1 -1
- data/lib/enum_ext.rb +18 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e95f6a4c82487df31be4eb7e31a4f310292c590
|
4
|
+
data.tar.gz: 367ea13f066e7a7fe7b00ad790d36983a5b6e000
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8032fae12c639e80edfdf9940da399660cb3f9d0c64f096d8a8ae33d8139eb250071bbc8dc4e97ebfdfc1a041196b8fcd13df3fde45f9867d9932eaecc53ab20
|
7
|
+
data.tar.gz: d82b610d27f075c96d94e3457338ba394710e94296ac5e31c947423eb00accfc1a02f6e0123948cf782d8250d24b07d42ba9679817b5a5de147494e53fa6539a
|
data/lib/enum_ext/version.rb
CHANGED
data/lib/enum_ext.rb
CHANGED
@@ -61,12 +61,14 @@ module EnumExt
|
|
61
61
|
# outside_wharehouse: ( delivery_set_statuses - in_warehouse_statuses )... any other array operations like &, + and so can be used
|
62
62
|
# }
|
63
63
|
def ext_enum_sets( enum_name, options )
|
64
|
+
enum_plural = enum_name.to_s.pluralize
|
65
|
+
|
64
66
|
self.instance_eval do
|
65
67
|
options.each do |set_name, enum_vals|
|
66
|
-
scope set_name, -> { where( enum_name => self.send(
|
68
|
+
scope set_name, -> { where( enum_name => self.send( enum_plural ).slice( *enum_vals.map(&:to_s) ).values ) }
|
67
69
|
|
68
70
|
|
69
|
-
define_singleton_method( "#{set_name}_#{
|
71
|
+
define_singleton_method( "#{set_name}_#{enum_plural}" ) do
|
70
72
|
enum_vals
|
71
73
|
end
|
72
74
|
|
@@ -76,33 +78,33 @@ module EnumExt
|
|
76
78
|
end
|
77
79
|
|
78
80
|
# t_set_enums
|
79
|
-
define_singleton_method( "t_#{set_name}_#{
|
80
|
-
send( "t_#{
|
81
|
+
define_singleton_method( "t_#{set_name}_#{enum_plural}" ) do
|
82
|
+
send( "t_#{enum_plural}" ).slice( *self.send("#{set_name}_#{enum_plural}") )
|
81
83
|
end
|
82
84
|
|
83
85
|
# t_set_enums_options
|
84
|
-
define_singleton_method( "t_#{set_name}_#{
|
85
|
-
send( "t_#{set_name}_#{
|
86
|
+
define_singleton_method( "t_#{set_name}_#{enum_plural}_options" ) do
|
87
|
+
send( "t_#{set_name}_#{enum_plural}" ).invert.to_a.map do | key_val |
|
86
88
|
key_val[0] = key_val[0].call if key_val[0].respond_to?(:call) && key_val[0].try(:arity) < 1
|
87
89
|
key_val
|
88
90
|
end
|
89
91
|
end
|
90
92
|
|
91
93
|
# set_enums_i
|
92
|
-
define_singleton_method( "#{set_name}_#{
|
93
|
-
self.send( "#{
|
94
|
+
define_singleton_method( "#{set_name}_#{enum_plural}_i" ) do
|
95
|
+
self.send( "#{enum_plural}" ).slice( *self.send("#{set_name}_#{enum_plural}") ).values
|
94
96
|
end
|
95
97
|
|
96
98
|
end
|
97
99
|
|
98
|
-
scope "with_#{
|
99
|
-
where( enum_name => self.send(
|
100
|
-
*sets_arr.map{|set_name| self.try( "#{set_name}_#{
|
101
|
-
} unless respond_to?("with_#{
|
100
|
+
scope "with_#{enum_plural}", -> (sets_arr) {
|
101
|
+
where( enum_name => self.send( enum_plural ).slice(
|
102
|
+
*sets_arr.map{|set_name| self.try( "#{set_name}_#{enum_plural}" ) || set_name }.flatten.uniq.map(&:to_s) ).values )
|
103
|
+
} unless respond_to?("with_#{enum_plural}")
|
102
104
|
|
103
|
-
scope "without_#{
|
104
|
-
where.not( id: self.send("with_#{
|
105
|
-
} unless respond_to?("without_#{
|
105
|
+
scope "without_#{enum_plural}", -> (sets_arr) {
|
106
|
+
where.not( id: self.send("with_#{enum_plural}", sets_arr) )
|
107
|
+
} unless respond_to?("without_#{enum_plural}")
|
106
108
|
end
|
107
109
|
end
|
108
110
|
|
@@ -230,7 +232,7 @@ module EnumExt
|
|
230
232
|
define_singleton_method( "t_#{enum_pural}" ) do
|
231
233
|
# if localization is abscent than block must be given
|
232
234
|
localizations.try(:with_indifferent_access) || localizations ||
|
233
|
-
send(enum_pural).keys.map {|en| [en, self.new( {enum_name => en} ).send("t_#{enum_name}")] }.to_h
|
235
|
+
send(enum_pural).keys.map {|en| [en, self.new( {enum_name => en} ).send("t_#{enum_name}")] }.to_h.with_indifferent_access
|
234
236
|
end
|
235
237
|
|
236
238
|
#t_enums_options
|