lists_constant 0.1.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +114 -0
- data/Rakefile +7 -0
- data/lib/lists_constant/version.rb +3 -0
- data/lib/lists_constant.rb +63 -0
- data/lists_constant.gemspec +23 -0
- data/test/lib/lists_constant/lists_constant_test.rb +115 -0
- data/test/lib/lists_constant/version_test.rb +8 -0
- data/test/test_helper.rb +2 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Andrew Horner
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# ListsConstant
|
|
2
|
+
|
|
3
|
+
ListsConstant is a module which allows you to easily define
|
|
4
|
+
lists of constant values. I18n is used to translate the listed
|
|
5
|
+
constants into readable values.
|
|
6
|
+
|
|
7
|
+
This library is intended to make it simple to keep view-specific
|
|
8
|
+
information (like the text representations of your listed values)
|
|
9
|
+
out of your model classes.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
gem 'lists-constant'
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install lists-constant
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
|
|
29
|
+
In `my_state_machine.rb`:
|
|
30
|
+
```
|
|
31
|
+
class MyStateMachine
|
|
32
|
+
include ListsConstant
|
|
33
|
+
lists_constant :first, :second, :third, as: :steps
|
|
34
|
+
|
|
35
|
+
attr_accessor :step
|
|
36
|
+
def initialize(step)
|
|
37
|
+
@step = step
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
In `locales/en.yml`:
|
|
43
|
+
```
|
|
44
|
+
en:
|
|
45
|
+
my_state_machine:
|
|
46
|
+
steps:
|
|
47
|
+
first: Initialize
|
|
48
|
+
second: Validate
|
|
49
|
+
third: Save
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
In `locales/es.yml`:
|
|
53
|
+
```
|
|
54
|
+
es:
|
|
55
|
+
my_state_machine:
|
|
56
|
+
steps:
|
|
57
|
+
first: Inicie
|
|
58
|
+
second: Valide
|
|
59
|
+
third: Guarde
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Using the generated constant:
|
|
63
|
+
```
|
|
64
|
+
MyStateMachine::STEPS
|
|
65
|
+
# => [:first, :second, :third]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Using class-level localization:
|
|
69
|
+
```
|
|
70
|
+
I18n.locale = :en
|
|
71
|
+
MyStateMachine.steps[:first]
|
|
72
|
+
# => 'Initialize'
|
|
73
|
+
|
|
74
|
+
MyStateMachine.step_options
|
|
75
|
+
# => {
|
|
76
|
+
'Initialize' => :first,
|
|
77
|
+
'Validate' => :second,
|
|
78
|
+
'Save' => :third
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
I18n.locale = :es
|
|
82
|
+
MyStateMachine.steps[:first]
|
|
83
|
+
# => 'Inicie'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Instance query methods:
|
|
87
|
+
```
|
|
88
|
+
msm = MyStateMachine.new(:second)
|
|
89
|
+
|
|
90
|
+
msm.step_second?
|
|
91
|
+
# => true
|
|
92
|
+
|
|
93
|
+
msm.step_third?
|
|
94
|
+
# => false
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Using instance-level localization:
|
|
98
|
+
```
|
|
99
|
+
I18n.locale = :en
|
|
100
|
+
msm.localized_step
|
|
101
|
+
# => 'Validate'
|
|
102
|
+
|
|
103
|
+
I18n.locale = :es
|
|
104
|
+
msm.localized_step
|
|
105
|
+
# => 'Valide'
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Contributing
|
|
109
|
+
|
|
110
|
+
1. Fork it
|
|
111
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
112
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
113
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
114
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require_relative './lists_constant/version'
|
|
2
|
+
require 'active_support/inflector'
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
|
|
5
|
+
module ListsConstant
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
def self.namespace
|
|
9
|
+
"#{@namespace}." if @namespace
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.namespace=(namespace)
|
|
13
|
+
@namespace = namespace
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module ClassMethods
|
|
17
|
+
|
|
18
|
+
def lists_constant(*values)
|
|
19
|
+
options = values.extract_options!
|
|
20
|
+
|
|
21
|
+
field = options[:as].to_s
|
|
22
|
+
raise ArgumentError.new('A constant name must be provided using the :as option') if field.empty?
|
|
23
|
+
|
|
24
|
+
const_set(field.upcase, values.freeze)
|
|
25
|
+
add_constant_list_getters(field)
|
|
26
|
+
add_localized_lookups(field)
|
|
27
|
+
add_query_methods(field, values)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def add_constant_list_getters(field)
|
|
34
|
+
define_singleton_method field do
|
|
35
|
+
const_get(field.upcase).inject({}) do |hash, value|
|
|
36
|
+
hash[value] = I18n.t(value, scope: "#{ListsConstant.namespace}#{name.underscore}.#{field}")
|
|
37
|
+
hash
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
define_singleton_method "#{field.singularize}_options" do
|
|
42
|
+
send(field).invert
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def add_localized_lookups(field)
|
|
47
|
+
define_method "localized_#{field.singularize}" do
|
|
48
|
+
value = send(field.singularize)
|
|
49
|
+
return nil if value.nil? || value.empty?
|
|
50
|
+
|
|
51
|
+
I18n.t(value, scope: "#{ListsConstant.namespace}#{self.class.name.underscore}.#{field}")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def add_query_methods(field, values)
|
|
56
|
+
values.each do |value|
|
|
57
|
+
define_method "#{field.singularize}_#{value}?" do
|
|
58
|
+
send(field.singularize).to_s == value.to_s
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/lists_constant/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Andrew Horner"]
|
|
6
|
+
gem.email = ["andrew@tablexi.com"]
|
|
7
|
+
gem.description = %q{Easily create localization-friendly constant lists}
|
|
8
|
+
gem.summary = %q{
|
|
9
|
+
ListsConstant supplies a module which allows easy definition of lists of
|
|
10
|
+
constant values for a Ruby class. I18n is used to translate the listed
|
|
11
|
+
constants into readable values.
|
|
12
|
+
}
|
|
13
|
+
gem.homepage = ""
|
|
14
|
+
|
|
15
|
+
gem.files = `git ls-files`.split($\)
|
|
16
|
+
gem.test_files = gem.files.grep(%r{^test/})
|
|
17
|
+
gem.name = "lists_constant"
|
|
18
|
+
gem.require_paths = ["lib"]
|
|
19
|
+
gem.version = ListsConstant::VERSION
|
|
20
|
+
|
|
21
|
+
gem.add_dependency('activesupport')
|
|
22
|
+
gem.add_dependency('i18n')
|
|
23
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require_relative '../../test_helper'
|
|
2
|
+
|
|
3
|
+
class Lister
|
|
4
|
+
|
|
5
|
+
attr_accessor :color
|
|
6
|
+
|
|
7
|
+
def initialize(color)
|
|
8
|
+
@color = color
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe ListsConstant do
|
|
13
|
+
before do
|
|
14
|
+
I18n.locale = :en
|
|
15
|
+
Lister.send :include, ListsConstant
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "requires a name for the constant list" do
|
|
19
|
+
assert_raises ArgumentError do
|
|
20
|
+
Lister.lists_constant :red, :blue, :yellow
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "creating a constant list" do
|
|
25
|
+
before do
|
|
26
|
+
Lister.lists_constant :red,
|
|
27
|
+
:blue,
|
|
28
|
+
:yellow,
|
|
29
|
+
as: :colors
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
after do
|
|
33
|
+
Lister.send :remove_const, 'COLORS'
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "adds the constant to the includer" do
|
|
37
|
+
assert_equal Lister::COLORS, [:red, :blue, :yellow]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "adds instance query methods for each value" do
|
|
41
|
+
listed = Lister.new(:red)
|
|
42
|
+
|
|
43
|
+
assert listed.color_red?
|
|
44
|
+
refute listed.color_yellow?
|
|
45
|
+
refute listed.color_blue?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe "given I18n translations" do
|
|
49
|
+
before do
|
|
50
|
+
@translations = {
|
|
51
|
+
red: 'rojo',
|
|
52
|
+
blue: 'azul',
|
|
53
|
+
yellow: 'amarillo'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
I18n.backend.store_translations :es, {
|
|
57
|
+
lister: {
|
|
58
|
+
colors: @translations
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
I18n.locale = :es
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "generates a hash of the localized names with their values" do
|
|
66
|
+
assert_equal Lister.colors, {
|
|
67
|
+
red: @translations[:red],
|
|
68
|
+
blue: @translations[:blue],
|
|
69
|
+
yellow: @translations[:yellow]
|
|
70
|
+
}
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "generates an inverted hash for option helpers" do
|
|
74
|
+
assert_equal Lister.color_options, {
|
|
75
|
+
@translations[:red] => :red,
|
|
76
|
+
@translations[:blue] => :blue,
|
|
77
|
+
@translations[:yellow] => :yellow
|
|
78
|
+
}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "localizes the variable description" do
|
|
82
|
+
listed = Lister.new(:red)
|
|
83
|
+
assert_equal listed.localized_color, @translations[:red]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe "with a namespace" do
|
|
87
|
+
before do
|
|
88
|
+
ListsConstant.namespace = :mistranslations
|
|
89
|
+
|
|
90
|
+
@mistranslations = {
|
|
91
|
+
red: 'sangriento',
|
|
92
|
+
blue: 'morado',
|
|
93
|
+
yellow: 'gallina'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
I18n.backend.store_translations :es, {
|
|
97
|
+
mistranslations: {
|
|
98
|
+
lister: {
|
|
99
|
+
colors: @mistranslations
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "scopes within the namespace for localization" do
|
|
106
|
+
assert_equal Lister.colors, {
|
|
107
|
+
red: @mistranslations[:red],
|
|
108
|
+
blue: @mistranslations[:blue],
|
|
109
|
+
yellow: @mistranslations[:yellow]
|
|
110
|
+
}
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lists_constant
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Andrew Horner
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: activesupport
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: i18n
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :runtime
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
description: Easily create localization-friendly constant lists
|
|
47
|
+
email:
|
|
48
|
+
- andrew@tablexi.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- .gitignore
|
|
54
|
+
- Gemfile
|
|
55
|
+
- LICENSE
|
|
56
|
+
- README.md
|
|
57
|
+
- Rakefile
|
|
58
|
+
- lib/lists_constant.rb
|
|
59
|
+
- lib/lists_constant/version.rb
|
|
60
|
+
- lists_constant.gemspec
|
|
61
|
+
- test/lib/lists_constant/lists_constant_test.rb
|
|
62
|
+
- test/lib/lists_constant/version_test.rb
|
|
63
|
+
- test/test_helper.rb
|
|
64
|
+
homepage: ''
|
|
65
|
+
licenses: []
|
|
66
|
+
post_install_message:
|
|
67
|
+
rdoc_options: []
|
|
68
|
+
require_paths:
|
|
69
|
+
- lib
|
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
+
none: false
|
|
72
|
+
requirements:
|
|
73
|
+
- - ! '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
none: false
|
|
78
|
+
requirements:
|
|
79
|
+
- - ! '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 1.8.24
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 3
|
|
87
|
+
summary: ListsConstant supplies a module which allows easy definition of lists of
|
|
88
|
+
constant values for a Ruby class. I18n is used to translate the listed constants
|
|
89
|
+
into readable values.
|
|
90
|
+
test_files:
|
|
91
|
+
- test/lib/lists_constant/lists_constant_test.rb
|
|
92
|
+
- test/lib/lists_constant/version_test.rb
|
|
93
|
+
- test/test_helper.rb
|