labelizer 0.0.1 → 0.0.2
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 +8 -8
- data/README.md +17 -27
- data/lib/labelizer.rb +12 -22
- data/lib/labelizer/version.rb +1 -1
- data/version.txt +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NjdiNzMyY2I5YzkzZTI4NDcwMmQ4Y2Q2YzM0ODM5MWIzZWZjZjRlZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmQyMDAwZjViOTAwMThiMGJlZDgyMGQwZmFlZmE2M2U5MWMwZWU0MQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Mjc0ZjFjODY5OWNiNmEzYzNlYjExZDg5MzllNTA1NDk5MDc5YmQ2NWE1OGNj
|
10
|
+
YzMwYTFjYzFhOTcxMDhhNmU3MGQ3ZDdiZDJkMzJlZmRlMjJhMzRhMWU1YmIw
|
11
|
+
Mjc3ZjU1YjA1ODU3NGYzYmNkNDA0NjA0NGI3ZDk5ZmVmODhmNjg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTNiOTIyZmMxYzcwZDkyMDIzN2UyMGRmZDdhNmI4NDc2YWM5ZmViMjlmN2Jj
|
14
|
+
NjA3Y2MxOWMwYzViNmRmNzQ2Zjc5NDQ2Mjk4NDNiNDM1M2Q2OGVmYjZmMTAy
|
15
|
+
MzM3N2E5ZmYyMTc3YmM0ZDQyOTdkZmM4MDM1OGEzODkzYzJiNGE=
|
data/README.md
CHANGED
@@ -24,9 +24,17 @@ Or install it yourself as:
|
|
24
24
|
## Usage
|
25
25
|
|
26
26
|
```ruby
|
27
|
-
#
|
28
|
-
|
29
|
-
|
27
|
+
# app/models/my_model.rb
|
28
|
+
class Customer < ApplicationModel
|
29
|
+
enum registration_state: {
|
30
|
+
starting: 0,
|
31
|
+
confirming: 1,
|
32
|
+
completed: 2,
|
33
|
+
}
|
34
|
+
|
35
|
+
include Labelizer
|
36
|
+
|
37
|
+
labelize :registration_state, %w(label description color)
|
30
38
|
end
|
31
39
|
```
|
32
40
|
|
@@ -50,21 +58,6 @@ ja:
|
|
50
58
|
color: label label-success
|
51
59
|
```
|
52
60
|
|
53
|
-
```ruby
|
54
|
-
# app/models/my_model.rb
|
55
|
-
class Customer < ApplicationModel
|
56
|
-
enum registration_state: {
|
57
|
-
starting: 0,
|
58
|
-
confirming: 1,
|
59
|
-
completed: 2,
|
60
|
-
}
|
61
|
-
|
62
|
-
include Labelizer
|
63
|
-
|
64
|
-
labelize :registration_state
|
65
|
-
end
|
66
|
-
```
|
67
|
-
|
68
61
|
```erb
|
69
62
|
# app/views/customers/show.html.erb
|
70
63
|
<% customer = Customer.find id %>
|
@@ -93,15 +86,12 @@ end
|
|
93
86
|
Convert label:
|
94
87
|
|
95
88
|
```ruby
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
},
|
103
|
-
}
|
104
|
-
end
|
89
|
+
labelize :registration_state, %w(label description color), converter: {
|
90
|
+
color: ->(value){
|
91
|
+
# value : attribute value
|
92
|
+
"label label-#{value}"
|
93
|
+
},
|
94
|
+
}
|
105
95
|
```
|
106
96
|
|
107
97
|
```yaml
|
data/lib/labelizer.rb
CHANGED
@@ -3,39 +3,29 @@ require "i18n"
|
|
3
3
|
require "labelizer/version"
|
4
4
|
|
5
5
|
module Labelizer
|
6
|
-
class << self
|
7
|
-
def configure
|
8
|
-
yield config
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def config
|
14
|
-
@config ||= OpenStruct.new
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
6
|
def self.included(base)
|
19
7
|
base.send :extend, ClassMethods
|
20
8
|
end
|
21
9
|
|
22
10
|
module ClassMethods
|
23
|
-
def labelize(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
11
|
+
def labelize(attr, labels, converter: {})
|
12
|
+
@labelizer_converters ||= {}
|
13
|
+
@labelizer_converters[attr.to_sym] = converter
|
14
|
+
|
15
|
+
define_method :"#{attr}_labelized" do
|
16
|
+
self.class.labelized[attr][__send__(attr)]
|
17
|
+
end
|
28
18
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
19
|
+
labels.each do |label|
|
20
|
+
define_method :"#{attr}_#{label}" do
|
21
|
+
self.class.labelized[attr][__send__(attr)][label]
|
33
22
|
end
|
34
23
|
end
|
35
24
|
end
|
36
25
|
def labelized
|
37
26
|
model = model_name.i18n_key
|
38
27
|
@labelized ||= Hash.new{|h,attr|
|
28
|
+
converters = @labelizer_converters && @labelizer_converters[attr.to_sym]
|
39
29
|
h[attr] = Hash.new{|h,value|
|
40
30
|
h[value] = Hash.new{|h,label|
|
41
31
|
result = ::I18n.translate(
|
@@ -48,7 +38,7 @@ module Labelizer
|
|
48
38
|
],
|
49
39
|
raise: true,
|
50
40
|
) rescue value
|
51
|
-
if converter =
|
41
|
+
if converter = converters && converters[label.to_sym]
|
52
42
|
result = converter[result]
|
53
43
|
end
|
54
44
|
h[value] = result
|
data/lib/labelizer/version.rb
CHANGED
data/version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|