code-box 0.5.1 → 0.6.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8571a319760fdeab49de5c25b646af2c9796496
4
+ data.tar.gz: 70066a95056dda5b85a66eefd45b504ba359eb63
5
+ SHA512:
6
+ metadata.gz: 91c1d2f6117c5e1b5b077d7a3d1311370963243198aa569e95c5f4a18f1a3a09a4bbe9f814794ccafe5a997cef9a6ca5fd8ef674b0bd6b31f59750083923ec2c
7
+ data.tar.gz: f62c03744f239e18854bdc98cb88ccf9ad597ecfcc2f96ca908e76daa6fcbd684696ab986a10694ac55d70d116023021d22ab71720a8699fa5c5a126d195528a
data/Gemfile.lock CHANGED
@@ -1,29 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-box (0.5.1)
5
- activerecord (~> 3.0)
4
+ code-box (0.5.0)
5
+ activerecord (~> 4.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (3.2.7)
11
- activesupport (= 3.2.7)
12
- builder (~> 3.0.0)
13
- activerecord (3.2.7)
14
- activemodel (= 3.2.7)
15
- activesupport (= 3.2.7)
16
- arel (~> 3.0.2)
17
- tzinfo (~> 0.3.29)
18
- activesupport (3.2.7)
19
- i18n (~> 0.6)
20
- multi_json (~> 1.0)
21
- arel (3.0.2)
22
- builder (3.0.0)
10
+ activemodel (4.0.0)
11
+ activesupport (= 4.0.0)
12
+ builder (~> 3.1.0)
13
+ activerecord (4.0.0)
14
+ activemodel (= 4.0.0)
15
+ activerecord-deprecated_finders (~> 1.0.2)
16
+ activesupport (= 4.0.0)
17
+ arel (~> 4.0.0)
18
+ activerecord-deprecated_finders (1.0.3)
19
+ activesupport (4.0.0)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ minitest (~> 4.2)
22
+ multi_json (~> 1.3)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 0.3.37)
25
+ arel (4.0.0)
26
+ atomic (1.1.14)
27
+ builder (3.1.4)
23
28
  coderay (1.0.5)
24
- i18n (0.6.0)
29
+ i18n (0.6.5)
25
30
  method_source (0.8.2)
26
- multi_json (1.3.6)
31
+ minitest (4.7.5)
32
+ multi_json (1.8.0)
27
33
  pry (0.9.12.2)
28
34
  coderay (~> 1.0.5)
29
35
  method_source (~> 0.8)
@@ -31,13 +37,15 @@ GEM
31
37
  rake (0.9.2.2)
32
38
  slop (3.4.6)
33
39
  sqlite3 (1.3.6)
34
- tzinfo (0.3.33)
40
+ thread_safe (0.1.3)
41
+ atomic
42
+ tzinfo (0.3.37)
35
43
 
36
44
  PLATFORMS
37
45
  ruby
38
46
 
39
47
  DEPENDENCIES
40
- activerecord (~> 3.0)
48
+ activerecord (~> 4.0)
41
49
  code-box!
42
50
  pry
43
51
  rake
data/README.md CHANGED
@@ -1,8 +1,3 @@
1
- # Notes
2
-
3
- If you re using ActiveRecord ~> 4.0.0 use the version ~> 0.6
4
- If you re using ActiveRecord ~> 3.0 use the version ~> 0.5.0
5
-
6
1
  # CodeBox::CodeAttribute
7
2
 
8
3
  Lets you define attributes as codes, instead keys (ids). For simple option storage saving a string code is often more simple an conveniant the storing an artificial id-key referencing a special code object.
@@ -171,34 +166,6 @@ __IMPORTANT__ Code object constants will only be created when the code object is
171
166
  end
172
167
 
173
168
 
174
- If `*codes` are provided - in addition the following test_methods are defined:
175
-
176
- class Gender
177
- include CodeBox::ActsAsCode['male', 'female'] # Code attribute name will be 'code'
178
- # Above is a shortcut for...
179
- # include CodeBox::ActsAsCode
180
- # acts_as_code('male', 'female') # Code attribute name will be 'code'
181
-
182
-
183
- # Given codes 'male' and 'female' the following constants will be defined:
184
- # ... al the stuff above
185
- #
186
- # def male?
187
- # code == Codes::Male
188
- # end
189
- #
190
- # def female?
191
- # code == Codes::Female
192
- # end
193
- #
194
- end
195
-
196
- Pass the option `define_test_methods: false` if you don't want to have test-methods generated.
197
- If you prefer the all test-methods prefixed with e.g. `is_` so the methods above look like `is_male?` then you can configure this
198
- globaly by defining `CodeBox.test_method_prefix='is_'`.
199
-
200
-
201
-
202
169
  In addition `acts_as_code` defines the following methods:
