code-box 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p125
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-box (0.4.0)
4
+ code-box (0.5.0)
5
5
  activerecord (~> 3.0)
6
6
 
7
7
  GEM
@@ -20,9 +20,16 @@ GEM
20
20
  multi_json (~> 1.0)
21
21
  arel (3.0.2)
22
22
  builder (3.0.0)
23
+ coderay (1.0.5)
23
24
  i18n (0.6.0)
25
+ method_source (0.8.2)
24
26
  multi_json (1.3.6)
27
+ pry (0.9.12.2)
28
+ coderay (~> 1.0.5)
29
+ method_source (~> 0.8)
30
+ slop (~> 3.4)
25
31
  rake (0.9.2.2)
32
+ slop (3.4.6)
26
33
  sqlite3 (1.3.6)
27
34
  tzinfo (0.3.33)
28
35
 
@@ -32,5 +39,6 @@ PLATFORMS
32
39
  DEPENDENCIES
33
40
  activerecord (~> 3.0)
34
41
  code-box!
42
+ pry
35
43
  rake
36
44
  sqlite3
data/README.md CHANGED
@@ -122,26 +122,73 @@ The include and code specification will create the following methods in Person:
122
122
 
123
123
  ### Defining code classes (acts_as_code)
124
124
 
125
- As describe above code_attributes can reference code objects if the `code_attribute` is of type `:associated` or `:lookup`.
125
+ Above described code_attributes can reference code objects. Code objects can be defined using
126
126
 
127
- Making an code object `acts_as_code` provides the following features:
127
+ acts_as_code(*codes, options)
128
128
 
129
- * `#translated_code(locale=I18n.locale, *other_locale_options)`
130
- <br/>Translates the code stored in `code`
129
+ Options are:
131
130
 
132
- * `#translated_code(locale=I18n.locale, *other_locale_options)`
133
- <br/>Translates the code stored in `code`
131
+ * `:code_attribute => :code`
132
+ Name of the attribute holding the code (default 'code').
133
+ * `:sti => false`
134
+ If `true` the uniqueness validation is scoped by the attribute `type` (default false).
135
+ * `:uniqueness_case_sensitive => true`
136
+ If `true` the the uniqueness validation is case sensitive (default true).
137
+ * `:position_attr => :position
138
+ If present, the order when fetching the codes is done with this expression (default scope - means - if you want to omit the order used `unscoped` on any AR operation).
134
139
 
135
- * `.translate_code(code, *options)`
140
+ All options except `:code_attribute` are used only for ActiveRecord models.
141
+
142
+ If `*codes` are provided the following code constants will be defined when calling `acts_as_code('male', 'female', code_attribute: 'code')`
143
+
144
+ __IMPORTANT__ Code object constants will only be created when the code object is not an ActiveRecord model!
145
+
146
+ class Gender
147
+ include CodeBox::ActsAsCode['male', 'female'] # Code attribute name will be 'code'
148
+ # Above is a shortcut for...
149
+ # include CodeBox::ActsAsCode
150
+ # acts_as_code('male', 'female') # Code attribute name will be 'code'
151
+
152
+
153
+ # Given codes 'male' an 'female' the following constants will be defined:
154
+ #
155
+ # module Codes
156
+ # Male = 'male'
157
+ # Female = 'female'
158
+ # All = [Male, Female]
159
+ # end
160
+ #
161
+ # Below constants pnly is is not ActiveRecod model!
162
+ # Male = Gender.new('male')
163
+ # Female = Gender.new('female')
164
+ # All = [Male, Female]
165
+ #
166
+ end
167
+
168
+
169
+ Furthermote àcts_as_code` defines the following methods:
170
+
171
+ * `.for_code(code)`
172
+ Answers the code object for the given code (fetched from cache)
173
+
174
+ * `#translated_code(locale=I18n.locale, *other_locale_options)`
175
+ Translates the code stored in `code`
176
+
177
+ * `#translated_code(locale=I18n.locale, *other_locale_options)`
178
+ Translates the code stored in `code`
179
+
180
+ * `.translate_code(codes_and_options)`
136
181
  <br/>Translates a single code if `code` is a code, an array of codes of `code` is an array.
137
182
  If code is an array the option :build => :zip can be used to build a select option capable array (e.g `[['Switzerland', 'SUI'],['Germany', 'GER'],['Denmark', 'DEN']]`)
138
183
 
