vinber 0.1.0 → 0.1.1
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/README.md +68 -19
- data/lib/vinber/version.rb +1 -1
- data/lib/vinber.rb +49 -2
- 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: 5846099a1282c804d8caee37689c761e2c46e8f4
|
4
|
+
data.tar.gz: b06ec6b47bb81c6e8349064513aae54b9e8ef2aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40dca19b242327e0c29915d62a528a1983f5b632d7ae6667d359809045402eb30f80351ac2549608fd6d8b69bbe7eb0e36c1a0a72abeb1c4ef7d81a05d6ab0de
|
7
|
+
data.tar.gz: 4a73b9e8f2b025307a0a351817a6c9206b0757eb1872218270e618b71b0d21cee1af459f00f59767795e4270c0a50c7f499fe1d5f534b88b3497484529a19ac8
|
data/README.md
CHANGED
@@ -1,41 +1,90 @@
|
|
1
|
-
|
1
|
+
## Vinber ##
|
2
2
|
|
3
|
-
|
3
|
+
Vinber works for Rails, provides other way enumeration style which without force format output and force method definations.
|
4
4
|
|
5
|
-
|
5
|
+
__NOTE__: Vinber current version *require* Ruby v1.9.0 or later and just tested on Rails 4.x for now .
|
6
6
|
|
7
|
-
## Installation
|
8
7
|
|
9
|
-
|
8
|
+
### Installation ###
|
9
|
+
# Installing as Ruby gem
|
10
|
+
$ gem install vinber
|
11
|
+
|
12
|
+
# Or in gemfile
|
13
|
+
$ gem vinber
|
10
14
|
|
15
|
+
### Usage ###
|
16
|
+
|
17
|
+
#### vinber
|
18
|
+
You will be able to use `vinber` in any model which inherit `ActiveRecord::Base`.
|
11
19
|
```ruby
|
12
|
-
|
20
|
+
vinber attribute: {} # hash
|
21
|
+
vinber attribute: [] # array
|
13
22
|
```
|
14
23
|
|
15
|
-
|
24
|
+
### vinber_list
|
25
|
+
`vinber_list` return a nested Array, it besides the attribute I18n value and vinber key.
|
26
|
+
```ruby
|
27
|
+
vinber_list class_object, attribute
|
28
|
+
```
|
16
29
|
|
17
|
-
|
30
|
+
### vinber_value
|
31
|
+
`vinber_value` return the I18n value of specific attribute.
|
18
32
|
|
19
|
-
|
33
|
+
```ruby
|
34
|
+
vinber_value instance_object, attribute
|
35
|
+
```
|
20
36
|
|
21
|
-
|
37
|
+
### Examples ###
|
38
|
+
Step 1, you need define your vinbers in your model.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# app/models/your_models.rb
|
42
|
+
class YourModel < ActiveRecord::Base
|
43
|
+
...
|
22
44
|
|
23
|
-
|
45
|
+
vinber :status => {:submited => 1, :processing => 2, :completed => 3}
|
24
46
|
|
25
|
-
|
47
|
+
...
|
48
|
+
end
|
49
|
+
```
|
26
50
|
|
27
|
-
|
51
|
+
Step 2, set your locale for I18n.
|
52
|
+
```yaml
|
53
|
+
# config/locales/zh.yml or en.yml(any yml your want)
|
54
|
+
zh:
|
55
|
+
vinber:
|
56
|
+
YourModel:
|
57
|
+
status_submited: 提交
|
58
|
+
status_processing: 进行中
|
59
|
+
status_completed: 完成
|
28
60
|
|
29
|
-
|
61
|
+
```
|
30
62
|
|
31
|
-
|
63
|
+
Step 3, invoking in view !! We assume you has YourModel and it contains records, the status of first record is `2`.
|
32
64
|
|
33
|
-
|
65
|
+
```haml
|
66
|
+
# app/views/your_modles/index.haml
|
67
|
+
- obj = YourModel.first
|
68
|
+
%p= vinber_value(obj, :status)
|
34
69
|
|
35
|
-
|
70
|
+
# => <p>进行中</p>
|
36
71
|
|
72
|
+
= form_tag({}) do
|
73
|
+
= select_tag "status", options_for_select(vinber_list YourModel, :status)
|
37
74
|
|
38
|
-
|
75
|
+
# => <form accept-charset="UTF-8" method="post">
|
76
|
+
# => <select name="status" id="status">
|
77
|
+
# => <option value="1">提交</option>
|
78
|
+
# => <option value="2">进行中</option>
|
79
|
+
# => <option value="3">完成</option>
|
80
|
+
# => </select>
|
81
|
+
# => </form>
|
82
|
+
```
|
39
83
|
|
40
|
-
|
84
|
+
### TODO ###
|
85
|
+
- Write more clear document
|
86
|
+
- Add soft warning when error
|
87
|
+
- more functions
|
41
88
|
|
89
|
+
### License ###
|
90
|
+
Released under the [MIT](http://opensource.org/licenses/MIT) license. See LICENSE file for details.
|
data/lib/vinber/version.rb
CHANGED
data/lib/vinber.rb
CHANGED
@@ -1,5 +1,52 @@
|
|
1
|
-
|
1
|
+
require_relative 'vinber/version'
|
2
|
+
require_relative 'vinber/translate'
|
3
|
+
require_relative 'vinber/vinber_list'
|
4
|
+
require_relative 'vinber/vinber_value'
|
5
|
+
require_relative 'vinber/current_vinbers'
|
2
6
|
|
3
7
|
module Vinber
|
4
|
-
|
8
|
+
class VinberUndefined < StandardError; end
|
9
|
+
|
10
|
+
def self.extended(base)
|
11
|
+
base.class_attribute(:defined_vinbers, instance_writer: false)
|
12
|
+
base.defined_vinbers = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def inherited(base)
|
16
|
+
base.defined_vinbers = defined_vinbers.deep_dup
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def vinber(definitions)
|
21
|
+
definitions.each do |name, values|
|
22
|
+
defined_vinbers[name.to_s] = case
|
23
|
+
when values.is_a?(Hash)
|
24
|
+
values.with_indifferent_access
|
25
|
+
when values.is_a?(Array)
|
26
|
+
values
|
27
|
+
else
|
28
|
+
Array.wrap values.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def vinbers
|
34
|
+
Vinber::CurrentVinbers.new defined_vinbers
|
35
|
+
end
|
36
|
+
|
37
|
+
def vinber_defined?(attr_key = nil)
|
38
|
+
attr_key ? defined_vinbers.has_key?(attr_key.to_s) : defined_vinbers.present?
|
39
|
+
end
|
5
40
|
end
|
41
|
+
|
42
|
+
# Extend/Include to Rails
|
43
|
+
ActiveRecord::Base.extend Vinber
|
44
|
+
ActiveRecord::Base.extend Vinber::Value
|
45
|
+
ActiveRecord::Base.send(:include, Vinber::Value)
|
46
|
+
|
47
|
+
ApplicationHelper.extend Vinber::Value
|
48
|
+
ApplicationHelper.send(:include, Vinber::Value)
|
49
|
+
ApplicationHelper.send(:include, Vinber::List)
|
50
|
+
|
51
|
+
ApplicationController.extend Vinber::Value
|
52
|
+
ApplicationController.send(:include, Vinber::Value)
|