203
170
 
204
171
  * `.for_code(code)`
@@ -212,29 +179,7 @@ In addition `acts_as_code` defines the following methods:
212
179
  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']]`)
213
180
 
214
181
  * `.build_select_options(codes_and_options)`
215
- 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_empty` is passed. If `:include_empty` is…
216
-
217
- * `true`, then options label is derived from the I18n tranlsation of the key defined in CodeBox.i18n_empty_options_key (default is `shared.options.pls_select` - you can change this default). The value of the option is `nil` by default.
218
- * `false` then no empty option is defined (default)
219
- * a String then the string is used as label. The value of the option is `nil` by default.
220
- * a String starting with `i18n.` the the string is considered as a tranlation key (without the `i18n.` part). Value is nil by default.
221
- * is a Hash then the value is take from the Hashs value for key `:value` (nil if not present), and the label is taken from the value of key `:label` (default CodeBox.i18n_empty_option_key). If a String is present it is interpreted as a plain label or I18n key as described above.
222
-
223
- ``Examples
224
-
225
- .build_select_options(include_empty: true)
226
-
227
- is same as …
228
-
229
- .build_select_options(include_empty: {})
230
-
231
- is same as …
232
-
233
- .build_select_options(include_empty: {label: nil, value: nil})
234
-
235
- is same as …
236
-
237
- .build_select_options(include_empty: {label: '<your default key in CodeBox.i18n_empty_options_key>', value: nil})
182
+ 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'`.
238
183
 
239
184
  * `.clear_code_cache`
240
185
  Clears the cache so its build up again lazy when needed form all codes.
@@ -302,14 +247,6 @@ Assuming we have an ActiveRecod code class with `code_attribute :code` we can de
302
247
 
303
248
  ## Changelog
304
249
 
305
- ### Version 0.5.1
306
- * Adding testing methods for codes objects. E.g. Code-Object with code 'my_code' get method `#codeObjectInstance.my_code?` defined.
307
- * Method `.build_select_options` has been changed so you can pass in more flexible empty option configuration (see Readme).
308
- * Ugrade of Rails 4 Version pending…
309
-
310
- ### Version 0.6.0
311
- * Moving to activerecord 4.0.
312
-
313
250
  ### Version 0.5.0
314
251
  * Change constant definition. Create code constants in a Codes module so the can be included in other places - an document it.
