code-box 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-box (0.0.2)
4
+ code-box (0.3.0)
5
5
  activerecord (~> 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -46,10 +46,14 @@ Example
46
46
  code_attribute :nationality
47
47
  end
48
48
 
49
- The include will create the following method in Person:
49
+ The include will create the following methods in Person:
50
50
 
51
51
  `#nationality` Will return the nationality text for the value stored in `nationality_code`. For the code 'SUI' the I18n key would look like: `activerecord.values.person.nationality_code.SUI` (Note: The key is build like the standard I18n keys for activerecord classes or attribute by default. Since I dislike the `activerecord` naming and prefer `model` I made this configurable - see below).
52
52
 
53
+ `.translate_nationality_code` Translates a code or an array of codes returning the translated code
54
+ or an array of translated codes. If passing the option `:build => :zip` the method returns an array
55
+ of arrays with translation and code which can be used to build html options.
56
+
53
57
 
54
58
  #### Lookup through code object
55
59
 
@@ -69,28 +69,29 @@ module CodeBox
69
69
  end
70
70
 
71
71
  class_eval <<-RUBY_
72
- def translated_#{code_attr}(locale=I18n.locale)
73
- self.class.translate_#{code_attr}(code, :locale => locale)
72
+ def translated_#{code_attr}(locale = I18n.locale, *options)
73
+ locale_options = options.extract_options!
74
+ locale_options.merge!({:locale => locale})
75
+ self.class.translate_#{code_attr}(#{code_attr}, locale_options.merge({:locale => locale}))
74
76
  end
75
77
 
76
78
  # translator
77
79
  class << self
78
80
  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
81
+ options = code.extract_options!
82
+ codes = code.first
83
+ is_parameter_array = codes.kind_of? Array
83
84
 
84
85
  codes = Array(codes)
85
86
  translated_codes = codes.map { |code|
86
87
  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
+ I18n.t("\#{self._code_box_i18n_model_segment}.values.\#{self.name.underscore}.#{code_attr}.\#{code_key}", options)
88
89
  }
89
90
 
90
91
  if options[:build] == :zip
91
92
  translated_codes.zip(codes)
92
93
  else
93
- is_paramter_array ? translated_codes : translated_codes.first
94
+ is_parameter_array ? translated_codes : translated_codes.first
94
95
  end
95
96
  end
96
97
  end
@@ -102,6 +103,7 @@ module CodeBox
102
103
  def self.initialize_cache
103
104
  Hash[all.map{ |code| [code.#{code_attr}, code] }]
104
105
  end
106
+
105
107
  RUBY_
106
108
 
107
109
  instance_eval <<-CODE
@@ -109,6 +111,10 @@ module CodeBox
109
111
  def code_cache
110
112
  @code_cache ||= initialize_cache
111
113
  end
114
+
115
+ def clear_code_cache
116
+ @code_cache = nil
117
+ end
112
118
  end
113
119
  CODE
114
120
 
@@ -132,6 +138,12 @@ module CodeBox
132
138
  order_attr = position_attr ? position_attr.to_s : code_attr.to_s
133
139
 
134
140
  class_eval <<-CODE
141
+ attr_accessor :#{code_attr}
142
+
143
+ def self.initialize(#{code_attr})
144
+ self.#{code_attr} = #{code_attr}
145
+ end
146
+
135
147
  def hash
136
148
  (self.class.name + '#' + #{code_attr}).hash
137
149
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module CodeBox
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/code-box.rb CHANGED
@@ -9,8 +9,10 @@ module CodeBox
9
9
  def i18n_model_segment=(segment)
10
10
  Config[:i18n_model_segment] = segment
11
11
  end
12
+
12
13
  def i18n_model_segment
13
14
  Config[:i18n_model_segment]
14
15
  end
16
+
15
17
  module_function :i18n_model_segment=, :i18n_model_segment
16
18
  end
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.2.0
4
+ version: 0.3.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-22 00:00:00.000000000 Z
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -117,12 +117,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
117
  - - ! '>='
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -1456200753415907069
120
123
  required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  none: false
122
125
  requirements:
123
126
  - - ! '>='
124
127
  - !ruby/object:Gem::Version
125
128
  version: '0'
129
+ segments:
130
+ - 0
131
+ hash: -1456200753415907069
126
132
  requirements: []
127
133
  rubyforge_project:
128
134
  rubygems_version: 1.8.21