enums 1.0.0 → 1.1.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: 0f703a80394cb87387c0efe3311aed2c59220710
4
- data.tar.gz: 39b065497a6e1fb107de0819e62999fc14c14fe0
3
+ metadata.gz: c1fd2c8d5f02ac6908e4a728c5875e177f3b1d1e
4
+ data.tar.gz: cde381d80361a980cc34825a29c897d5f3a604ff
5
5
  SHA512:
6
- metadata.gz: 419c10d70475ab3b6800b6895dcb7eb2422f28c4284a27811c3ec0339922268c953e0a0844bb874a4554c5193f6d5b1b4e328cca9a129e82e694be73f6b69427
7
- data.tar.gz: 3379a2c0395e83430e10b862f0de84cf5f4cc1fc2aebe5cbe5fd28e1e2eaf74c5a98a3b25b285cfdccc49841d69ad00d294e6f8d83c2c55fcc50595f6ab0e00b
6
+ metadata.gz: '09b36ca1dd6f1918839ab56ec2a0884777bb99a57e65aaff09a97e9d962baf3392cbd3907a70356e8226c257086b005ac6133ccdc56f26b8d26699320c6a3f7e'
7
+ data.tar.gz: 273c1e400392057321bae3f78776ffad156a32beffa4ad661ea8135f59cb718f861795212036cb63c480f810d13c2a41ae36e87d11d7789c1ffb0d4f871f9a0d
data/README.md CHANGED
@@ -45,8 +45,13 @@ Why? Why not? Discuss.
45
45
 
46
46
  ``` ruby
47
47
  Enum.new( 'Color', :red, :green, :blue )
48
+ # or
49
+ enum 'Color', :red, :green, :blue
50
+ # or
51
+ enum 'Color', [:red, :green, :blue]
48
52
  ```
49
53
 
54
+
50
55
  (Auto-)builds a class and code like:
51
56
 
52
57
  ``` ruby
@@ -71,14 +76,14 @@ class Color < Enum
71
76
  def self.keys() [:red, :blue, :green]; end
72
77
  def self.members() [RED, BLUE, GREEN]; end
73
78
 
74
- def self.value( index ) @members[index]; end
79
+ def self.value( index ) members[index]; end
75
80
  def self.zero() value(0); end
76
81
 
77
82
  def self.key( key )
78
83
  @hash ||= Hash[ keys.zip( members ) ]
79
84
  @hash[ key ]
80
85
  end
81
- def self.[]( key ) self.key( key ); end
86
+ def self.[]( key ) self.key( key ); end
82
87
 
83
88
  def red?() self == RED; end
84
89
  def blue?() self == BLUE; end
@@ -100,6 +105,8 @@ Color.zero #=> same as Color(0)
100
105
  Color.red #=> Color::RED
101
106
  Color.values #=> [0, 1, 2]
102
107
  Color.keys #=> [:red, :green, :blue]
108
+ Color.size #=> 3
109
+ Color.length #=> same as Color.size
103
110
  Color.members #=> [RED, GREEN, BLUE]
104
111
  # -or-
105
112
  # [<Color @key=:red, @value=0>,
@@ -125,9 +132,16 @@ Let's try another example:
125
132
 
126
133
  ``` ruby
127
134
  Enum.new( 'State', :fundraising, :expired_refund, :successful )
135
+ # or
136
+ enum 'State', :fundraising, :expired_refund, :successful
137
+ # or
138
+ enum 'State', [:fundraising, :expired_refund, :successful]
139
+
128
140
 
129
141
  State.values #=> [0, 1, 2]
130
142
  State.keys #=> [:fundraising, :expired_refund, :successful]
143
+ State.size #=> 3
144
+ State.length #=> same as State.size
131
145
 
132
146
  State.members #=> [FUNDRAISING, EXPIRED_REFUND, SUCCESSFUL]
133
147
  # -or-
@@ -147,6 +161,8 @@ State::FUNDRAISING.key #=> :fundraising
147
161
  state = State.fundraising
148
162
  state.fundraising? #=> true
149
163
  state.value #=> 0
164
+ state.is_a? Enum #=> true
165
+ state.is_a? State #=> true
150
166
 
151
167
  State(0) #=> State::FUNDRAISING
152
168
  State(1) #=> State::EXPIRED_REFUND
