inum 5.1.1 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92efa81161155f336cb84f1e2f6ae43dfa7e3dbe
4
- data.tar.gz: f9e80cd38edae6fca9d49b70561d90833addec3c
3
+ metadata.gz: abafc6e9e99d1761840987fb78cf6a860997a556
4
+ data.tar.gz: b82eebf3ec9240161f7a6510ce04481032aaebea
5
5
  SHA512:
6
- metadata.gz: 8bc684c912cd842f6e54b0f4f7a1591e33949a2681ef703f4266ce6c2c3c8dbb96e872df1794a6d9870832cf1ec00569287b11e327484778c10a00580bd832d7
7
- data.tar.gz: ea2395a6da2f5c111412726a62c1f1af97a90d370875d725f6c98c3e5326dbf0fa397e5f886df951389f34b1f2021c9c86ba476236dde87eb6dd71c48639229b
6
+ metadata.gz: 6fe09e0679c6a2ef1397d49b17c8031ced46758a1d3ebfa2db71375c75e85ae274cdb45a9ee9edac4f4e1432b01da15b49b6e89cd79cf8849d08418244b5bed3
7
+ data.tar.gz: 0ec254f25e567994628f4881d321bc6ac87f62c530e5049a13927946ef3f14e757a9971620b380ddcff1981df48841d66f9fb21c72b92479269311ef7f37ac6d
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -7,6 +7,7 @@ rvm:
7
7
  - 2.1.0
8
8
  - 2.2.0
9
9
  - 2.3.0
10
+ - 2.4.0
10
11
  env:
11
12
  - RAILS_VERSION=3.2.20
12
13
  - RAILS_VERSION=4.1.7
@@ -15,3 +16,6 @@ matrix:
15
16
  include:
16
17
  - rvm: 2.3.0
17
18
  env: RAILS_VERSION=5.0.0
19
+ - rvm: 2.4.0
20
+ env: RAILS_VERSION=5.1.0
21
+
data/README.md CHANGED
@@ -3,6 +3,10 @@
3
3
  Inum(enumerated type of Integer) provide a Java-enum-like Enum.
4
4
  Inum has a function to localize by i18n.
5
5
 
6
+ [![Build Status](https://travis-ci.org/alfa-jpn/inum.svg?branch=master)](https://travis-ci.org/alfa-jpn/inum)
7
+ [![Coverage Status](https://coveralls.io/repos/github/alfa-jpn/inum/badge.svg?branch=master)](https://coveralls.io/github/alfa-jpn/inum?branch=master)
8
+ [![Code Climate](https://codeclimate.com/github/alfa-jpn/inum/badges/gpa.svg)](https://codeclimate.com/github/alfa-jpn/inum)
9
+
6
10
  ## Installation
7
11
 
8
12
  Add this line to your application's Gemfile:
@@ -62,13 +66,13 @@ AnimeTypes.length # => 3
62
66
  AnimeTypes.labels # => [:EVANGELION, :HARUHI, :NYARUKO]
63
67
  AnimeTypes.values # => [0, 1, 2]
64
68
 
65
- # Get collection array.
66
- AnimeTypes.collection # => [["EVANGELION", 0], ["HARUHI", 1], ....]
69
+ # Get form items.
70
+ AnimeTypes.form_items # => [['エヴァンゲリオン', 'EVANGELION'], ['涼宮ハルヒの憂鬱', 'HARUHI'], ....]
67
71
 
68
- # collection usually use with some rails view helpers.
69
- # f.select :name, Enum.collection
70
- # f.select :name, Enum.collection(except:[:EVANGELION])
71
- # f.select :name, Enum.collection(only:[:EVANGELION])
72
+ # form_items usually use with some rails view helpers.
73
+ # f.select :name, Enum.form_items
74
+ # f.select :name, Enum.form_items(except:[:EVANGELION])
75
+ # f.select :name, Enum.form_items(only:[:EVANGELION])
72
76
  ```
73
77
 
74
78
  can use Enumerable and Comparable.
@@ -134,4 +138,8 @@ end
134
138
  4. Push to the branch (`git push origin my-new-feature`)
135
139
  5. Create new Pull Request
136
140
 
137
- # I have poor English. Please help meeeeeeeee!!!!
141
+ ### Testing
142
+
143
+ ```shell
144
+ $ bundle exec rspec
145
+ ```
@@ -4,29 +4,30 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'inum/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "inum"
7
+ spec.name = 'inum'
8
8
  spec.version = Inum::VERSION
9
- spec.authors = ["alfa-jpn"]
10
- spec.email = ["alfa.jpn@gmail.com"]
11
- spec.description = "Inum(enumerated type of Integer) provide a java-Enum-like."
12
- spec.summary = "Inum(enumerated type of Integer) provide a java-Enum-like."
13
- spec.homepage = "https://github.com/alfa-jpn/inum"
14
- spec.license = "MIT"
9
+ spec.authors = ['alfa-jpn']
10
+ spec.email = ['alfa.jpn@gmail.com']
11
+ spec.description = 'Inum(enumerated type of Integer) provide a java-Enum-like.'
12
+ spec.summary = 'Inum(enumerated type of Integer) provide a java-Enum-like.'
13
+ spec.homepage = 'https://github.com/alfa-jpn/inum'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.required_ruby_version = ">= 1.9.3"
21
+ spec.required_ruby_version = '>= 1.9.3'
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.5"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec", ">= 3.0.0"
26
- spec.add_development_dependency "yard"
27
- spec.add_development_dependency "activerecord"
28
- spec.add_development_dependency "sqlite3"
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '>= 3.0.0'
26
+ spec.add_development_dependency 'yard'
27
+ spec.add_development_dependency 'activerecord'
28
+ spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'coveralls'
29
30
 
30
- spec.add_dependency "i18n"
31
- spec.add_dependency "activesupport"
31
+ spec.add_dependency 'i18n'
32
+ spec.add_dependency 'activesupport'
32
33
  end
@@ -2,9 +2,8 @@ module Inum
2
2
  require 'active_support/inflector'
3
3
  require 'i18n'
4
4
 
5
- # InumBase class.
6
- #
7
- # @abstract Inum class should be a inheritance of Inum::Base.
5
+ # Inum base class.
6
+ # @abstract Inum::Base class should be a inheritance of Inum::Base.
8
7
  # @example
9
8
  # class FruitType < Inum::Base
10
9
  # define :APPLE, 0
@@ -25,9 +24,9 @@ module Inum
25
24
  @label_string = label.to_s.freeze
26
25
  @label_downcase = @label_string.downcase.freeze
27
26
  @label_upcase = @label_string.upcase.freeze
28
- @label_underscore = ActiveSupport::Inflector.underscore(label).gsub('/', '.').freeze
27
+ @label_underscore = ActiveSupport::Inflector.underscore(@label_string).freeze
29
28
  @value = value.freeze
30
- @i18n_namespace = (self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).gsub('/', '.') : '').freeze
29
+ @i18n_namespace = (self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).tr('/', '.') : '').freeze
31
30
  end
32
31
 
33
32
  # Compare object.
@@ -35,7 +34,7 @@ module Inum
35
34
  # @param object [Object] parsable object.
36
35
  # @return [Integer] same normal <=>.
37
36
  def <=> (object)
38
- if other = self.class.parse(object)
37
+ if (other = self.class.parse(object))
39
38
  @value <=> other.to_i
40
39
  else
41
40
  nil
@@ -102,7 +101,7 @@ module Inum
102
101
  end
103
102
 
104
103
  # Translate Enum to localized string.(use i18n)
105
- # @note find default `Namespace.Classname.EnumMember`
104
+ # @note find default `namespace.enum_class_name.enum_label`
106
105
  #
107
106
  # @return [String] localized string of Enum.
108
107
  def translate
@@ -118,33 +117,33 @@ module Inum
118
117
  end
119
118
  alias_method :to_i, :value
120
119
 
121
- # Get collection.
120
+ # Execute the yield(block) with each member of enum.
121
+ #
122
+ # @yield [enum] execute the block with enums.
123
+ # @yieldparam [Inum::Base] enum enum.
124
+ def self.each(&block)
125
+ @enums.each(&block)
126
+ end
127
+
128
+ # Get items for rails form.
122
129
  # @note Type of usable with a Rails form helper.
123
- # @exapmle
124
- # f.select :name, Enum.collection
125
- # f.select :name, Enum.collection(except:[:HOGE]) # Except Enum::HOGE
126
- # f.select :name, Enum.collection(only:[:HOGE, :FUGA]) # Only Enum::HOGE and Enum::FUGA
130
+ # @example
131
+ # f.select :name, Enum.form_items
132
+ # f.select :name, Enum.form_items(except:[:HOGE]) # Except Enum::HOGE
133
+ # f.select :name, Enum.form_items(only:[:HOGE, :FUGA]) # Only Enum::HOGE and Enum::FUGA
127
134
  #
128
135
  # @param option [Hash] Options.
129
136
  # @option option [Array<Symbol>] except Except enum.
130
137
  # @option option [Array<Symbol>] only Limit enum.
131
138
  # @return [Array<Array>] collection. ex `[["HOGE", 0], ["FUGA", 1]]`
132
- def self.collection(option = {})
139
+ def self.form_items(option = {})
133
140
  map { |e|
134
141
  next if option[:except] and option[:except].include?(e.label)
135
142
  next if option[:only] and !option[:only].include?(e.label)
136
- [e.translate, e.value]
143
+ [e.translate, e.to_s]
137
144
  }.compact
138
145
  end
139
146
 
140
- # Execute the yield(block) with each member of enum.
141
- #
142
- # @yield [enum] execute the block with enums.
143
- # @yieldparam [Inum::Base] enum enum.
144
- def self.each(&block)
145
- @enums.each(&block)
146
- end
147
-
148
147
  # Override the rule of i18n keys.
149
148
  # @abstract if change the rule of i18n keys.
150
149
  #
@@ -180,12 +179,12 @@ module Inum
180
179
  parse(object.to_i)
181
180
  else
182
181
  underscore = object.underscore
183
- find {|e| e.underscore == underscore}
182
+ find { |e| e.underscore == underscore }
184
183
  end
185
184
  when Symbol
186
185
  parse(object.to_s)
187
186
  when Integer
188
- find {|e| e.value == object}
187
+ find { |e| e.value == object }
189
188
  when self
190
189
  object
191
190
  else
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = '5.1.1'
2
+ VERSION = '5.2.0'
3
3
  end
@@ -1,8 +1,7 @@
1
- require 'rspec'
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Inum::ActiveRecordMixin do
5
- create_temp_table(:tvs){|t| t.integer :anime}
4
+ create_temp_table(:tvs) { |t| t.integer :anime }
6
5
 
7
6
  before :each do
8
7
  class Anime < Inum::Base
@@ -23,7 +22,7 @@ describe Inum::ActiveRecordMixin do
23
22
  end
24
23
 
25
24
  let :bind_class do
26
- TV.tap {|klass| klass.instance_eval{ bind_inum :anime, Anime } }
25
+ TV.tap { |klass| klass.instance_eval{ bind_inum :anime, Anime } }
27
26
  end
28
27
 
29
28
  let :instance do
@@ -99,11 +98,23 @@ describe Inum::ActiveRecordMixin do
99
98
  }.from(type.to_i).to(Anime.parse(1).to_i)
100
99
  end
101
100
  end
101
+
102
+ context 'When invalid value' do
103
+ let(:target){ 'Hoge' }
104
+
105
+ it 'set value.' do
106
+ expect {
107
+ instance.anime = target
108
+ }.to change {
109
+ instance.send(:read_attribute, :anime)
110
+ }.from(type.to_i).to(nil)
111
+ end
112
+ end
102
113
  end
103
114
 
104
115
  describe '#getter' do
105
116
  it 'Return enum' do
106
- expect(instance.anime.instance_of?(Anime)).to be_truthy
117
+ expect(instance.anime).to be_instance_of(Anime)
107
118
  end
108
119
  end
109
120
 
@@ -114,7 +125,7 @@ describe Inum::ActiveRecordMixin do
114
125
 
115
126
  describe 'Comparison methods' do
116
127
  it 'Will be defined with prefix.' do
117
- expect(bind_class.method_defined?(:prefix_nyaruko?)).to be_truthy
128
+ expect(bind_class.method_defined?(:prefix_nyaruko?)).to be_truthy
118
129
  end
119
130
  end
120
131
  end
@@ -1,4 +1,3 @@
1
- require 'rspec'
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Inum::Base do
@@ -221,21 +220,39 @@ describe Inum::Base do
221
220
  end
222
221
  end
223
222
 
224
- describe '.collection' do
223
+ describe '.new' do
224
+ it 'Can not create a instance of enum.' do
225
+ expect{ Anime.new(:NICONICO, 2525) }.to raise_error(NoMethodError)
226
+ end
227
+ end
228
+
229
+ describe '.each' do
230
+ it 'Execute block with a right order.' do
231
+ count = 0
232
+ orders = [Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::BakuOn]
233
+
234
+ Anime.each do |enum|
235
+ expect(enum).to eq(orders[count])
236
+ count += 1
237
+ end
238
+ end
239
+ end
240
+
241
+ describe '.form_items' do
225
242
  subject do
226
- allow(I18n).to receive(:t) { 't' }
227
- Anime.collection(option)
243
+ allow(I18n).to receive(:t) { |key| "t-key-#{key}" }
244
+ Anime.form_items(option)
228
245
  end
229
246
 
230
247
  let(:option) { Hash.new }
231
248
 
232
249
  it 'Return all item array.' do
233
250
  expect(subject).to match_array([
234
- ['t', 0],
235
- ['t', 1],
236
- ['t', 2],
237
- ['t', 3],
238
- ['t', 5]
251
+ ['t-key-anime.nyaruko', 'Nyaruko'],
252
+ ['t-key-anime.muromisan', 'Muromisan'],
253
+ ['t-key-anime.nourin', 'Nourin'],
254
+ ['t-key-anime.kmb', 'KMB'],
255
+ ['t-key-anime.baku_on', 'BakuOn'],
239
256
  ])
240
257
  end
241
258
 
@@ -243,7 +260,7 @@ describe Inum::Base do
243
260
  let(:option) { {only: [:KMB]} }
244
261
 
245
262
  it 'Return only selected item array.' do
246
- expect(subject).to match_array([['t', 3]])
263
+ expect(subject).to match_array([['t-key-anime.kmb', 'KMB']])
247
264
  end
248
265
  end
249
266
 
@@ -252,33 +269,15 @@ describe Inum::Base do
252
269
 
253
270
  it 'Return only except item array.' do
254
271
  expect(subject).to match_array([
255
- ['t', 0],
256
- ['t', 1],
257
- ['t', 2],
258
- ['t', 5]
272
+ ['t-key-anime.nyaruko', 'Nyaruko'],
273
+ ['t-key-anime.muromisan', 'Muromisan'],
274
+ ['t-key-anime.nourin', 'Nourin'],
275
+ ['t-key-anime.baku_on', 'BakuOn'],
259
276
  ])
260
277
  end
261
278
  end
262
279
  end
263
280
 
264
- describe '.new' do
265
- it 'Can not create a instance of enum.' do
266
- expect{ Anime.new(:NICONICO, 2525) }.to raise_error(NoMethodError)
267
- end
268
- end
269
-
270
- describe '.each' do
271
- it 'Execute block with a right order.' do
272
- count = 0
273
- orders = [Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::BakuOn]
274
-
275
- Anime.each do |enum|
276
- expect(enum).to eq(orders[count])
277
- count += 1
278
- end
279
- end
280
- end
281
-
282
281
  describe '.labels' do
283
282
  it 'Return array of label.' do
284
283
  expect(Anime.labels).to match_array([:Nyaruko, :Muromisan, :Nourin, :KMB, :BakuOn])
@@ -3,27 +3,17 @@ require 'bundler/setup'
3
3
  require 'active_record'
4
4
  require 'inum'
5
5
 
6
+ require 'rspec'
7
+ require 'spec_helpers/active_record_helper'
8
+ require 'coveralls'
9
+
6
10
  RSpec.configure do |config|
7
- config.color = true
11
+ config.color = true
8
12
  config.mock_framework = :rspec
9
- config.before(:all) {
13
+ config.before(:all) do
10
14
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
11
15
  I18n.enforce_available_locales = false
12
- }
13
- end
14
-
15
- def create_temp_table(name, &block)
16
- raise 'No Block given!' unless block_given?
17
-
18
- before :all do
19
- migration = ActiveRecord::Migration.new
20
- migration.verbose = false
21
- migration.create_table name, &block
22
- end
23
-
24
- after :all do
25
- migration = ActiveRecord::Migration.new
26
- migration.verbose = false
27
- migration.drop_table name
28
16
  end
29
17
  end
18
+
19
+ Coveralls.wear!
@@ -0,0 +1,18 @@
1
+ # Create table.
2
+ # @param [String] name table name.
3
+ # @yield [t] table dsl block.
4
+ def create_temp_table(name, &block)
5
+ raise 'No Block given!' unless block_given?
6
+
7
+ before :all do
8
+ migration = ActiveRecord::Migration.new
9
+ migration.verbose = false
10
+ migration.create_table name, &block
11
+ end
12
+
13
+ after :all do
14
+ migration = ActiveRecord::Migration.new
15
+ migration.verbose = false
16
+ migration.drop_table name
17
+ end
18
+ end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inum
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.1
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2017-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: i18n
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -129,6 +143,7 @@ executables: []
129
143
  extensions: []
130
144
  extra_rdoc_files: []
131
145
  files:
146
+ - ".coveralls.yml"
132
147
  - ".gitignore"
133
148
  - ".travis.yml"
134
149
  - Gemfile
@@ -143,6 +158,8 @@ files:
143
158
  - spec/inum/active_record_mixin_spec.rb
144
159
  - spec/inum/base_spec.rb
145
160
  - spec/spec_helper.rb
161
+ - spec/spec_helpers/active_record_helper.rb
162
+ - vendor/.keep
146
163
  homepage: https://github.com/alfa-jpn/inum
147
164
  licenses:
148
165
  - MIT
@@ -163,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
180
  version: '0'
164
181
  requirements: []
165
182
  rubyforge_project:
166
- rubygems_version: 2.5.1
183
+ rubygems_version: 2.5.2
167
184
  signing_key:
168
185
  specification_version: 4
169
186
  summary: Inum(enumerated type of Integer) provide a java-Enum-like.
@@ -171,4 +188,4 @@ test_files:
171
188
  - spec/inum/active_record_mixin_spec.rb
172
189
  - spec/inum/base_spec.rb
173
190
  - spec/spec_helper.rb
174
- has_rdoc:
191
+ - spec/spec_helpers/active_record_helper.rb