code-box 0.6.0 → 1.0.0

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