@@ -172,7 +188,7 @@ and so on.
172
188
  - [Ruby Sample Contracts for the Universum Blockchain/World Computer Runtime](https://github.com/s6ruby/universum-contracts)
173
189
 
174
190
 
175
- ## More Safe Data Structure (Array, Hash, Struct)
191
+ ## More Safe Data Structures (Array, Hash, Struct)
176
192
 
177
193
  [Safe Data Structures (Array, Hash, Struct)](https://github.com/s6ruby/safestruct) - Say goodbye to null / nil (and maybe) and the Billion-Dollar mistake. Say hello to zero and the Billon-Dollar fix.
178
194
 
@@ -48,6 +48,13 @@ require 'enums/version' # note: let version always go first
48
48
 
49
49
  module Safe
50
50
 
51
+ ## note: use ClassMethods pattern for auto-including class methods
52
+ def self.included( klass )
53
+ klass.extend( ClassMethods )
54
+ end
55
+ module ClassMethods; end
56
+
57
+
51
58
 
52
59
  ## base class for enum
53
60
  class Enum
@@ -91,6 +98,10 @@ class Enum
91
98
  @values
92
99
  end
93
100
 
101
+ def self.size() keys.size; end
102
+ def self.length() size; end ## alias (as is the ruby tradition)
103
+
104
+
94
105
  def self.convert( arg )
95
106
  ## todo/check: support keys too - why? why not?
96
107
  ## e.g. Color(0), Color(1)
@@ -104,6 +115,9 @@ class Enum
104
115
  ## meta-programming "macro" - build class (on the fly)
105
116
  def self.build_class( class_name, *keys )
106
117
 
118
+ ## todo/fix:
119
+ ## check class name MUST start with uppercase letter
120
+
107
121
  ## check if all keys are symbols and follow the ruby id(entifier) naming rules
108
122
  keys.each do |key|
109
123
  if key.is_a?( Symbol ) && key =~ /\A[a-z][a-zA-Z0-9_]*\z/
@@ -143,17 +157,21 @@ RUBY
143
157
  end
144
158
  RUBY
145
159
 
146
- ## note: use Safe (module) and NOT Object for namespacing
147
- ## use include Enum to make all enums and convenience converters global
160
+ ## note: use Object for "namespacing"
161
+ ## make all enums convenience converters (always) global
162
+ ## including uppercase methods (e.g. State(), Color(), etc.) does NOT work otherwise (with other module includes)
148
163
 
149
164
  ## add global convenience converter function
150
165
  ## e.g. State(0) is same as State.convert(0)
151
- Safe.class_eval( <<RUBY )
166
+ ## Color(0) is same as Color.convert(0)
167
+ Object.class_eval( <<RUBY )
152
168
  def #{class_name}( arg )
153
169
  #{class_name}.convert( arg )
154
170
  end
155
171
  RUBY
156
172
 
173
+ ## note: use Safe (module) and NO Object for namespacing
174
+ ## use include Safe to make all enums constants and machinery global
157
175
  Safe.const_set( class_name, klass ) ## returns klass (plus sets global constant class name)
158
176
  end
159
177
 
@@ -162,6 +180,24 @@ RUBY
162
180
  alias_method :new, :build_class # replace original version with create
163
181
  end
164
182
  end # class Enum
183
+
184
+
185
+ module ClassMethods
186
+ def enum( class_name, *args )
187
+ ########################################
188
+ # note: lets you use:
189
+ # enum 'Color', :red, :green, :blue
190
+ # -or-
191
+ # enum 'Color', [:red, :green, :blue]
192
+ if args[0].is_a?( Array )
193
+ keys = args[0]
194
+ else
195
+ keys = args
196
+ end
197
+
198
+ Enum.new( class_name, *keys )
199
+ end
200
+ end # module ClassMethods
165
201
  end # module Safe
166
202
 
167
203
 
@@ -4,7 +4,7 @@
4
4
  class Enums
5
5
 
6
6
  MAJOR = 1
7
- MINOR = 0
7
+ MINOR = 1
8
8
  PATCH = 0
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
@@ -13,76 +13,99 @@ class TestEnum < MiniTest::Test
13
13
  include Safe ## make all enums (and "convenience" converters) global
14
14
 
15
15
 
16
- def test_state
17
- pp Enum.new( 'State', :fundraising, :expired_refund, :successful )
18
-
19
-
20
- puts "Safe.constants:"
21
- pp Safe.constants
22
- puts "Enum.constants:"
23
- pp Enum.constants
24
- puts "State.constants:"
25
- pp State.constants
26
-
27
-
28
- pp State.values
29
- pp State.keys
30
- ## pp State(0)
16
+ ## Enum.new( 'State', :fundraising, :expired_refund, :successful )
17
+ enum 'State', :fundraising, :expired_refund, :successful
31
18
 
32
- pp State.members
33
- pp State.members[0].key
34
- pp State.members[0].value
35
- pp State.members[1].key
36
- pp State.members[1].value
19
+ pp State
20
+ pp State(0)
37
21
 
38
- pp State.fundraising.value
39
- pp State.fundraising.key
40
- pp State::FUNDRAISING.value
41
- pp State::FUNDRAISING.key
22
+ ## Enum.new( 'Color', :red, :green, :blue )
23
+ enum 'Color', :red, :green, :blue
42
24
 
25
+ pp Color
26
+ pp Color(0)
43
27
 
44
- pp State
45
- state = State.fundraising
46
- pp state.fundraising?
47
- pp state
48
- pp state.value
28
+ puts "Safe.constants:"
29
+ pp Safe.constants #=> [:ClassMethods, :Enum, :State, :Color]
30
+ puts "Enum.constants:"
31
+ pp Enum.constants #=> []
49
32
 
50
33
 
51
- pp State(0)
52
- pp State(1)
53
- pp State(2)
54
- pp State(3)
55
-
56
- pp State.zero
57
- pp State(0) == State.zero
58
- pp State(1) == State.zero
59
-
60
- pp State.value(0)
61
- pp State.key(:fundraising)
62
- pp State[:fundraising]
34
+ def test_state
35
+ assert_equal [:FUNDRAISING, :EXPIRED_REFUND, :SUCCESSFUL], State.constants
36
+
37
+ assert_equal [0, 1, 2], State.values
38
+ assert_equal [:fundraising, :expired_refund, :successful], State.keys
39
+
40
+ assert_equal 3, State.size
41
+ assert_equal 3, State.length
42
+
43
+ assert_equal State.fundraising, State::FUNDRAISING
44
+ assert_equal State.expired_refund, State::EXPIRED_REFUND
45
+ assert_equal State.successful, State::SUCCESSFUL
46
+
47
+ pp State.members
48
+ assert_equal :fundraising, State.members[0].key
49
+ assert_equal 0, State.members[0].value
50
+ assert_equal :expired_refund, State.members[1].key
51
+ assert_equal 1, State.members[1].value
52
+
53
+ assert_equal 0, State.fundraising.value
54
+ assert_equal :fundraising, State.fundraising.key
55
+ assert_equal 0, State::FUNDRAISING.value
56
+ assert_equal :fundraising, State::FUNDRAISING.key
57
+
58
+ pp State
59
+ state = State.fundraising
60
+ pp state
61
+ assert_equal true, state.fundraising?
62
+ assert_equal 0, state.value
63
+ assert_equal true, state.is_a?( Enum )
64
+ assert_equal true, state.is_a?( State )
65
+
66
+ assert_equal State.fundraising, State(0)
67
+ assert_equal State.expired_refund, State(1)
68
+ assert_equal State.successful, State(2)
69
+
70
+ pp State.zero
71
+ assert_equal true, State(0) == State.zero
72
+ assert_equal false, State(1) == State.zero
73
+
74
+ assert_equal State.fundraising, State.value(0)
75
+ assert_equal State.fundraising, State.key(:fundraising)
76
+ assert_equal State.fundraising, State[:fundraising]
63
77
  end
64
78
 
79
+
65
80
  def test_color
66
- pp Enum.new( 'Color', :red, :green, :blue )
67
- pp Color.zero
68
- pp Color(0)
69
- pp Color.red
70
- pp Color.values
71
- pp Color.keys
72
- pp Color.constants
73
- pp Color.members
74
- pp Color(1)
75
- pp Color.value(1)
76
- pp Color.key(:red)
77
- pp Color[:red]
78
- color = Color.red
79
- pp color.red?
80
- pp color == Color.red
81
- pp color.blue?
82
- pp color == Color.blue
83
-
84
- pp Color::RED
85
- pp Color.members
81
+ assert_equal [0, 1, 2], Color.values
82
+ assert_equal [:red, :green, :blue], Color.keys
83
+ assert_equal [:RED, :GREEN, :BLUE], Color.constants
84
+
85
+ assert_equal 3, Color.size
86
+ assert_equal 3, Color.length
87
+
88
+ pp Color.zero
89
+ pp Color.red
90
+
91
+ assert_equal Color.red, Color(0)
92
+ assert_equal Color.green, Color(1)
93
+ assert_equal Color.blue, Color(2)
94
+
95
+ pp Color.members
96
+ assert_equal Color.green, Color.value(1)
97
+ assert_equal Color.red, Color.key(:red)
98
+ assert_equal Color.red, Color[:red]
99
+
100
+ color = Color.red
101
+ assert_equal true, color.red?
102
+ assert_equal true, color == Color.red
103
+ assert_equal false, color.blue?
104
+ assert_equal false, color == Color.blue
105
+
106
+ assert_equal Color.red, Color::RED
107
+ assert_equal Color.green, Color::GREEN
108
+ assert_equal Color.blue, Color::BLUE
86
109
  end
87
110
 
88
111
  end # class TestEnum
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enums
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-28 00:00:00.000000000 Z
11
+ date: 2019-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc