easy_enum 0.1.2

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: a277b4952bac2f58dd791e094b9f57952df468c3e9e1bd79aabb86065d740f15
4
+ data.tar.gz: eeb710a77ecf87a36bf93fdea34ed79f27fd4dc96312fcc06657d3c8331bf050
5
+ SHA512:
6
+ metadata.gz: 4c67b817be49fe3ace46c30d646a85afcb33e5141ea277c749d0e34cb50f8dd74f41d1872eb49d68857d1c669e55626b1207bd683643857ed18117172e053fa2
7
+ data.tar.gz: f236d45007f52cff6177f10b03f4269fdd8a98c13cef6d158f04722486e1ed8a5bf05d71003e448d51c1756b25aba65c2d82fb4d699788ca0f44fa4a6136325a
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Marco Roth
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # EasyEnum
2
+
3
+ Turn any Ruby class in an easy to use enum.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'easy_enum'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install easy_enum
20
+
21
+ ## Basic Usage
22
+
23
+ ```ruby
24
+ include EasyEnum
25
+
26
+ class Color < EasyEnum
27
+ easy_enum(green: 0, red: 1, blue: 2)
28
+ end
29
+
30
+ # or
31
+
32
+ class Color < EasyEnum
33
+ self.easy_enum = {
34
+ green: 0,
35
+ red: 1,
36
+ blue: 2
37
+ }
38
+ end
39
+
40
+ ```
41
+
42
+ ### Getting an Instance
43
+
44
+ ```ruby
45
+ Color.green
46
+ Color.red
47
+ Color.blue
48
+
49
+ Color.new(0)
50
+ Color.new(1)
51
+ Color.new(2)
52
+
53
+ Color.key(:green)
54
+ Color.key(:red)
55
+ Color.key(:blue)
56
+
57
+ Color.value(0)
58
+ Color.value(1)
59
+ Color.value(2)
60
+
61
+ ```
62
+
63
+ ### Getting Values
64
+
65
+ ```ruby
66
+ green = Color.green
67
+ green.value
68
+ # => 0
69
+
70
+ red = Color.red
71
+ red.value
72
+ # => 1
73
+
74
+ blue = Color.blue
75
+ blue.value
76
+ # => 2
77
+
78
+ ```
79
+
80
+ ### Check the value of your instance
81
+
82
+ ```ruby
83
+ color = Color.green
84
+
85
+ color.green? # => true
86
+ color.red? # => false
87
+ color.blue? # => false
88
+
89
+ ```
90
+
91
+ ### Exploring the Enum
92
+
93
+ ```ruby
94
+ Color.members
95
+ # => {:green=>0, :red=>1, :blue=>2}
96
+
97
+ Color.keys
98
+ # => [:green, :red, :blue]
99
+
100
+ Color.values
101
+ # => [0, 1, 2]
102
+
103
+ Color.key?(:green)
104
+ # => true
105
+
106
+ Color.value?(0)
107
+ # => true
108
+
109
+ ```
110
+
111
+
112
+ ### Comparison
113
+
114
+ ```ruby
115
+ green = Color.green
116
+
117
+ green == :green # => true
118
+ green == 0 # => true
119
+ green == Color.green # => true
120
+ green == Color.key(:green) # => true
121
+ green == Color.value(0) # => true
122
+
123
+ green == Color.red # => false
124
+
125
+ ```
126
+
127
+
128
+ ### Redefining the value of your instance
129
+
130
+ ```ruby
131
+ color = Color.green
132
+ color.value # => 0
133
+ color.key # => :green
134
+
135
+ color.value = 1
136
+ color.value # => 1
137
+ color.key # => :red
138
+
139
+ color.key = :blue
140
+ color.value # => 2
141
+ color.key # => :blue
142
+
143
+ color.key = :yellow
144
+ # => raises EasyEnum::KeyNotInEnum
145
+
146
+ color.value = 3
147
+ # => raises EasyEnum::ValueNotInEnum
148
+
149
+ ```
150
+
151
+
152
+ ## Development
153
+
154
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
155
+
156
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
157
+
158
+ ## Contributing
159
+
160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/easy_enum.
161
+
162
+ ## License
163
+
164
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ module EasyEnum
2
+ class Color < EasyEnum
3
+ easy_enum(green: 0, red: 1, blue: 2)
4
+ end
5
+ end
@@ -0,0 +1,102 @@
1
+ module EasyEnum
2
+ class EasyEnum
3
+ attr_reader :value
4
+
5
+ class << self
6
+ attr_writer :easy_enum
7
+ end
8
+
9
+ def initialize(value)
10
+ self.value = value
11
+ end
12
+
13
+ def self.easy_enum(easy_enum = {})
14
+ @easy_enum ||= easy_enum
15
+ end
16
+
17
+ def value=(value)
18
+ if self.class.values.include?(value)
19
+ @value = value
20
+ return
21
+ end
22
+ raise ValueNotInEnum
23
+ end
24
+
25
+ def key=(key)
26
+ if self.class.keys.include?(key)
27
+ @value = self.class.members[key]
28
+ return
29
+ end
30
+ raise KeyNotInEnum
31
+ end
32
+
33
+ def key
34
+ self.class.easy_enum.each do |key, val|
35
+ return key if value == val
36
+ end
37
+
38
+ nil
39
+ end
40
+
41
+ def self.members
42
+ easy_enum
43
+ end
44
+
45
+ def self.keys
46
+ easy_enum.keys
47
+ end
48
+
49
+ def self.values
50
+ easy_enum.values
51
+ end
52
+
53
+ def self.key(key)
54
+ value = members[key]
55
+ return new(value) if value
56
+
57
+ nil
58
+ end
59
+
60
+ def self.value(value)
61
+ return new(value) if values.include? value
62
+
63
+ nil
64
+ end
65
+
66
+ def self.key?(key)
67
+ keys.include? key
68
+ end
69
+
70
+ def self.value?(value)
71
+ values.include? value
72
+ end
73
+
74
+ def self.size
75
+ easy_enum.count
76
+ end
77
+
78
+ def ==(other)
79
+ if other.is_a? self.class
80
+ other.key == key
81
+ elsif other.is_a? Symbol
82
+ other == key
83
+ else
84
+ other == value
85
+ end
86
+ end
87
+
88
+ def method_missing(method, *args) # rubocop:disable Style/MissingRespondToMissing
89
+ self.class.easy_enum.each do |key, val|
90
+ return val == value if method == "#{key}?".to_sym
91
+ end
92
+
93
+ super
94
+ end
95
+
96
+ def self.method_missing(method, *args) # rubocop:disable Style/MissingRespondToMissing
97
+ return key(method) if keys.include?(method)
98
+
99
+ super
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,9 @@
1
+ module EasyEnum
2
+ class Unit < EasyEnum
3
+ self.easy_enum = {
4
+ meter: 0,
5
+ yard: 1,
6
+ feet: 2
7
+ }
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EasyEnum
2
+ VERSION = '0.1.2'.freeze
3
+ end
data/lib/easy_enum.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'easy_enum/version'
2
+ require 'easy_enum/easy_enum'
3
+
4
+ module EasyEnum
5
+ class ValueNotInEnum < StandardError; end
6
+ class KeyNotInEnum < StandardError; end
7
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Marco Roth
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.68.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.68.0
69
+ description: Turn any Ruby class in an easy to use enum
70
+ email:
71
+ - marco.roth@intergga.ch
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ - lib/easy_enum.rb
79
+ - lib/easy_enum/color.rb
80
+ - lib/easy_enum/easy_enum.rb
81
+ - lib/easy_enum/unit.rb
82
+ - lib/easy_enum/version.rb
83
+ homepage: https://github.com/marcoroth/easy_enum
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Turn any Ruby class in an easy to use enum
106
+ test_files: []