inum 1.1.0 → 1.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: 13fa0a53e7d738142a3ada77e6ae500e1be64b74
4
- data.tar.gz: 0f69ba1695638ee4b501353e98da80bc8f48842c
3
+ metadata.gz: 781689678af4d4332e501ba581e4fcb3ce02e238
4
+ data.tar.gz: c9d80cf8f71ac711992dc776f6d5d9db2dd462f3
5
5
  SHA512:
6
- metadata.gz: b4c68d473c6edee840eb3e49e55e78c1e3cd8477f44fb8afec1afee047b4bf4c59cb881c834875701065cd04cdbecb4f29d7bb042a7f35fcdc383fc6904b4e92
7
- data.tar.gz: 18298d6d00e8b4c06312dbc8c33e9eab8081c7c2b1a028f8babddbea6ad170892a1c3abc2ebc3dfc17210f740738fc840f806b5a4b542df032837a3dc2580857
6
+ metadata.gz: b90ff4e1a44db7c92ed86c758c93bea51241acd42e541fa2b7a42c10ca1188db30a4e223ac0089d679dc7cced45e5cf726a358f8bf33b3bd64b83865b7c1fac8
7
+ data.tar.gz: f8300442ef7814585669a34ce6bb85acacfede154c663ff5b686b5ded203c1d0e6fe83661ceb7abe672da1fa3bc598c10c227b11f425ba2b21b3e0f78de29c56
data/lib/inum/base.rb CHANGED
@@ -10,17 +10,15 @@ module Inum
10
10
  # end
11
11
  class Base
12
12
 
13
- attr_accessor :value
14
13
  private_class_method :new
15
14
 
16
15
  # initialize Inum::Base with value.
17
- # @note Inum::Base can't call new method.
16
+ # @note The instance of Enum Member is singleton.
18
17
  #
18
+ # @param label [Symbol] label of Enum.
19
19
  # @param value [Integer] value of Enum.
20
- def initialize(value)
21
- unless self.class.defined_enums.has_value?(value)
22
- raise ArgumentError, "unknown value #{value}!"
23
- end
20
+ def initialize(label, value)
21
+ @label = label
24
22
  @value = value
25
23
  end
26
24
 
@@ -57,11 +55,18 @@ module Inum
57
55
 
58
56
  # Enum to String.
59
57
  #
60
- # @return [String] string value of Enum.
58
+ # @return [String] string value(label) of Enum.
61
59
  def to_s
62
- self.class.defined_enums.key(@value).to_s
60
+ @label.to_s
63
61
  end
64
62
 
63
+ # get all labels of Enum.
64
+ #
65
+ # @return [Array<Symbol>] all labels of Enum.
66
+ def self.labels
67
+ defined_enums.keys
68
+ end
69
+
65
70
  # get Enum length.
66
71
  #
67
72
  # @return [Integer] count of Enums.
@@ -71,14 +76,14 @@ module Inum
71
76
 
72
77
  # return array of Enums.
73
78
  #
74
- # @return [Array<Array<String, Integer>>] sorted array of Enums.
79
+ # @return [Array<Array<Symbol, Integer>>] sorted array of Enums.
75
80
  def self.to_a
76
- defined_enums.map{|k,v| [k.to_s,v] }.sort{|a,b| a[1] <=> b[1] }
81
+ defined_enums.flatten(0).sort{|a,b| a[1] <=> b[1] }
77
82
  end
78
83
 
79
84
  # return hash of Enums.
80
85
  #
81
- # @return [Hash] hash of Enums.
86
+ # @return [Hash<Symbol, Integer>] hash of Enums.
82
87
  def self.to_h
83
88
  defined_enums.dup
84
89
  end
@@ -101,17 +106,26 @@ module Inum
101
106
  raise ArgumentError, "#{object} is nani?"
102
107
  end
103
108
  end
109
+
110
+ # get all values of Enum.
111
+ #
112
+ # @return [Array<Integer>] all values of Enum.
113
+ def self.values
114
+ defined_enums.values
115
+ end
104
116
 
105
117
  private
106
118
  # Define Enum in called class.
107
119
  #
