code-box 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/code-box/acts_as_code.rb +77 -49
- data/lib/code-box/code_attribute.rb +2 -14
- data/lib/code-box/version.rb +1 -1
- data/lib/code-box.rb +13 -2
- data/test/code-box/test_acts_as_code.rb +17 -3
- data/test/code-box/test_code_attribute.rb +1 -1
- data/test/resources/locale/de.yml +7 -4
- data/test/resources/locale/en.yml +7 -4
- data/test/resources/models.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -39,7 +39,7 @@ There are several options to specify an attribute as a code:
|
|
39
39
|
Example
|
40
40
|
|
41
41
|
class Person
|
42
|
-
|
42
|
+
include CodeBox::CodeAttribute
|
43
43
|
|
44
44
|
attr_accessor :nationality_code
|
45
45
|
|
@@ -56,7 +56,7 @@ The include will create the following method in Person:
|
|
56
56
|
Example
|
57
57
|
|
58
58
|
class Person
|
59
|
-
|
59
|
+
include CodeBox::CodeAttribute
|
60
60
|
|
61
61
|
attr_accessor :nationality_code
|
62
62
|
|
@@ -84,7 +84,7 @@ The include will create the following method in Person:
|
|
84
84
|
The code value is interpreted as a foreign key on an associated AR Code object.
|
85
85
|
|
86
86
|
class Person < ActiveRecord::Base
|
87
|
-
|
87
|
+
include CodeBox::CodeAttribute
|
88
88
|
|
89
89
|
code_attribute :nationality, :lookup_type => :activerecord
|
90
90
|
end
|
@@ -7,19 +7,47 @@ module CodeBox
|
|
7
7
|
@opts = {}
|
8
8
|
|
9
9
|
def self.[](*options)
|
10
|
-
@opts = options
|
10
|
+
@opts = options.dup
|
11
|
+
|
12
|
+
instance_eval <<-RUBY_
|
13
|
+
class << self
|
14
|
+
def _code_box_i18n_model_segment
|
15
|
+
"#{options.extract_options![:i18n_model_segment]}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
RUBY_
|
19
|
+
|
11
20
|
self
|
12
21
|
end
|
13
22
|
|
14
23
|
def self.included(base)
|
24
|
+
unless (class << self; self; end).method_defined?(:_code_box_i18n_model_segment)
|
25
|
+
instance_eval <<-RUBY_
|
26
|
+
class << self
|
27
|
+
def _code_box_i18n_model_segment
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
RUBY_
|
32
|
+
end
|
33
|
+
|
34
|
+
instance_eval <<-RUBY_
|
35
|
+
class << base
|
36
|
+
def _code_box_i18n_model_segment
|
37
|
+
return CodeBox.i18n_model_segment if "#{self._code_box_i18n_model_segment}".empty?
|
38
|
+
"#{self._code_box_i18n_model_segment}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
RUBY_
|
42
|
+
|
15
43
|
base.extend(ClassMethods)
|
16
|
-
base.acts_as_code(*@opts) if @opts
|
44
|
+
base.acts_as_code(*@opts) if @opts
|
17
45
|
end
|
18
46
|
|
19
47
|
|
20
48
|
module ClassMethods
|
21
49
|
DefaultOptions = {
|
22
|
-
:
|
50
|
+
:type => :poro,
|
23
51
|
:code_attribute => :code,
|
24
52
|
:polymorphic => false,
|
25
53
|
:uniqueness_case_sensitive => true,
|
@@ -33,16 +61,60 @@ module CodeBox
|
|
33
61
|
code_attr = opts[:code_attribute]
|
34
62
|
position_attr = opts[:position_attribute]
|
35
63
|
case_sensitive = opts[:uniqueness_case_sensitive]
|
36
|
-
model_type = opts.delete(:
|
64
|
+
model_type = opts.delete(:type)
|
37
65
|
|
38
66
|
# Create a constant for each code
|
39
67
|
codes.each do |code|
|
40
68
|
const_set("Code#{code.to_s.camelize}", code)
|
41
69
|
end
|
42
70
|
|
43
|
-
|
71
|
+
class_eval <<-RUBY_
|
72
|
+
def translated_#{code_attr}(locale=I18n.locale)
|
73
|
+
self.class.translate_#{code_attr}(code, :locale => locale)
|
74
|
+
end
|
75
|
+
|
76
|
+
# translator
|
77
|
+
class << self
|
78
|
+
def translate_#{code_attr}(*code)
|
79
|
+
options = code.extract_options!
|
80
|
+
locale = options[:locale] || I18n.locale
|
81
|
+
codes = code.first
|
82
|
+
is_paramter_array = codes.kind_of? Array
|
83
|
+
|
84
|
+
codes = Array(codes)
|
85
|
+
translated_codes = codes.map { |code|
|
86
|
+
code_key = code.nil? ? :null_value : code
|
87
|
+
I18n.t("\#{self._code_box_i18n_model_segment}.values.\#{self.name.underscore}.#{code_attr}.\#{code_key}", :locale => locale)
|
88
|
+
}
|
89
|
+
|
90
|
+
if options[:build] == :zip
|
91
|
+
translated_codes.zip(codes)
|
92
|
+
else
|
93
|
+
is_paramter_array ? translated_codes : translated_codes.first
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
44
97
|
|
98
|
+
def self.for_code(code)
|
99
|
+
code_cache[code]
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.initialize_cache
|
103
|
+
Hash[all.map{ |code| [code.#{code_attr}, code] }]
|
104
|
+
end
|
105
|
+
RUBY_
|
106
|
+
|
107
|
+
instance_eval <<-CODE
|
108
|
+
class << self
|
109
|
+
def code_cache
|
110
|
+
@code_cache ||= initialize_cache
|
111
|
+
end
|
112
|
+
end
|
113
|
+
CODE
|
114
|
+
|
115
|
+
case model_type
|
45
116
|
when :active_record
|
117
|
+
|
46
118
|
order_expression = if self.attribute_names.include?(position_attr) then
|
47
119
|
"coalesce(#{position_attr.to_s}, #{code_attr.to_s})"
|
48
120
|
else
|
@@ -54,48 +126,12 @@ module CodeBox
|
|
54
126
|
validates_uniqueness_of :#{code_attr}#{opts[:polymorphic] ? ', :scope => :type' : ' '}, :case_sensitive => #{case_sensitive}
|
55
127
|
|
56
128
|
default_scope order('#{order_expression}')
|
57
|
-
|
58
|
-
def self.initialize_cache
|
59
|
-
all.inject({}) {|hash, obj| hash[obj.#{code_attr}] = obj; hash }
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.for_code(code)
|
63
|
-
code_cache[code]
|
64
|
-
end
|
65
|
-
|
66
|
-
def hash
|
67
|
-
(self.class.name + '#' + #{code_attr}).hash
|
68
|
-
end
|
69
|
-
|
70
|
-
def equal?(other)
|
71
|
-
other && is_a?(other.class) && #{code_attr} == other.#{code_attr}
|
72
|
-
end
|
73
|
-
|
74
|
-
def ==(other)
|
75
|
-
self.equal? other
|
76
|
-
end
|
77
|
-
CODE
|
78
|
-
|
79
|
-
instance_eval <<-CODE
|
80
|
-
class << self
|
81
|
-
def code_cache
|
82
|
-
@code_cache ||= initialize_cache
|
83
|
-
end
|
84
|
-
end
|
85
129
|
CODE
|
86
130
|
|
87
131
|
when :poro
|
88
132
|
order_attr = position_attr ? position_attr.to_s : code_attr.to_s
|
89
133
|
|
90
134
|
class_eval <<-CODE
|
91
|
-
def self.initialize_cache
|
92
|
-
all.inject({}) {|hash, obj| hash[obj.#{code_attr}] = obj; hash }
|
93
|
-
end
|
94
|
-
|
95
|
-
def self.for_code(code)
|
96
|
-
code_cache[code]
|
97
|
-
end
|
98
|
-
|
99
135
|
def hash
|
100
136
|
(self.class.name + '#' + #{code_attr}).hash
|
101
137
|
end
|
@@ -113,14 +149,6 @@ module CodeBox
|
|
113
149
|
end
|
114
150
|
CODE
|
115
151
|
|
116
|
-
instance_eval <<-CODE
|
117
|
-
class << self
|
118
|
-
def code_cache
|
119
|
-
@code_cache ||= initialize_cache
|
120
|
-
end
|
121
|
-
end
|
122
|
-
CODE
|
123
|
-
|
124
152
|
else
|
125
153
|
raise ArgumentError, "'#{model_type}' is not a valid type. Use :active_record or :poro(default) instead"
|
126
154
|
end
|
@@ -1,18 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module CodeBox
|
4
|
-
|
5
4
|
module CodeAttribute
|
6
|
-
Config = { :i18n_model_segment => :activerecord }
|
7
|
-
|
8
|
-
def i18n_model_segment=(segment)
|
9
|
-
Config[:i18n_model_segment] = segment
|
10
|
-
end
|
11
|
-
def i18n_model_segment
|
12
|
-
Config[:i18n_model_segment]
|
13
|
-
end
|
14
|
-
module_function :i18n_model_segment=, :i18n_model_segment
|
15
|
-
|
16
5
|
|
17
6
|
def self.[](*options)
|
18
7
|
instance_eval <<-RUBY_
|
@@ -41,7 +30,7 @@ module CodeBox
|
|
41
30
|
instance_eval <<-RUBY_
|
42
31
|
class << base
|
43
32
|
def _code_box_i18n_model_segment
|
44
|
-
return CodeBox
|
33
|
+
return CodeBox.i18n_model_segment if "#{self._code_box_i18n_model_segment}".empty?
|
45
34
|
"#{self._code_box_i18n_model_segment}"
|
46
35
|
end
|
47
36
|
end
|
@@ -69,7 +58,6 @@ module CodeBox
|
|
69
58
|
code_class_name = opts_copy.delete(:class_name) || "::Codes::#{code_name.to_s.camelize}"
|
70
59
|
|
71
60
|
case lookup_type
|
72
|
-
|
73
61
|
when :lookup
|
74
62
|
class_eval <<-RUBY_
|
75
63
|
# getter
|
@@ -116,7 +104,7 @@ module CodeBox
|
|
116
104
|
codes = Array(codes)
|
117
105
|
translated_codes = codes.map { |code|
|
118
106
|
code_key = code.nil? ? :null_value : code
|
119
|
-
I18n.t("\#{self._code_box_i18n_model_segment}.\#{self.name.underscore}
|
107
|
+
I18n.t("\#{self._code_box_i18n_model_segment}.values.\#{self.name.underscore}.#{code_attr_name}.\#{code_key}", :locale => locale)
|
120
108
|
}
|
121
109
|
|
122
110
|
if options[:build] == :zip
|
data/lib/code-box/version.rb
CHANGED
data/lib/code-box.rb
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'code-box/code_attribute'
|
4
|
+
require 'code-box/acts_as_code'
|
5
5
|
|
6
|
+
module CodeBox
|
7
|
+
Config = { :i18n_model_segment => :activerecord }
|
8
|
+
|
9
|
+
def i18n_model_segment=(segment)
|
10
|
+
Config[:i18n_model_segment] = segment
|
11
|
+
end
|
12
|
+
def i18n_model_segment
|
13
|
+
Config[:i18n_model_segment]
|
14
|
+
end
|
15
|
+
module_function :i18n_model_segment=, :i18n_model_segment
|
16
|
+
end
|
@@ -4,7 +4,6 @@ require 'helper'
|
|
4
4
|
|
5
5
|
class TestActsAsCode < Test::Unit::TestCase
|
6
6
|
|
7
|
-
|
8
7
|
def test_constants
|
9
8
|
assert_equal 'single', Codes::CivilStatus::CodeSingle
|
10
9
|
end
|
@@ -16,21 +15,36 @@ class TestActsAsCode < Test::Unit::TestCase
|
|
16
15
|
assert_equal Codes::CivilStatus.for_code('married'), Codes::CivilStatus::all.last
|
17
16
|
end
|
18
17
|
|
19
|
-
|
20
18
|
def test_ar_code_all
|
19
|
+
Codes::ArCode.delete_all
|
20
|
+
|
21
21
|
Codes::ArCode.create(:code => 'code_1', :name => "Code_1_name")
|
22
22
|
Codes::ArCode.create(:code => 'code_2', :name => "Code_2_name")
|
23
23
|
|
24
24
|
assert_equal 2, Codes::ArCode.all.size
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
27
|
def test_ar_code_lookup
|
28
|
+
Codes::ArCode.delete_all
|
29
29
|
code_1 = Codes::ArCode.create(:code => 'code_1', :name => "Code_1_name")
|
30
30
|
code_2 = Codes::ArCode.create(:code => 'code_2', :name => "Code_2_name")
|
31
31
|
|
32
32
|
assert_equal code_2, Codes::ArCode.for_code('code_2')
|
33
33
|
end
|
34
34
|
|
35
|
+
def test_constant_definition
|
36
|
+
assert Codes::CivilStatus.const_defined?('CodeSingle')
|
37
|
+
assert Codes::CivilStatus.const_defined?('CodeMarried')
|
38
|
+
|
39
|
+
assert_equal Codes::CivilStatus::CodeMarried, 'married'
|
40
|
+
assert_equal Codes::CivilStatus::CodeSingle, 'single'
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_code_translation
|
44
|
+
code = Codes::CivilStatus.new('married')
|
45
|
+
|
46
|
+
assert_equal code.translated_code, 'married'
|
47
|
+
assert_equal code.translated_code(:de), 'verheiratet'
|
48
|
+
end
|
35
49
|
|
36
50
|
end
|
@@ -7,7 +7,7 @@ class TestCodeAttribute < Test::Unit::TestCase
|
|
7
7
|
def setup
|
8
8
|
end
|
9
9
|
|
10
|
-
# :type => :i18n
|
10
|
+
# :type => :i18n -----------------------------------------------------------------
|
11
11
|
def test_code_attribute_i18n_lookup
|
12
12
|
obj = Codes::SampleClass.new(gender_code: 'f', country_iso: 'de')
|
13
13
|
I18n.locale =:en
|
@@ -2,17 +2,20 @@
|
|
2
2
|
---
|
3
3
|
de:
|
4
4
|
activerecord:
|
5
|
-
|
6
|
-
|
5
|
+
values:
|
6
|
+
codes/sample_class:
|
7
7
|
gender_code:
|
8
8
|
f: weiblich
|
9
9
|
m: männlich
|
10
10
|
country_iso:
|
11
11
|
de: Deutschland
|
12
12
|
ch: Schweiz
|
13
|
+
codes/civil_status:
|
14
|
+
code:
|
15
|
+
married: verheiratet
|
13
16
|
model:
|
14
|
-
|
15
|
-
|
17
|
+
values:
|
18
|
+
codes/segment_model:
|
16
19
|
gender_code:
|
17
20
|
f: weiblich
|
18
21
|
m: männlich
|
@@ -2,17 +2,20 @@
|
|
2
2
|
---
|
3
3
|
en:
|
4
4
|
activerecord:
|
5
|
-
|
6
|
-
|
5
|
+
values:
|
6
|
+
codes/sample_class:
|
7
7
|
gender_code:
|
8
8
|
f: female
|
9
9
|
m: male
|
10
10
|
country_iso:
|
11
11
|
de: Germany
|
12
12
|
ch: Switzerland
|
13
|
+
codes/civil_status:
|
14
|
+
code:
|
15
|
+
married: married
|
13
16
|
model:
|
14
|
-
|
15
|
-
|
17
|
+
values:
|
18
|
+
codes/segment_model:
|
16
19
|
gender_code:
|
17
20
|
f: female
|
18
21
|
m: male
|
data/test/resources/models.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code-box
|
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: 2012-06-
|
12
|
+
date: 2012-06-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|