rasti-types 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
  SHA256:
3
- metadata.gz: 2a37c435c609128695ba34ff046716f3692dce4b5a0d785365f39c44a887921b
4
- data.tar.gz: a3a9c0d4bfd480922fc412c992c3849afa577a8b53495d4c91e6b71dfd334364
3
+ metadata.gz: ee88a26c1db0346a2be756ce54e9cb5f61cdb1cb251745c790d59427dcaeb2f2
4
+ data.tar.gz: f8c66a79f42446f8d64cc273a2457483b6722b0332c4dc68ccd7725f1cb8df37
5
5
  SHA512:
6
- metadata.gz: 8bf152f0c673cf017632cb967f9d48ec29a89a689198e3f4df5b87dcb05af6d1aa1ae286658b9420e744b07028e543d6acf74b454cc2c58762f8de9127d40078
7
- data.tar.gz: a75a4e0c14e8c4a6b42c36f04e3ff63b00d7fdc5952080bbb23ec448fd8639e915e44c7c86cc134ddf75d94bb31bdfc0467c9953561dd1954950086ef525c4a8
6
+ metadata.gz: 9828d21e82cfbbc25c772d0988e1dbbb2a783227cd4404ff3becf2e2cd6305dc3ff4d50977b5167bf7f41450c19d885a134e9526edcb55f3bdf9aa11fa0437f9
7
+ data.tar.gz: 193f841ad283e619c53608a0de291f0352f7e5d73f769dbf9e9607e378b3e1245bb158b6d2e41fb77c1326f682f7bbe758ce766ca31ede1eb3d21103f9c8c3cc
data/lib/rasti/types.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'multi_require'
2
+ require 'inflecto'
3
+ require 'rasti-enum'
2
4
 
3
5
  module Rasti
4
6
  module Types
@@ -34,10 +34,7 @@ module Rasti
34
34
 
35
35
  def define_getters
36
36
  values.each do |value|
37
- getter_name = value.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
38
- define_singleton_method getter_name do
39
- value
40
- end
37
+ define_singleton_method(Inflecto.underscore(value)) { value }
41
38
  end
42
39
  end
43
40
 
@@ -0,0 +1,45 @@
1
+ module Rasti
2
+ module Types
3
+ class TypedEnum
4
+
5
+ include Castable
6
+
7
+ def self.[](enum)
8
+ new enum
9
+ end
10
+
11
+ def initialize(enum)
12
+ @enum = enum
13
+ define_getters
14
+ end
15
+
16
+ def values
17
+ enum.values
18
+ end
19
+
20
+ def to_s
21
+ "#{self.class}[#{enum}]"
22
+ end
23
+ alias_method :inspect, :to_s
24
+
25
+ private
26
+
27
+ attr_reader :enum
28
+
29
+ def valid?(value)
30
+ enum.include? value
31
+ end
32
+
33
+ def transform(value)
34
+ enum[value]
35
+ end
36
+
37
+ def define_getters
38
+ values.each do |value|
39
+ define_singleton_method(Inflecto.underscore(value)) { value }
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module Types
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
data/rasti-types.gemspec CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_runtime_dependency 'multi_require', '~> 1.0'
22
+ spec.add_runtime_dependency 'inflecto', '~> 0.0'
23
+ spec.add_runtime_dependency 'rasti-enum', '~> 1.0'
22
24
 
23
25
  spec.add_development_dependency 'rake', '~> 12.0'
24
26
  spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
data/spec/enum_spec.rb CHANGED
@@ -17,12 +17,13 @@ describe Rasti::Types::Enum do
17
17
  end
18
18
  end
19
19
 
20
- it 'Constants' do
20
+ it 'Values' do
21
21
  enum = Rasti::Types::Enum[:first_value, 'SecondValue', 'THIRD_VALUE']
22
22
 
23
23
  enum.first_value.must_equal 'first_value'
24
24
  enum.second_value.must_equal 'SecondValue'
25
25
  enum.third_value.must_equal 'THIRD_VALUE'
26
+ enum.values.must_equal ['first_value', 'SecondValue', 'THIRD_VALUE']
26
27
  end
27
28
 
28
29
  end
@@ -34,4 +34,20 @@ class Point
34
34
  raise Rasti::Types::CompoundError, errors if errors.any?
35
35
  end
36
36
 
37
+ end
38
+
39
+
40
+ module Colors
41
+
42
+ extend Rasti::Enum
43
+
44
+ class Red < Rasti::Enum::Value
45
+ end
46
+
47
+ class Green < Rasti::Enum::Value
48
+ end
49
+
50
+ class Blue < Rasti::Enum::Value
51
+ end
52
+
37
53
  end
@@ -0,0 +1,27 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::TypedEnum do
4
+
5
+ let(:enum) { Rasti::Types::TypedEnum[Colors] }
6
+
7
+ ['BLUE', 'GREEN', 'RED'].each do |value|
8
+ it "#{value.inspect} -> #{value}" do
9
+ enum.cast(value).must_equal Colors[value]
10
+ end
11
+ end
12
+
13
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new, nil].each do |value|
14
+ it "#{value.inspect} -> CastError" do
15
+ error = proc { enum.cast(value) }.must_raise Rasti::Types::CastError
16
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::TypedEnum[Colors]"
17
+ end
18
+ end
19
+
20
+ it 'Values' do
21
+ enum.blue.must_equal Colors::Blue.new
22
+ enum.green.must_equal Colors::Green.new
23
+ enum.red.must_equal Colors::Red.new
24
+ enum.values.must_equal [enum.blue, enum.green, enum.red]
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-types
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
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-11 00:00:00.000000000 Z
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: inflecto
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rasti-enum
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +188,7 @@ files:
160
188
  - lib/rasti/types/string.rb
161
189
  - lib/rasti/types/symbol.rb
162
190
  - lib/rasti/types/time.rb
191
+ - lib/rasti/types/typed_enum.rb
163
192
  - lib/rasti/types/uuid.rb
164
193
  - lib/rasti/types/version.rb
165
194
  - rasti-types.gemspec
@@ -178,6 +207,7 @@ files:
178
207
  - spec/string_spec.rb
179
208
  - spec/symbol_spec.rb
180
209
  - spec/time_spec.rb
210
+ - spec/typed_enum_spec.rb
181
211
  - spec/uuid_spec.rb
182
212
  homepage: https://github.com/gabynaiman/rasti-types
183
213
  licenses:
@@ -218,4 +248,5 @@ test_files:
218
248
  - spec/string_spec.rb
219
249
  - spec/symbol_spec.rb
220
250
  - spec/time_spec.rb
251
+ - spec/typed_enum_spec.rb
221
252
  - spec/uuid_spec.rb