108
- # @param name [String, Symbol] name of Enum.
109
- # @param value [Integer,Fixnum] value of Enum.(default:autoincrement for 0.)
110
- def self.define_enum(name, value = defined_enums.size)
111
- validate_enum_args!(name, value)
120
+ # @param label [Symbol] label of Enum.
121
+ # @param value [Integer] value of Enum.(default:autoincrement for 0.)
122
+ def self.define_enum(label, value = defined_enums.size)
123
+ value = value.to_i
124
+
125
+ validate_enum_args!(label, value)
112
126
 
113
- defined_enums[name] = value
114
- self.const_set(name, new(value))
127
+ defined_enums[label] = value
128
+ self.const_set(label, new(label, value))
115
129
  end
116
130
 
117
131
  # get hash of :DEFINED_ENUMS.
@@ -127,27 +141,23 @@ module Inum
127
141
 
128
142
  # Validate enum args, and raise exception.
129
143
  #
130
- # @param name [Object] name of Enum.
131
- # @param value [Object] value of Enum.
132
- def self.validate_enum_args!(name, value)
133
- unless name.instance_of?(String) or name.instance_of?(Symbol)
134
- raise ArgumentError, "#{name}<#{name.class}> isn't String or Symbol."
135
- end
136
-
137
- unless value.instance_of?(Integer) or value.instance_of?(Fixnum)
138
- raise ArgumentError, "#{value}<#{value.class}> isn't Integer or Fixnum."
144
+ # @param label [Object] label of Enum.
145
+ # @param value [Integer] value of Enum.
146
+ def self.validate_enum_args!(label, value)
147
+ unless label.instance_of?(Symbol)
148
+ raise ArgumentError, "The label(#{label}!) isn't instance of Symbol."
139
149
  end
140
150
 
141
- if name == :DEFINED_ENUMS
142
- raise ArgumentError, "#{name} is keyword."
151
+ if label == :DEFINED_ENUMS
152
+ raise ArgumentError, "The label(#{label}!) is keyword."
143
153
  end
144
154
 
145
- if defined_enums.has_key?(name)
146
- raise ArgumentError, "name(#{name}!) already exists!!"
155
+ if defined_enums.has_key?(label)
156
+ raise ArgumentError, "The label(#{label}!) already exists!!"
147
157
  end
148
158
 
149
159
  if defined_enums.has_value?(value)
150
- raise ArgumentError, "value(#{value}!) already exists!!"
160
+ raise ArgumentError, "The value(#{value}!) already exists!!"
151
161
  end
152
162
  end
153
163
  end
@@ -10,6 +10,8 @@ module Inum
10
10
  # end
11
11
  #
12
12
  module DefineCheckMethod
13
+ require 'inum/utils'
14
+
13
15
  # Define compare method in class.
14
16
  #
15
17
  # @param variable_name [String] name of variable.
@@ -17,7 +19,7 @@ module Inum
17
19
  def define_check_method(variable_name, enum_class)
18
20
  self.class_eval do
19
21
  enum_class::DEFINED_ENUMS.each_key do |enum_name|
20
- define_method("#{enum_name.downcase}?") do
22
+ define_method("#{Inum::Utils::underscore(enum_name)}?") do
21
23
  enum_class::parse(enum_name).eql?(send(variable_name))
22
24
  end
23
25
  end
data/lib/inum/utils.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Inum
2
+ # Utils of Inum.
3
+ class Utils
4
+
5
+ # convert a camel cased word to a underscore word.
6
+ #
7
+ # @param camel_cased_word [String] camel cased word.
8
+ # @return [String, Symbol] underscore word.
9
+ def self.underscore(camel_cased_word)
10
+ word = camel_cased_word.to_s.dup
11
+
12
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
13
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
14
+ word.tr!('-', '_')
15
+
16
+ word.downcase!
17
+
18
+ word
19
+ end
20
+
21
+ end
22
+ end
data/lib/inum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -6,8 +6,7 @@ describe Inum::Base do
6
6
  it 'define_method validate correct' do
7
7
  # correct.
