rubenum 1.0.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
+ SHA256:
3
+ metadata.gz: f7cf05cb7fbb00f4b92af49a1d9f217b8214221288c895c9ab3c5f7bcc616386
4
+ data.tar.gz: a239697d833132e9d93830527610c3b02fe83750f00e8e7d4dd5b94e65979248
5
+ SHA512:
6
+ metadata.gz: 8ea675efc270202e3a61f6cb7ee551ea2ad30c04769c150eaecbd1993657385b0624d254b9357c53623716feeb04edacd6703d5f56a12ce59c3dd7cce28794d0
7
+ data.tar.gz: 1c6b87567a2a1cccc0b08503663dd74223e3ce146220fa2e4973b8f88914e0d37d88a05ad2bac8746c715ce58d6dcf55a958a2b2553e08ee5eef833bd4940cf0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Jérémy Blain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Rubenum
2
+
3
+ Enum implementation for Ruby.
4
+
5
+ Create an enum from an array of strings with `Rubenum.new`.
6
+ Only valid Ruby identifiers are accepted.
7
+ Every element will be capitalized in order to make them Ruby constants.
8
+
9
+ ### Arguments
10
+ - elements (array of string) : array containing the elements of the enum
11
+ - upcase (boolean optional default:false) : true to upcase elements instead of capitalize them
12
+ - binary_values (boolean optional default:false) : true to use binary values (1 2 4 8 16 ...) instead of incremental values (1 2 3 4 5 ...)
13
+
14
+ ### Example
15
+ e = Rubenum.new(%w[red orange green])
16
+ puts e::Orange # => 2
17
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
9
+
data/lib/rubenum.rb ADDED
@@ -0,0 +1,45 @@
1
+ # Enum implementation for Ruby.
2
+ module Rubenum
3
+ # Create an enum from an array of strings.
4
+ # Only valid Ruby identifiers are accepted.
5
+ # Every element will be capitalized in order to make them Ruby constants.
6
+ #
7
+ # Arguments :
8
+ # - elements (array of string) : array containing the elements of the enum
9
+ # - upcase (boolean optional default:false) : true to upcase elements instead of capitalize them
10
+ # - binary_values (boolean optional default:false) : true to use binary values(1 2 4 8 16 ...) instead of incremental values (1 2 3 4 5 ...)
11
+ #
12
+ # Example :
13
+ # e = Rubenum.new(%w[red orange green])
14
+ # puts e::Orange # => 2
15
+ def self.new(elements, upcase: false, binary_values: false)
16
+ Module.new do |mod|
17
+ raise ArgumentError, "<elements> must be an array" unless elements.is_a?(Array)
18
+ raise ArgumentError, "<elements> can't be empty" if elements.empty?
19
+
20
+ # Format the elements and remove duplicates
21
+ cleaned_elements = elements.map do |e|
22
+ raise ArgumentError, "<elements> must only contain strings" unless e.is_a?(String)
23
+ upcase ? e.upcase : e.capitalize
24
+ end
25
+ cleaned_elements.uniq!
26
+
27
+ cleaned_elements.each_with_index do |e, i|
28
+ # Calculate the value of the element
29
+ value = 1
30
+ if binary_values
31
+ value <<= i
32
+ else
33
+ value += i
34
+ end
35
+
36
+ # Add the element to the enum
37
+ mod.const_set(e, value)
38
+ end
39
+
40
+ # Freeze the enum so it can't be modifed
41
+ mod.freeze
42
+ end
43
+ end
44
+ end
45
+
data/rubenum.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rubenum'
3
+ s.version = '1.0.0'
4
+ s.summary = "Enum implementation for Ruby."
5
+ s.author = 'Jérémy Blain'
6
+ s.files = Dir['lib/**/*'] + Dir['test/**/*']
7
+ s.files += ['LICENSE', 'README.md', 'Rakefile', 'rubenum.gemspec']
8
+
9
+ s.license = 'MIT'
10
+ s.homepage = 'http://github.com/blainjeremy/rubenum'
11
+ s.email = 'blain.jeremy@gmail.com'
12
+
13
+ s.date = '2019-05-06'
14
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
15
+ end
16
+
@@ -0,0 +1,90 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'rubenum'
4
+
5
+ class TestRubenum < Minitest::Test
6
+ # Valid elements
7
+
8
+ def test_with_valid_elements
9
+ e = Rubenum.new(%w[First sec_ond third_ fou_rth4])
10
+ assert_equal e.constants.sort, [:First, :Sec_ond, :Third_, :Fou_rth4].sort
11
+ end
12
+
13
+ def test_when_elements_isnt_an_array
14
+ assert_raises ArgumentError do
15
+ Rubenum.new(45)
16
+ end
17
+ end
18
+
19
+ def test_with_wrong_type_of_elements
20
+ assert_raises ArgumentError do
21
+ Rubenum.new([:First, "sec_ond", ["third"], 4])
22
+ end
23
+ end
24
+
25
+ def test_with_empty_elements
26
+ assert_raises ArgumentError do
27
+ Rubenum.new([])
28
+ end
29
+ end
30
+
31
+ def test_duplicate_elements_are_made_unique
32
+ e = Rubenum.new(%w[First first sec_ond third_ fou_rth4])
33
+ assert_equal e.constants.sort, [:First, :Sec_ond, :Third_, :Fou_rth4].sort
34
+ end
35
+
36
+ def test_with_a_single_char
37
+ e = Rubenum.new(%w[First x sec_ond third_ fou_rth4])
38
+ assert_equal e.constants.sort, [:First, :X, :Sec_ond, :Third_, :Fou_rth4].sort
39
+ end
40
+
41
+ def test_not_working_with_a_space_char
42
+ assert_raises NameError do
43
+ Rubenum.new(["First", "sec ond", "third_", "fou_rth4"])
44
+ end
45
+ end
46
+
47
+ def test_not_working_with_a_leading_underscore
48
+ assert_raises NameError do
49
+ Rubenum.new(%w[First _sec_ond third_ fou_rth4])
50
+ end
51
+ end
52
+
53
+ def test_not_working_with_a_special_char
54
+ assert_raises NameError do
55
+ Rubenum.new(%w[First sec!ond third_ fou_rth4])
56
+ end
57
+ end
58
+
59
+ def test_not_working_with_a_leading_digit
60
+ assert_raises NameError do
61
+ Rubenum.new(%w[First 2sec_ond third_ fou_rth4])
62
+ end
63
+ end
64
+
65
+ # Upcase option
66
+
67
+ def test_upcase
68
+ e = Rubenum.new(%w[First sec_ond third_ fou_rth4], upcase: true)
69
+ assert_equal e.constants.sort, [:FIRST, :SEC_OND, :THIRD_, :FOU_RTH4].sort
70
+ end
71
+
72
+ # Binary values option
73
+
74
+ def test_with_decimal
75
+ e = Rubenum.new(%w[First sec_ond third_ fou_rth4])
76
+ assert_equal 1, e::First
77
+ assert_equal 2, e::Sec_ond
78
+ assert_equal 3, e::Third_
79
+ assert_equal 4, e::Fou_rth4
80
+ end
81
+
82
+ def test_with_binary
83
+ e = Rubenum.new(%w[First sec_ond third_ fou_rth4], binary_values: true)
84
+ assert_equal 1, e::First
85
+ assert_equal 2, e::Sec_ond
86
+ assert_equal 4, e::Third_
87
+ assert_equal 8, e::Fou_rth4
88
+ end
89
+ end
90
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubenum
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jérémy Blain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: blain.jeremy@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ - LICENSE
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - lib/rubenum.rb
25
+ - rubenum.gemspec
26
+ - test/test_rubenum.rb
27
+ homepage: http://github.com/blainjeremy/rubenum
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Enum implementation for Ruby.
50
+ test_files: []