315
252
  * Remove option `:model_type. The option is obsolte and can be derived from the including module.
data/code-box.gemspec CHANGED
@@ -12,9 +12,9 @@ Gem::Specification.new do |gem|
12
12
  gem.summary = %q{Specify attributes as code and provide lookup by I18n-, cache- or associated and support for building code classes.}
13
13
  gem.homepage = %q{http://github.com/verticonaut/code-box}
14
14
 
15
- gem.add_dependency "activerecord", "~> 3.0"
15
+ gem.add_dependency "activerecord", "~> 4.0"
16
16
 
17
- gem.add_development_dependency "activerecord", "~> 3.0"
17
+ gem.add_development_dependency "activerecord", "~> 4.0"
18
18
  gem.add_development_dependency "sqlite3"
19
19
  gem.add_development_dependency "rake"
20
20
  gem.add_development_dependency "pry"
@@ -47,23 +47,21 @@ module CodeBox
47
47
 
48
48
  module ClassMethods
49
49
  DefaultOptions = {
50
- code_attribute: 'code',
51
- sti: false,
52
- uniqueness_case_sensitive: true,
53
- position_attr: :position,
54
- define_test_methods: true,
50
+ :code_attribute => 'code',
51
+ :sti => false,
52
+ :uniqueness_case_sensitive => true,
53
+ :position_attr => :position,
55
54
  }
56
55
 
57
56
  def acts_as_code(*codes_and_or_options)
58
- options = codes_and_or_options.extract_options!
59
- codes = codes_and_or_options
60
- opts = DefaultOptions.merge(options)
61
- code_attr = opts[:code_attribute].to_s
62
- position_attr = opts[:position_attribute]
63
- case_sensitive = opts[:uniqueness_case_sensitive]
64
- define_test_methods = opts[:define_test_methods]
57
+ options = codes_and_or_options.extract_options!
58
+ codes = codes_and_or_options
59
+ opts = DefaultOptions.merge(options)
60
+ code_attr = opts[:code_attribute].to_s
61
+ position_attr = opts[:position_attribute]
62
+ case_sensitive = opts[:uniqueness_case_sensitive]
65
63
 
66
- model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
64
+ model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
67
65
 
68
66
  class_eval <<-RUBY_
69
67
  def translated_#{code_attr}(locale = I18n.locale, *options)
@@ -100,32 +98,12 @@ module CodeBox
100
98
  end
101
99
 
102
100
  def self.build_select_options(*args)
103
- options = args.extract_options!
104
- codes = args.empty? ? #{code_attr.pluralize.camelize}::All : args
105
- include_empty = options[:include_empty] || false
106
- locale = options.fetch(:locale, I18n.locale)
107
-
108
- label, value = case include_empty
109
- when Hash
110
- [
111
- include_empty.fetch(:label, "i18n.#{CodeBox.i18n_empty_options_key}"),
112
- include_empty.fetch(:value, nil)
113
- ]
114
- when TrueClass
115
- [ "i18n.#{CodeBox.i18n_empty_options_key}", nil ]
116
- when String
117
- [ include_empty, nil ]
118
- else # is something falsish
119
- []
120
- end
121
-
101
+ options = args.extract_options!
102
+ codes = args.empty? ? #{code_attr.pluralize.camelize}::All : args
103
+ include_nil = !!options[:include_nil]
122
104
 
123
- # If starts with 'i18n.' it is considered an I18n key, else the label itself
124
105
  options = translate_#{code_attr}(codes, build: :zip)
125
- if include_empty
126
- label = I18n.t(label[5..-1], locale: locale) if label.starts_with?('i18n.')
127
- options.unshift [label, value]
128
- end
106
+ options.unshift [I18n.t(CodeBox.i18n_empty_options_key), nil] if include_nil
129
107
 
130
108
  options
131
109
  end
@@ -197,11 +175,14 @@ module CodeBox
197
175
  raise ArgumentError, "'#{model_type}' is not a valid type. Use :active_record or :poro(default) instead"
198
176
  end
199
177
 
200
- define_codes(*codes, define_test_methods) unless codes.empty?
178
+ define_codes(*codes)
201
179
  end
202
180
 
203
- def define_codes(*codes, define_test_methods)
181
+ def define_codes(*codes)
182
+ return if codes.empty?
183
+
204
184
  # --- Define the code constants...
185
+
205
186
  code_attr = self._code_box_code_attr_name
206
187
  model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
207
188
 
@@ -223,27 +204,8 @@ module CodeBox
223
204
  codes_module.const_set('All', constants.values.compact)
224
205
  end
225
206
 
226
-
227
- # Define test methods for each code like e.g.
228
- # def married?
229
- # code == Codes::Married
230
- # end
231
- if define_test_methods
232
- method_prefix = CodeBox.test_method_prefix
233
-
234
- codes.each do |code|
235
- method_name = "#{method_prefix}#{code.to_s}"
236
- class_eval <<-CODE
237
- def #{method_name}?
238
- #{code_attr} == #{module_name}::#{code.to_s.camelize}
239
- end
240
- CODE
241
- end
242
- end
243
-
244
207
  return if model_type == :active_record
245
208
 
246
-
247
209
  # --- Define the code instance constants...
248
210
 
249
211
  constants = {}
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module CodeBox
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/code-box.rb CHANGED
@@ -5,9 +5,8 @@ require 'code-box/acts_as_code'
5
5
 
6
6
  module CodeBox
7
7
  Config = {
8
- i18n_model_segment: :activerecord,
9
- i18n_empty_options_key: 'shared.options.pls_select',
10
- test_method_prefix: '',
8
+ :i18n_model_segment => :activerecord,
9
+ :i18n_empty_options_key => 'shared.options.pls_select',
11
10
  }
12
11
 
13
12
  def i18n_model_segment=(segment)
@@ -26,15 +25,5 @@ module CodeBox
26
25
  Config[:i18n_empty_options_key]
27
26
  end
28
27
 
29
- def test_method_prefix=(prefix)
30
- Config[:test_method_prefix] = (prefix.nil? ? '' : prefix.strip)
31
- end
32
-
33
- def test_method_prefix
34
- Config[:test_method_prefix]
35
- end
36
-
37
- module_function :i18n_model_segment, :i18n_model_segment=,
38
- :i18n_empty_options_key, :i18n_empty_options_key=,
39
- :test_method_prefix, :test_method_prefix=
28
+ module_function :i18n_model_segment, :i18n_model_segment=, :i18n_empty_options_key, :i18n_empty_options_key=
40
29
  end
@@ -57,15 +57,15 @@ class TestActsAsCode < Test::Unit::TestCase
57
57
 
58
58
  def test_code_instance_constant_definitions_w_define_code
59
59
  # Constants
60
- assert Codes::CivilStatus.const_defined?('Single')
61
- assert Codes::CivilStatus.const_defined?('Married')
62
- assert Codes::CivilStatus.const_defined?('All')
60
+ assert Codes::CivilStatusUseDefine.const_defined?('Single')
61
+ assert Codes::CivilStatusUseDefine.const_defined?('Married')
62
+ assert Codes::CivilStatusUseDefine.const_defined?('All')
63
63
 
64
64
  # Constants-Values
65
- assert_equal [Codes::CivilStatus::Single, Codes::CivilStatus::Married], Codes::CivilStatus::All
66
- assert_equal [Codes::CivilStatus::Single, Codes::CivilStatus::Married], Codes::CivilStatus.all
67
- assert_equal Codes::CivilStatus::Single.code, 'single'
68
- assert_equal Codes::CivilStatus::Married.code, 'married'
65
+ assert_equal [Codes::CivilStatusUseDefine::Single, Codes::CivilStatusUseDefine::Married], Codes::CivilStatusUseDefine::All
66
+ assert_equal [Codes::CivilStatusUseDefine::Single, Codes::CivilStatusUseDefine::Married], Codes::CivilStatusUseDefine.all
67
+ assert_equal Codes::CivilStatusUseDefine::Single.code, 'single'
68
+ assert_equal Codes::CivilStatusUseDefine::Married.code, 'married'
69
69
  end
70
70
 
71
71
  def test_code_translation
@@ -79,29 +79,9 @@ class TestActsAsCode < Test::Unit::TestCase
79
79
  options_array = Codes::CivilStatus.build_select_options
80
80
  assert_equal options_array.size, 2
81
81
 
82
- options_array = Codes::CivilStatus.build_select_options(include_empty: true)
83
- assert_equal options_array.size, 3
84
- assert_equal options_array.first[1], nil
85
- end
86
-
87
- def test_options_building_with_text_label
88
- options_array = Codes::CivilStatus.build_select_options(include_empty: { label: 'MyLabel' })
89
- assert_equal options_array.size, 3
90
- assert_equal options_array.first[0], 'MyLabel'
91
- assert_equal options_array.first[1], nil
92
- end
93
-
94
- def test_options_building_with_custom_value
95
- options_array = Codes::CivilStatus.build_select_options(include_empty: { value: 'all' })
96
- assert_equal options_array.size, 3
97
- assert_equal options_array.first[1], 'all'
98
- end
99
-
100
- def test_options_building_with_custom_label_and_value
101
- options_array = Codes::CivilStatus.build_select_options(include_empty: { label: 'Yaiii', value: 'all' })
82
+ options_array = Codes::CivilStatus.build_select_options(include_nil: true)
102
83
  assert_equal options_array.size, 3
103
- assert_equal options_array.first[0], 'Yaiii'
104
- assert_equal options_array.first[1], 'all'
84
+ puts options_array: options_array
105
85
  end
106
86
 
107
87
  end
metadata CHANGED
@@ -1,94 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Martin Schweizer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2013-09-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
- version: '3.0'
19
+ version: '4.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
- version: '3.0'
26
+ version: '4.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activerecord
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: '3.0'
33
+ version: '4.0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: '3.0'
40
+ version: '4.0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: pry
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '>='
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '>='
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  description: Specify attributes as code and provide lookup by I18n-, cache- or associated
@@ -120,6 +109,7 @@ files:
120
109
  - test/resources/schema.rb
121
110
  homepage: http://github.com/verticonaut/code-box
122
111
  licenses: []
112
+ metadata: {}
123
113
  post_install_message:
124
114
  rdoc_options:
125
115
  - --charset
@@ -128,22 +118,20 @@ rdoc_options:
128
118
  require_paths:
129
119
  - lib
130
120
  required_ruby_version: !ruby/object:Gem::Requirement
131
- none: false
132
121
  requirements:
133
- - - ! '>='
122
+ - - '>='
134
123
  - !ruby/object:Gem::Version
135
124
  version: '0'
136
125
  required_rubygems_version: !ruby/object:Gem::Requirement
137
- none: false
138
126
  requirements:
139
- - - ! '>='
127
+ - - '>='
140
128
  - !ruby/object:Gem::Version
141
129
  version: '0'
142
130
  requirements: []
143
131
  rubyforge_project:
144
- rubygems_version: 1.8.21
132
+ rubygems_version: 2.0.3
145
133
  signing_key:
146
- specification_version: 3
134
+ specification_version: 4
147
135
  summary: Specify attributes as code and provide lookup by I18n-, cache- or associated
148
136
  and support for building code classes.
149
137
  test_files:
@@ -154,4 +142,3 @@ test_files:
154
142
  - test/resources/locale/en.yml
155
143
  - test/resources/models.rb
156
144
  - test/resources/schema.rb
157
- has_rdoc: