inum 1.2.0 → 1.3.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: 781689678af4d4332e501ba581e4fcb3ce02e238
4
- data.tar.gz: c9d80cf8f71ac711992dc776f6d5d9db2dd462f3
3
+ metadata.gz: 0a45543edcaeff37b2532ff978a90bd3eb773bc2
4
+ data.tar.gz: 357be60fffc7baec45c791aebd39c9da9990a2cb
5
5
  SHA512:
6
- metadata.gz: b90ff4e1a44db7c92ed86c758c93bea51241acd42e541fa2b7a42c10ca1188db30a4e223ac0089d679dc7cced45e5cf726a358f8bf33b3bd64b83865b7c1fac8
7
- data.tar.gz: f8300442ef7814585669a34ce6bb85acacfede154c663ff5b686b5ded203c1d0e6fe83661ceb7abe672da1fa3bc598c10c227b11f425ba2b21b3e0f78de29c56
6
+ metadata.gz: 82e42a428ed15ef2f98b434c72fcaf56fe6a5431d23faa81e0f16632e39792ae26e029ce3d67ec9f381624b887f56f44f3067d5d39ceec6f915d3c6ef7721c58
7
+ data.tar.gz: 7e7301e5e39d9032efe85e7fd49e817d10ccea32ae2b8ff890aa366c8502ec809c36bf481c8fa81b2159fdc5e56bf3a59ea5a8abc94b6d2b568e6eba51742515
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Inum
2
2
 
3
3
  Inum(enumerated type of Integer) provide a Java-enum-like Enum.
4
+ Inum has a function to localize by i18n.
4
5
 
5
6
  ## Installation
6
7
 
@@ -42,6 +43,7 @@ How to use Enum(Inum).
42
43
  ``` ruby
43
44
  p AnimeType::EVANGELION # => EVANGELION
44
45
  p AnimeType::EVANGELION.to_i # => 0
46
+ p AnimeType::EVANGELION.to_t # => エヴァンゲリオン (i18n find `anime_type.evangelion`.)
45
47
 
46
48
  # parse object to instance of AnimeType.
47
49
  # object can use class of Symbol or String or Integer or Self.
@@ -53,7 +55,11 @@ p AnimeType::HARUHI.eql?('HARUHI') # => true (eql? can compare all parsable ob
53
55
  p AnimeType::HARUHI + 1 # => NYARUKO
54
56
  p AnimeType::NYARUKO - 1 # => HARUHI
55
57
 
58
+ # can use each method.
59
+ AnimeType::each {|enum| p enum }
60
+
56
61
  ```
62
+ can use Enumerable and Comparable.
57
63
 
58
64
  - more detail is [Class::Base](http://rubydoc.info/github/alfa-jpn/inum/Inum/Base)
59
65
 
@@ -87,6 +93,16 @@ p anime.haruhi? # => true
87
93
 
88
94
  ```
89
95
 
96
+ ### Localize(i18n)
97
+ to_t methods localize enum by i18n.
98
+ default i18n key is name_space.class_name.enum_member_label.
99
+ If change default, Override i18n_key class method.
100
+ ``` ruby
101
+ # default i18n_key.
102
+ def self.i18n_key(label)
103
+ Inum::Utils::underscore("#{self.class.name}::#{label}")
104
+ end
105
+ ```
90
106
 
91
107
  ## API DOCUMENT
92
108
 
@@ -99,3 +115,5 @@ p anime.haruhi? # => true
99
115
  3. Commit your changes (`git commit -am 'Add some feature'`)
100
116
  4. Push to the branch (`git push origin my-new-feature`)
101
117
  5. Create new Pull Request
118
+
119
+ # I have poor English. Please help meeeeeeeee!!!!
data/inum.gemspec CHANGED
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "yard"
25
+
26
+ spec.add_dependency "i18n", "~> 0.6.5"
25
27
  end
data/lib/inum/base.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  module Inum
2
+ require 'i18n'
3
+ require 'inum/utils'
4
+
2
5
  # InumBase class.
3
6
  #
4
7
  # @abstract Inum class should be a inheritance of Inum::Base.
@@ -9,7 +12,9 @@ module Inum
9
12
  # define_enum :ORANGE, 2
10
13
  # end
11
14
  class Base
12
-
15
+ extend Enumerable
16
+ include Comparable
17
+
13
18
  private_class_method :new
14
19
 
15
20
  # initialize Inum::Base with value.
@@ -20,8 +25,18 @@ module Inum
20
25
  def initialize(label, value)
21
26
  @label = label
22
27
  @value = value
28
+
29
+ @localized_enum = I18n.t(self.class.i18n_key(label))
23
30
  end
24
-
31
+
32
+ # Compare object.
33
+ #
34
+ # @param object [Object] parsable object.
35
+ # @return [Integer] same normal <=>.
36
+ def <=> (object)
37
+ @value <=> self.class.parse(object).to_i
38
+ end
39
+
25
40
  # plus object.
26
41
  #
27
42
  # @param value [Integer] plus value.(call to_i in this method.)
@@ -60,6 +75,21 @@ module Inum
60
75
  @label.to_s
61
76
  end
62
77
 
78
+ # Translate Enum to localized string.(use i18n)
79
+ # @note find default `Namespace.Classname.EnumMember`
80
+ #
81
+ # @return [String] localized string of Enum.
82
+ def to_t
83
+ @localized_enum
84
+ end
85
+
86
+ # Execute the yield(block) with each member of enum.
87
+ #
88
+ # @yield [enum] instance of enum.
89
+ def self.each
90
+ defined_enums.each_key {|key| yield parse(key.to_s)}
91
+ end
92
+
63
93
  # get all labels of Enum.
64
94
  #
65
95
  # @return [Array<Symbol>] all labels of Enum.
@@ -133,6 +163,16 @@ module Inum
133
163
  self.const_get(:DEFINED_ENUMS)
134
164
  end
135
165
 
166
+ # get key for I18n.t method.
167
+ # @note default: Namespace.Classname.Label(label of enum member.)
168
+ #
169
+ # @param label [Symbol] label of enum member.
170
+ # @return [String] key for I18n.t method.
171
+ # @abstract If change key from the default.
172
+ def self.i18n_key(label)
173
+ Inum::Utils::underscore("#{self.class.name}::#{label}")
174
+ end
175
+
136
176
  # call after inherited.
137
177
  # @note Define hash of :DEFINED_ENUMS in child.
138
178
  def self.inherited(child)
data/lib/inum/utils.rb CHANGED
@@ -9,6 +9,7 @@ module Inum
9
9
  def self.underscore(camel_cased_word)
10
10
  word = camel_cased_word.to_s.dup
11
11
 
12
+ word.gsub!('::', '.')
12
13
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
13
14
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
14
15
  word.tr!('-', '_')
data/lib/inum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -2,8 +2,8 @@ require 'rspec'
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Inum::Base do
5
-
6
- it 'define_method validate correct' do
5
+
6
+ it 'define_enum validate correct' do
7
7
  # correct.
8
8
  expect{
9
9
  Class.new(Inum::Base) { define_enum :REDBULL, 0 }
@@ -40,8 +40,8 @@ describe Inum::Base do
40
40
  }
41
41
  }.to raise_error
42
42
  end
43
-
44
- it 'define_method called without value, value is autoincrement.' do
43
+
44
+ it 'define_enum called without value, value is autoincrement.' do
45
45
  enum = Class.new(Inum::Base) do
46
46
  define_enum :REDBULL
47
47
  define_enum :MONSTER
@@ -52,9 +52,18 @@ describe Inum::Base do
52
52
  expect(enum::MONSTER.to_i).to eq(1)
53
53
  expect(enum::BURN.to_i).to eq(2)
54
54
  end
55
-
55
+
56
+ it 'i18n.t called when call define_enum.' do
57
+ I18n.should_receive(:t).with('class.redbull')
58
+
59
+ Class.new(Inum::Base) do
60
+ define_enum :REDBULL
61
+ end
62
+ end
63
+
56
64
  context 'define class of extended Inum::Base,' do
57
65
  before(:each) do
66
+ I18n.stub(:t).and_return('Good drink!')
58
67
  @enum = Class.new(Inum::Base) do
59
68
  define_enum :REDBULL, 0
60
69
  define_enum :MONSTER, 1
@@ -76,7 +85,13 @@ describe Inum::Base do
76
85
  it 'A enum instance is equal instance.' do
77
86
  expect( @enum::BURN.equal?(@enum::BURN )).to be_true
78
87
  end
79
-
88
+
89
+ it '<=> return a correct value.' do
90
+ expect( (@enum::REDBULL <=> 1) < 0 ).to be_true
91
+ expect( (@enum::REDBULL <=> 0) == 0 ).to be_true
92
+ expect( (@enum::MONSTER <=> 0) > 0 ).to be_true
93
+ end
94
+
80
95
  it '+ return a correct Inum.' do
81
96
  expect(@enum::REDBULL + 1).to eq(@enum::MONSTER)
82
97
  expect(@enum::REDBULL + @enum::MONSTER).to eq(@enum::MONSTER)
@@ -86,7 +101,11 @@ describe Inum::Base do
86
101
  expect(@enum::BURN - 1).to eq(@enum::MONSTER)
87
102
  expect(@enum::BURN - @enum::MONSTER).to eq(@enum::MONSTER)
88
103
  end
89
-
104
+
105
+ it 'Comparable module enable.' do
106
+ expect(@enum::REDBULL.between?(0,1)).to be_true
107
+ end
108
+
90
109
  it 'eql? return a correct result.' do
91
110
  expect( @enum::REDBULL.eql?(0) ).to be_true
92
111
  expect( @enum::REDBULL.eql?(1) ).to be_false
@@ -99,7 +118,26 @@ describe Inum::Base do
99
118
  it 'to_s return string.' do
100
119
  expect( @enum::MONSTER.to_s ).to eq('MONSTER')
101
120
  end
102
-
121
+
122
+ it 'to_t return localized string.' do
123
+ expect( @enum::REDBULL.to_t ).to eq('Good drink!')
124
+ end
125
+
126
+ it 'each can execute block with enum' do
127
+ count = 0
128
+ expect{
129
+ @enum::each do |enum|
130
+ expect(enum.instance_of?(@enum)).to be_true
131
+ count += 1
132
+ end
133
+ }.to change{count}.by(3)
134
+ end
135
+
136
+ it 'Enumerable module enable.' do
137
+ expect(@enum::count).to eq(3)
138
+ expect(@enum::include?(@enum::REDBULL)).to be_true
139
+ end
140
+
103
141
  it 'labels return Array<Symbol>.' do
104
142
  expect(@enum::labels.length).to eq(3)
105
143
  expect(@enum::labels.instance_of?(Array)).to be_true
@@ -9,6 +9,12 @@ describe Inum::Utils do
9
9
  expect(Inum::Utils::underscore('CamelCaseWord')).to eq('camel_case_word')
10
10
 
11
11
  expect(Inum::Utils::underscore('Came1Case')).to eq('came1_case')
12
+
13
+ expect(Inum::Utils::underscore('UNDER_SCORE')).to eq('under_score')
14
+ expect(Inum::Utils::underscore('under_score')).to eq('under_score')
15
+
16
+ expect(Inum::Utils::underscore('name::space')).to eq('name.space')
17
+ expect(Inum::Utils::underscore('Name::Space')).to eq('name.space')
12
18
  end
13
19
 
14
20
  it 'underscore can convert Symbol to underscore_word.' do
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'bundler/setup'
3
3
  require 'inum/base'
4
4
  require 'inum/define_check_method'
5
5
  require 'inum/utils'
6
+ require 'i18n'
6
7
 
7
8
  RSpec.configure do |config|
8
9
  config.mock_framework = :rspec
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: 1.2.0
4
+ version: 1.3.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: 2013-11-27 00:00:00.000000000 Z
11
+ date: 2013-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.6.5
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.6.5
69
83
  description: Inum(enumerated type of Integer) provide a java-Enum-like.
70
84
  email:
71
85
  - a.nkmr.ja@gmail.com