8
8
  expect{
9
- Class.new(Inum::Base) { define_enum :REDBULL, 0 }
10
- Class.new(Inum::Base) { define_enum 'REDBULL', 0 }
9
+ Class.new(Inum::Base) { define_enum :REDBULL, 0 }
11
10
  Class.new(Inum::Base) { define_enum :REDBULL, 0.to_i }
12
11
  Class.new(Inum::Base) {
13
12
  define_enum :REDBULL, 0
@@ -101,14 +100,24 @@ describe Inum::Base do
101
100
  expect( @enum::MONSTER.to_s ).to eq('MONSTER')
102
101
  end
103
102
 
103
+ it 'labels return Array<Symbol>.' do
104
+ expect(@enum::labels.length).to eq(3)
105
+ expect(@enum::labels.instance_of?(Array)).to be_true
106
+ expect(@enum::labels[0].instance_of?(Symbol)).to be_true
107
+ end
108
+
104
109
  it 'length return count of enum.' do
105
110
  expect(@enum::length).to eq(3)
106
111
  end
107
112
 
108
- it 'to_a return Array' do
109
- expect(@enum::to_a.instance_of?(Array)).to be_true
113
+ it 'to_a return Array<Array<Symbol,Integer>>.' do
110
114
  expect(@enum::to_a.length).to eq(3)
111
- expect(@enum::to_a[0][0]).to eq('REDBULL')
115
+ expect(@enum::to_a.instance_of?(Array)).to be_true
116
+ expect(@enum::to_a[0].instance_of?(Array)).to be_true
117
+ expect(@enum::to_a[0][0].instance_of?(Symbol)).to be_true
118
+ expect(@enum::to_a[0][1].integer?).to be_true
119
+
120
+ expect(@enum::to_a[0][0]).to eq(:REDBULL)
112
121
  (0..2).each{|i| expect(@enum::to_a[i][1]).to eq(i) }
113
122
  end
114
123
 
@@ -133,5 +142,11 @@ describe Inum::Base do
133
142
  it 'parse return instance from self instance.' do
134
143
  expect( @enum::parse(@enum::REDBULL) ).to eq( @enum::REDBULL )
135
144
  end
145
+
146
+ it 'values return Array<Integer>.' do
147
+ expect(@enum::values.length).to eq(3)
148
+ expect(@enum::values.instance_of?(Array)).to be_true
149
+ expect(@enum::values[0].integer?).to be_true
150
+ end
136
151
  end
137
152
  end
@@ -0,0 +1,17 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe Inum::Utils do
5
+
6
+ it 'underscore can convert CamelCasedWord to underscore_word.' do
7
+ expect(Inum::Utils::underscore('camelcase')).to eq('camelcase')
8
+ expect(Inum::Utils::underscore('CamelCase')).to eq('camel_case')
9
+ expect(Inum::Utils::underscore('CamelCaseWord')).to eq('camel_case_word')
10
+
11
+ expect(Inum::Utils::underscore('Came1Case')).to eq('came1_case')
12
+ end
13
+
14
+ it 'underscore can convert Symbol to underscore_word.' do
15
+ expect(Inum::Utils::underscore(:CamelCase)).to eq('camel_case')
16
+ end
17
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'inum/base'
4
4
  require 'inum/define_check_method'
5
+ require 'inum/utils'
5
6
 
6
7
  RSpec.configure do |config|
7
8
  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.1.0
4
+ version: 1.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: 2013-11-26 00:00:00.000000000 Z
11
+ date: 2013-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,9 +82,11 @@ files:
82
82
  - lib/inum.rb
83
83
  - lib/inum/base.rb
84
84
  - lib/inum/define_check_method.rb
85
+ - lib/inum/utils.rb
85
86
  - lib/inum/version.rb
86
87
  - spec/inum/base_spec.rb
87
88
  - spec/inum/define_check_method_spec.rb
89
+ - spec/inum/utils_spec.rb
88
90
  - spec/spec_helper.rb
89
91
  homepage: https://github.com/alfa-jpn/inum
90
92
  licenses:
@@ -113,5 +115,6 @@ summary: Inum(enumerated type of Integer) provide a java-Enum-like.
113
115
  test_files:
114
116
  - spec/inum/base_spec.rb
115
117
  - spec/inum/define_check_method_spec.rb
118
+ - spec/inum/utils_spec.rb
116
119
  - spec/spec_helper.rb
117
120
  has_rdoc: