lists_constant 0.1.1 → 0.2.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/README.md +25 -12
- data/lib/lists_constant/class_methods.rb +15 -0
- data/lib/lists_constant/lookup.rb +14 -0
- data/lib/lists_constant/lookups/class.rb +51 -0
- data/lib/lists_constant/lookups/instance.rb +44 -0
- data/lib/lists_constant/lookups.rb +6 -0
- data/lib/lists_constant/version.rb +1 -1
- data/lib/lists_constant.rb +7 -50
- data/test/fixtures/lister.rb +8 -0
- data/test/lib/lists_constant/class_methods_test.rb +30 -0
- data/test/lib/lists_constant/lookup_test.rb +63 -0
- data/test/lib/lists_constant/lookups/class_test.rb +55 -0
- data/test/lib/lists_constant/lookups/instance_test.rb +41 -0
- data/test/test_helper.rb +2 -0
- metadata +17 -4
- data/test/lib/lists_constant/lists_constant_test.rb +0 -115
data/README.md
CHANGED
|
@@ -12,7 +12,7 @@ out of your model classes.
|
|
|
12
12
|
|
|
13
13
|
Add this line to your application's Gemfile:
|
|
14
14
|
|
|
15
|
-
gem '
|
|
15
|
+
gem 'lists_constant'
|
|
16
16
|
|
|
17
17
|
And then execute:
|
|
18
18
|
|
|
@@ -20,7 +20,7 @@ And then execute:
|
|
|
20
20
|
|
|
21
21
|
Or install it yourself as:
|
|
22
22
|
|
|
23
|
-
$ gem install
|
|
23
|
+
$ gem install lists_constant
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
@@ -69,7 +69,7 @@ MyStateMachine::STEPS
|
|
|
69
69
|
# => [:first, :second, :third]
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
Class-level localization:
|
|
73
73
|
|
|
74
74
|
``` ruby
|
|
75
75
|
I18n.locale = :en
|
|
@@ -78,29 +78,30 @@ MyStateMachine.steps[:first]
|
|
|
78
78
|
|
|
79
79
|
MyStateMachine.step_options
|
|
80
80
|
# => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
# 'Initialize' => :first,
|
|
82
|
+
# 'Validate' => :second,
|
|
83
|
+
# 'Save' => :third
|
|
84
|
+
# }
|
|
85
85
|
|
|
86
86
|
I18n.locale = :es
|
|
87
87
|
MyStateMachine.steps[:first]
|
|
88
88
|
# => 'Inicie'
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
Class query method:
|
|
92
92
|
|
|
93
93
|
``` ruby
|
|
94
|
-
|
|
94
|
+
MyStateMachine.includes_step? :first
|
|
95
|
+
# => true
|
|
95
96
|
|
|
96
|
-
|
|
97
|
+
MyStateMachine.includes_step? 'second'
|
|
97
98
|
# => true
|
|
98
99
|
|
|
99
|
-
|
|
100
|
+
MyStateMachine.includes_step? 'profit!'
|
|
100
101
|
# => false
|
|
101
102
|
```
|
|
102
103
|
|
|
103
|
-
|
|
104
|
+
Instance-level localization:
|
|
104
105
|
|
|
105
106
|
``` ruby
|
|
106
107
|
I18n.locale = :en
|
|
@@ -112,6 +113,18 @@ msm.localized_step
|
|
|
112
113
|
# => 'Valide'
|
|
113
114
|
```
|
|
114
115
|
|
|
116
|
+
Instance query methods:
|
|
117
|
+
|
|
118
|
+
``` ruby
|
|
119
|
+
msm = MyStateMachine.new(:second)
|
|
120
|
+
|
|
121
|
+
msm.step_second?
|
|
122
|
+
# => true
|
|
123
|
+
|
|
124
|
+
msm.step_third?
|
|
125
|
+
# => false
|
|
126
|
+
```
|
|
127
|
+
|
|
115
128
|
Localization lookups may be scoped by assigning a namespace to
|
|
116
129
|
the `ListsConstant` module:
|
|
117
130
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module ListsConstant
|
|
2
|
+
|
|
3
|
+
module ClassMethods
|
|
4
|
+
|
|
5
|
+
def lists_constant *values
|
|
6
|
+
options = values.extract_options!
|
|
7
|
+
field = options[:as].to_s
|
|
8
|
+
raise ArgumentError.new('A constant name must be provided using the :as option') if field.empty?
|
|
9
|
+
|
|
10
|
+
const_set field.upcase, values.freeze
|
|
11
|
+
extend ListsConstant::Lookups::Class.new(field, values)
|
|
12
|
+
include ListsConstant::Lookups::Instance.new(field, values)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module ListsConstant
|
|
2
|
+
|
|
3
|
+
module Lookups
|
|
4
|
+
|
|
5
|
+
class Class < Module
|
|
6
|
+
|
|
7
|
+
def initialize field, values
|
|
8
|
+
@field = field
|
|
9
|
+
@values = values
|
|
10
|
+
|
|
11
|
+
super()
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def extended base
|
|
15
|
+
add_lookup_query_method base
|
|
16
|
+
add_localized_hash_method base
|
|
17
|
+
add_inverted_hash_method base
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def add_lookup_query_method base
|
|
24
|
+
field = @field
|
|
25
|
+
|
|
26
|
+
base.define_singleton_method "includes_#{field.singularize}?" do |value|
|
|
27
|
+
const_get(field.upcase).any? { |item| item.to_s == value.to_s }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def add_localized_hash_method base
|
|
32
|
+
field = @field
|
|
33
|
+
|
|
34
|
+
base.define_singleton_method field do
|
|
35
|
+
const_get(field.upcase).inject({}) do |hash, value|
|
|
36
|
+
hash[value] = ListsConstant::Lookup.new(self, field).lookup(value)
|
|
37
|
+
hash
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def add_inverted_hash_method base
|
|
43
|
+
field = @field
|
|
44
|
+
|
|
45
|
+
base.define_singleton_method "#{field.singularize}_options" do
|
|
46
|
+
send(field).invert
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module ListsConstant
|
|
2
|
+
|
|
3
|
+
module Lookups
|
|
4
|
+
|
|
5
|
+
class Instance < Module
|
|
6
|
+
|
|
7
|
+
def initialize field, values
|
|
8
|
+
@field = field
|
|
9
|
+
@values = values
|
|
10
|
+
|
|
11
|
+
super()
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def included base
|
|
15
|
+
add_localized_lookups base
|
|
16
|
+
add_query_methods base
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def add_localized_lookups base
|
|
23
|
+
field = @field
|
|
24
|
+
|
|
25
|
+
base.send :define_method, "localized_#{field.singularize}" do
|
|
26
|
+
value = send(field.singularize)
|
|
27
|
+
return nil if value.nil? || value.empty?
|
|
28
|
+
|
|
29
|
+
ListsConstant::Lookup.new(self.class, field).lookup(value)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def add_query_methods base
|
|
34
|
+
field = @field
|
|
35
|
+
|
|
36
|
+
@values.each do |value|
|
|
37
|
+
base.send :define_method, "#{field.singularize}_#{value}?" do
|
|
38
|
+
send(field.singularize).to_s == value.to_s
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/lists_constant.rb
CHANGED
|
@@ -1,63 +1,20 @@
|
|
|
1
|
-
require_relative './lists_constant/version'
|
|
2
1
|
require 'active_support/inflector'
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
require 'lists_constant/version'
|
|
4
|
+
require 'lists_constant/lookups'
|
|
5
|
+
require 'lists_constant/class_methods'
|
|
4
6
|
|
|
5
7
|
module ListsConstant
|
|
6
|
-
extend ActiveSupport::Concern
|
|
7
8
|
|
|
8
9
|
def self.namespace
|
|
9
10
|
"#{@namespace}." if @namespace
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
def self.namespace=
|
|
13
|
+
def self.namespace= namespace
|
|
13
14
|
@namespace = namespace
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
|
|
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
|
|
17
|
+
def self.included base
|
|
18
|
+
base.extend ClassMethods
|
|
62
19
|
end
|
|
63
20
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require_relative '../../test_helper'
|
|
2
|
+
|
|
3
|
+
describe ListsConstant::ClassMethods do
|
|
4
|
+
before do
|
|
5
|
+
Lister.send :extend, ListsConstant::ClassMethods
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "requires a name for a list constant" do
|
|
9
|
+
assert_raises ArgumentError do
|
|
10
|
+
Lister.lists_constant :red, :blue, :yellow
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "creating a constant list" do
|
|
15
|
+
before do
|
|
16
|
+
Lister.lists_constant :red,
|
|
17
|
+
:blue,
|
|
18
|
+
:yellow,
|
|
19
|
+
as: :colors
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
after do
|
|
23
|
+
Lister.send :remove_const, 'COLORS'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "adds the constant to the includer" do
|
|
27
|
+
assert_equal Lister::COLORS, [:red, :blue, :yellow]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require_relative '../../test_helper'
|
|
2
|
+
|
|
3
|
+
describe ListsConstant::Lookup do
|
|
4
|
+
before do
|
|
5
|
+
@lookup = ListsConstant::Lookup.new(Lister, 'colors')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe "given I18n translations" do
|
|
9
|
+
before do
|
|
10
|
+
@translations = {
|
|
11
|
+
red: 'rojo',
|
|
12
|
+
blue: 'azul',
|
|
13
|
+
yellow: 'amarillo'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
I18n.backend.store_translations :es, {
|
|
17
|
+
lister: {
|
|
18
|
+
colors: @translations
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
I18n.locale = :es
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "generates a hash of the localized names with their values" do
|
|
26
|
+
assert_equal @lookup.lookup(:red), @translations[:red]
|
|
27
|
+
assert_equal @lookup.lookup(:blue), @translations[:blue]
|
|
28
|
+
assert_equal @lookup.lookup(:yellow), @translations[:yellow]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "with a specified namespace" do
|
|
33
|
+
before do
|
|
34
|
+
ListsConstant.namespace = :mistranslations
|
|
35
|
+
|
|
36
|
+
@mistranslations = {
|
|
37
|
+
red: 'sangriento',
|
|
38
|
+
blue: 'morado',
|
|
39
|
+
yellow: 'gallina'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
I18n.backend.store_translations :es, {
|
|
43
|
+
mistranslations: {
|
|
44
|
+
lister: {
|
|
45
|
+
colors: @mistranslations
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
after do
|
|
52
|
+
ListsConstant.namespace = nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "scopes within the lookup for localization" do
|
|
56
|
+
assert_equal @lookup.lookup(:red), @mistranslations[:red]
|
|
57
|
+
assert_equal @lookup.lookup(:blue), @mistranslations[:blue]
|
|
58
|
+
assert_equal @lookup.lookup(:yellow), @mistranslations[:yellow]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require_relative '../../../test_helper'
|
|
2
|
+
|
|
3
|
+
describe ListsConstant::Lookups::Class do
|
|
4
|
+
before do
|
|
5
|
+
I18n.locale = :en
|
|
6
|
+
|
|
7
|
+
@colors = [:red, :blue, :yellow]
|
|
8
|
+
Lister.const_set 'COLORS', @colors
|
|
9
|
+
Lister.extend ListsConstant::Lookups::Class.new('colors', @colors)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
after do
|
|
13
|
+
Lister.send :remove_const, 'COLORS'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "determines if a passed value is available" do
|
|
17
|
+
assert Lister.includes_color?(:red)
|
|
18
|
+
assert Lister.includes_color?('blue')
|
|
19
|
+
refute Lister.includes_color?(:magenta)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "given I18n translations" do
|
|
23
|
+
before do
|
|
24
|
+
@translations = {
|
|
25
|
+
red: 'rojo',
|
|
26
|
+
blue: 'azul',
|
|
27
|
+
yellow: 'amarillo'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
I18n.backend.store_translations :es, {
|
|
31
|
+
lister: {
|
|
32
|
+
colors: @translations
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
I18n.locale = :es
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "generates a hash of the localized names with their values" do
|
|
40
|
+
assert_equal Lister.colors, {
|
|
41
|
+
red: @translations[:red],
|
|
42
|
+
blue: @translations[:blue],
|
|
43
|
+
yellow: @translations[:yellow]
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "generates an inverted hash for option helpers" do
|
|
48
|
+
assert_equal Lister.color_options, {
|
|
49
|
+
@translations[:red] => :red,
|
|
50
|
+
@translations[:blue] => :blue,
|
|
51
|
+
@translations[:yellow] => :yellow
|
|
52
|
+
}
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require_relative '../../../test_helper'
|
|
2
|
+
|
|
3
|
+
describe ListsConstant::Lookups::Instance do
|
|
4
|
+
before do
|
|
5
|
+
I18n.locale = :en
|
|
6
|
+
|
|
7
|
+
@colors = [:red, :blue, :yellow]
|
|
8
|
+
Lister.send :include, ListsConstant::Lookups::Instance.new('colors', @colors)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "adds instance query methods for each value" do
|
|
12
|
+
listed = Lister.new(:red)
|
|
13
|
+
|
|
14
|
+
assert listed.color_red?
|
|
15
|
+
refute listed.color_yellow?
|
|
16
|
+
refute listed.color_blue?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe "given I18n translations" do
|
|
20
|
+
before do
|
|
21
|
+
@translations = {
|
|
22
|
+
red: 'rojo',
|
|
23
|
+
blue: 'azul',
|
|
24
|
+
yellow: 'amarillo'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
I18n.backend.store_translations :es, {
|
|
28
|
+
lister: {
|
|
29
|
+
colors: @translations
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
I18n.locale = :es
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "localizes the variable description" do
|
|
37
|
+
listed = Lister.new(:red)
|
|
38
|
+
assert_equal listed.localized_color, @translations[:red]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lists_constant
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2013-
|
|
12
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activesupport
|
|
@@ -56,9 +56,18 @@ files:
|
|
|
56
56
|
- README.md
|
|
57
57
|
- Rakefile
|
|
58
58
|
- lib/lists_constant.rb
|
|
59
|
+
- lib/lists_constant/class_methods.rb
|
|
60
|
+
- lib/lists_constant/lookup.rb
|
|
61
|
+
- lib/lists_constant/lookups.rb
|
|
62
|
+
- lib/lists_constant/lookups/class.rb
|
|
63
|
+
- lib/lists_constant/lookups/instance.rb
|
|
59
64
|
- lib/lists_constant/version.rb
|
|
60
65
|
- lists_constant.gemspec
|
|
61
|
-
- test/
|
|
66
|
+
- test/fixtures/lister.rb
|
|
67
|
+
- test/lib/lists_constant/class_methods_test.rb
|
|
68
|
+
- test/lib/lists_constant/lookup_test.rb
|
|
69
|
+
- test/lib/lists_constant/lookups/class_test.rb
|
|
70
|
+
- test/lib/lists_constant/lookups/instance_test.rb
|
|
62
71
|
- test/lib/lists_constant/version_test.rb
|
|
63
72
|
- test/test_helper.rb
|
|
64
73
|
homepage: https://github.com/ahorner/lists-constant
|
|
@@ -89,6 +98,10 @@ summary: ListsConstant is a module which allows you to easily define lists of co
|
|
|
89
98
|
library is intended to make it simple to keep view-specific information (like the
|
|
90
99
|
text representations of your listed values) out of your model classes.
|
|
91
100
|
test_files:
|
|
92
|
-
- test/
|
|
101
|
+
- test/fixtures/lister.rb
|
|
102
|
+
- test/lib/lists_constant/class_methods_test.rb
|
|
103
|
+
- test/lib/lists_constant/lookup_test.rb
|
|
104
|
+
- test/lib/lists_constant/lookups/class_test.rb
|
|
105
|
+
- test/lib/lists_constant/lookups/instance_test.rb
|
|
93
106
|
- test/lib/lists_constant/version_test.rb
|
|
94
107
|
- test/test_helper.rb
|
|
@@ -1,115 +0,0 @@
|
|
|
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
|