139
- * `.for_code(code)`:
140
- <br/>Answers the code object for the given code (fetched from cache)
184
+ * `.build_select_options(codes_and_options)`
185
+ Build an options array from the passed codes (all codes if no codes are passed). Add an empty option at the beginning if the option `:include_nil` is passed. The localization key is defined in CodeBox (CodeBox.i18n_empty_options_key). If you want the change the default key `shared.options.pls_select` you can do so in an initializer by calling `CodeBox.i18n_empty_options_key='your.key'`.
141
186
 
142
187
  * `.clear_code_cache`
143
188
  <br/>Clears the cache so its build up on need from all codes from scratch
144
189
 
190
+ * Passing
191
+
145
192
 
146
193
  __Note:__ The code name can be configures using the `:code_attribute` option.
147
194
  `:code_attribute => :iso_code` leads to methods like `#translate_iso_code` etc.
data/code-box.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.add_development_dependency "activerecord", "~> 3.0"
18
18
  gem.add_development_dependency "sqlite3"
19
19
  gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "pry"
20
21
 
21
22
  gem.files = `git ls-files`.split($\)
22
23
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -47,9 +47,8 @@ module CodeBox
47
47
 
48
48
  module ClassMethods
49
49
  DefaultOptions = {
50
- :type => :poro,
51
50
  :code_attribute => 'code',
52
- :polymorphic => false,
51
+ :sti => false,
53
52
  :uniqueness_case_sensitive => true,
54
53
  :position_attr => :position,
55
54
  }
@@ -61,26 +60,8 @@ module CodeBox
61
60
  code_attr = opts[:code_attribute].to_s
62
61
  position_attr = opts[:position_attribute]
63
62
  case_sensitive = opts[:uniqueness_case_sensitive]
64
- model_type = opts.delete(:type)
65
63
 
66
- # Create a constant for each code
67
- if !codes.empty?
68
- code_constants = {}
69
- codes.each do |code|
70
- constant_name = "#{(code_attr+'_'+code.to_s).to_s.camelize}"
71
- constant = const_set(constant_name, code.to_s) unless const_defined?(constant_name)
72
- code_constants[constant_name] = constant
73
- end
74
- raise "Could not define all code constants. Only defined for #{code_constants.values.compact.inspect}" unless code_constants.values.compact.size == codes.size
75
-
76
- pl_code_attr = code_attr.pluralize.camelize
77
- constant_name = "All#{pl_code_attr}"
78
- if const_defined?(constant_name)
79
- raise "Could not define constant '#{const_name}' for all codes."
80
- else
81
- const_set(constant_name, code_constants.values.compact)
82
- end
83
- end
64
+ model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
84
65
 
85
66
  class_eval <<-RUBY_
86
67
  def translated_#{code_attr}(locale = I18n.locale, *options)
@@ -93,9 +74,9 @@ module CodeBox
93
74
  class << self
94
75
  attr_accessor :code_box_i18n_options_select_key
95
76
 
96
- def translate_#{code_attr}(*code)
97
- options = code.extract_options!
98
- codes = code.first
77
+ def translate_#{code_attr}(*codes_and_options)
78
+ options = codes_and_options.extract_options!
79
+ codes = codes_and_options.first
99
80
  is_parameter_array = codes.kind_of? Array
100
81
 
101
82
  codes = Array(codes)
@@ -116,13 +97,13 @@ module CodeBox
116
97
  code_cache[code]
117
98
  end
118
99
 
119
- def self.build_options(*args)
100
+ def self.build_select_options(*args)
120
101
  options = args.extract_options!
121
- codes = args.empty? ? All#{code_attr.pluralize.camelize} : args
102
+ codes = args.empty? ? #{code_attr.pluralize.camelize}::All : args
122
103
  include_nil = !!options[:include_nil]
123
104
 
124
105
  options = translate_#{code_attr}(codes, build: :zip)
125
- options << [I18n.t(CodeBox.i18n_empty_options_key), nil] if include_nil
106
+ options.unshift [I18n.t(CodeBox.i18n_empty_options_key), nil] if include_nil
126
107
 
127
108
  options
128
109
  end
@@ -134,12 +115,8 @@ module CodeBox
134
115
 
135
116
  instance_eval <<-CODE
136
117
  class << self
137
- def code_box_model_type
138
- '#{model_type.to_sym}'
139
- end
140
-
141
- def code_box_code_attr_name
142
- '#{code_attr}'
118
+ def _code_box_code_attr_name
119
+ '#{code_attr.to_s}'
143
120
  end
144
121
 
145
122
  def code_cache
@@ -163,7 +140,7 @@ module CodeBox
163
140
 
164
141
  class_eval <<-CODE
165
142
  validates_presence_of :#{code_attr}
166
- validates_uniqueness_of :#{code_attr}#{opts[:polymorphic] ? ', :scope => :type' : ' '}, :case_sensitive => #{case_sensitive}
143
+ validates_uniqueness_of :#{code_attr}#{opts[:sti] ? ', :scope => :type' : ' '}, :case_sensitive => #{case_sensitive}
167
144
 
168
145
  default_scope order('#{order_expression}')
169
146
  CODE
@@ -178,6 +155,10 @@ module CodeBox
178
155
  self.#{code_attr} = #{code_attr}
179
156
  end
180
157
 
158
+ def self.all
159
+ raise 'Class responsibility'
160
+ end
161
+
181
162
  def hash
182
163
  (self.class.name + '#' + #{code_attr}).hash
183
164
  end
@@ -190,81 +171,56 @@ module CodeBox
190
171
  self.equal? other
191
172
  end
192
173
  CODE
193
-
194
- if codes.empty?
195
- class_eval <<-CODE
196
- def self.all
197
- raise 'Class responsibility'
198
- end
199
- CODE
200
- else
201
- code_constants = {}
202
- codes.each do |code|
203
- constant_name = "#{code.to_s.camelize}"
204
- constant = const_set(constant_name, self.new(code.to_s)) unless const_defined?(constant_name)
205
- code_constants[constant_name] = constant
206
- end
207
- raise "Could not define all code instance constants. Only defined for #{code_constants.values.compact.inspect}" unless code_constants.values.compact.size == codes.size
208
-
209
- pl_code_attr = code_attr.pluralize.camelize
210
- constant_name = 'All'
211
- if const_defined?(constant_name)
212
- raise "Could not define constant '#{const_name}' for all codes."
213
- else
214
- const_set(constant_name, code_constants.values.compact)
215
- end
216
-
217
- class_eval <<-CODE
218
- def self.all
219
- All
220
- end
221
- CODE
222
- end
223
-
224
174
  else
225
175
  raise ArgumentError, "'#{model_type}' is not a valid type. Use :active_record or :poro(default) instead"
226
176
  end
177
+
178
+ define_codes(*codes)
227
179
  end
228
180
 
229
181
  def define_codes(*codes)
230
- code_attr = self.code_box_code_attr_name
231
- model_type = self.code_box_model_type
232
-
233
182
  return if codes.empty?
234
183
 
184
+ # --- Define the code constants...
185
+
186
+ code_attr = self._code_box_code_attr_name
187
+ model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
188
+
189
+ module_name = code_attr.pluralize.camelize
190
+ codes_module = const_set(module_name, Module.new)
191
+
235
192
  # Create a constant for each code
236
- code_constants = {}
193
+ constants = {}
237
194
  codes.each do |code|
238
- constant_name = "#{(code_attr+'_'+code.to_s).to_s.camelize}"
239
- constant = const_set(constant_name, code.to_s) unless const_defined?(constant_name)
240
- code_constants[constant_name] = constant
195
+ constant_name = code.to_s.camelize
196
+ constant = codes_module.const_set(constant_name, code.to_s) unless codes_module.const_defined?(constant_name)
197
+ constants[constant_name] = constant
241
198
  end
242
- raise "Could not define all code constants. Only defined for #{code_constants.values.compact.inspect}" unless code_constants.values.compact.size == codes.size
199
+ raise "Could not define all code constants. Only defined for #{constants.values.compact.inspect}" unless constants.values.compact.size == codes.size
243
200
 
244
- pl_code_attr = code_attr.pluralize.camelize
245
- constant_name = "All#{pl_code_attr}"
246
- if const_defined?(constant_name)
247
- raise "Could not define constant '#{const_name}' for all codes."
201
+ if const_defined?('All')
202
+ raise "Could not define constant 'All' for all codes."
248
203
  else
249
- const_set(constant_name, code_constants.values.compact)
204
+ codes_module.const_set('All', constants.values.compact)
250
205
  end
251
206
 
252
- return if self.code_box_model_type == :active_record
207
+ return if model_type == :active_record
208
+
209
+ # --- Define the code instance constants...
253
210
 
254
- code_constants = {}
211
+ constants = {}
255
212
  codes.each do |code|
256
- constant_name = "#{code.to_s.camelize}"
257
- constant = const_set(constant_name, self.new(code.to_s)) unless const_defined?(constant_name)
258
- code_constants[constant_name] = constant
213
+ constant_name = "#{code.to_s.camelize}"
214
+ constant = const_set(constant_name, self.new(code.to_s)) unless const_defined?(constant_name)
215
+ constants[constant_name] = constant
259
216
  end
260
- raise "Could not define all code instance constants. Only defined for #{code_constants.values.compact.inspect}" unless code_constants.values.compact.size == codes.size
217
+ raise "Could not define all code instance constants. Only defined for #{constants.values.compact.inspect}" unless constants.values.compact.size == codes.size
261
218
 
262
- pl_code_attr = code_attr.pluralize.camelize
263
- constant_name = 'All'
219
+ constant_name = 'All'
264
220
  if const_defined?(constant_name)
265
221
  raise "Could not define constant '#{const_name}' for all codes."
266
222
  else
267
- const_set(constant_name, code_constants.values.compact)
223
+ const_set(constant_name, constants.values.compact)
268
224
  end
269
225
 
270
226
  class_eval <<-CODE
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module CodeBox
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'helper'
4
+ require 'pry'
4
5
 
5
6
  class TestActsAsCode < Test::Unit::TestCase
6
7
 
@@ -30,14 +31,15 @@ class TestActsAsCode < Test::Unit::TestCase
30
31
 
31
32
  def test_code_constant_definitiones
32
33
  # Constants
33
- assert Codes::CivilStatus.const_defined?('CodeSingle')
34
- assert Codes::CivilStatus.const_defined?('CodeMarried')
35
- assert Codes::CivilStatus.const_defined?('AllCodes')
34
+ assert Codes::CivilStatus.const_defined?('Codes')
35
+ assert Codes::CivilStatus::Codes.const_defined?('Single')
36
+ assert Codes::CivilStatus::Codes.const_defined?('Married')
37
+ assert Codes::CivilStatus::Codes.const_defined?('All')
36
38
 
37
39
  # Constants-Values
38
- assert_equal [Codes::CivilStatus::CodeSingle, Codes::CivilStatus::CodeMarried], Codes::CivilStatus::AllCodes
39
- assert_equal Codes::CivilStatus::CodeMarried, 'married'
40
- assert_equal Codes::CivilStatus::CodeSingle, 'single'
40
+ assert_equal [Codes::CivilStatus::Codes::Single, Codes::CivilStatus::Codes::Married], Codes::CivilStatus::Codes::All
41
+ assert_equal Codes::CivilStatus::Codes::Married, 'married'
42
+ assert_equal Codes::CivilStatus::Codes::Single, 'single'
41
43
  end
42
44
 
43
45
  def test_code_instance_constant_definitions
@@ -74,12 +76,12 @@ class TestActsAsCode < Test::Unit::TestCase
74
76
  end
75
77
 
76
78
  def test_options_building
77
- options_array = Codes::CivilStatus.build_options
79
+ options_array = Codes::CivilStatus.build_select_options
78
80
  assert_equal options_array.size, 2
79
81
 
80
- options_array = Codes::CivilStatus.build_options(include_nil: true)
81
- puts arrrr: options_array
82
+ options_array = Codes::CivilStatus.build_select_options(include_nil: true)
82
83
  assert_equal options_array.size, 3
84
+ puts options_array: options_array
83
85
  end
84
86
 
85
87
  end
@@ -13,7 +13,7 @@ de:
13
13
  codes/civil_status:
14
14
  code:
15
15
  married: verheiratet
16
- single: ledig
16
+ single: ledig
17
17
  model:
18
18
  values:
19
19
  codes/segment_model:
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.1
4
+ version: 0.5.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-09-08 00:00:00.000000000 Z
12
+ date: 2013-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: Specify attributes as code and provide lookup by I18n-, cache- or associated
79
95
  and support for building code classes.
80
96
  email:
@@ -84,7 +100,7 @@ extensions: []
84
100
  extra_rdoc_files: []
85
101
  files:
86
102
  - .gitignore
87
- - .rvmrc
103
+ - .ruby-version
88
104
  - Gemfile
89
105
  - Gemfile.lock
90
106
  - LICENSE
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use 1.9.3-p125