ree_lib 1.0.73 → 1.0.74

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: 8bc7c01750d09b6274eabf44aa42ab5df1638372eed12a81bf2f79d09caf2920
4
- data.tar.gz: 8dd947819db285b9ead46579f4eacb3b482a27fa525f48e0f997f21ac230744e
3
+ metadata.gz: db0e9f5e3c64d69ff4f6d4d4550ab6563bfe0bc9529333e21a1251503ad562b2
4
+ data.tar.gz: 02abcd3afb9f8155666b98abdb5a9f833b3affca8038c19b2322ab60be82e2db
5
5
  SHA512:
6
- metadata.gz: 498b93f53f71563223d74404aa958ea60618c3513c21a9344ae6c18bce4fd6664113eb47aae7f4626577c632417f355a09252411ebe244921c1443f5846c8e3c
7
- data.tar.gz: d1c3ac12551051f7f9b5f71e785e4cbe22bc5854863b8e406c6b3f950ed486043c9f9c2fffc88ce2c95df471aab47f3c3401af2b6c353bffb2c22bd24d867619
6
+ metadata.gz: 07f42d6c784721984a1729cd38412c946fd8924982d98eff0158a91c4ce9f5c80ef94561e0ea9801a0e1df3baa177ef6bf7ae0db5c177c1f2960f118a339d797
7
+ data.tar.gz: 25c0ae598cdbfee90f4ea991705fd73328188f64f33bd2895635bff5d98dc527c2a209b40fa286208d327f0f50cd6e03e36b192b3b8ea2076166d685b9c227ac
@@ -13,10 +13,16 @@ module ReeEnum::Enumerable
13
13
  include ReeEnum::Contractable
14
14
 
15
15
  RESTRICTED_METHODS = [
16
- :setup_enum, :get_values, :get_enum_name,
17
- :val, :self, :class, :alias
16
+ :setup_enum, :get_values, :get_enum_name, :val,
17
+ :__ENCODING__, :__LINE__, :__FILE__, :BEGIN, :END,
18
+ :alias, :and, :begin, :break, :case, :class, :def, :defined?,
19
+ :do, :else, :elsif, :end, :ensure, :false, :for, :if, :in,
20
+ :module, :next, :nil, :not, :or, :redo, :rescue, :retry, :return,
21
+ :self, :super, :then, :true, :undef, :unless, :until, :when, :while, :yield
18
22
  ].freeze
19
23
 
24
+ ALLOWED_VALUE_TO_METHOD_REGEXP = /^[a-z_]\w*[?!]?$/
25
+
20
26
  def setup_enum(enum_name)
21
27
  @values ||= ReeEnum::Values.new(self, enum_name)
22
28
  end
@@ -29,25 +35,31 @@ module ReeEnum::Enumerable
29
35
  @values&.enum_name
30
36
  end
31
37
 
32
- def val(value, mapped_value = nil, method: value.to_s.to_sym)
38
+ def val(value, mapped_value = nil, method: nil)
33
39
  value = value.to_s if value.is_a?(Symbol)
34
40
  mapped_value ||= value
35
41
 
42
+ if method.nil? && value.is_a?(String) && value.match?(ALLOWED_VALUE_TO_METHOD_REGEXP)
43
+ method = value.to_sym
44
+ end
45
+
36
46
  if RESTRICTED_METHODS.include?(method)
37
47
  raise ArgumentError.new("#{method.inspect} is not allowed as enum method")
38
48
  end
39
49
 
40
50
  enum_value = @values.add(value, mapped_value, method)
41
51
 
42
- class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
43
- def #{method}
44
- get_values.by_value(#{value.inspect}.freeze)
45
- end
52
+ if !method.nil?
53
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
54
+ def #{method}
55
+ get_values.by_value(#{value.inspect}.freeze)
56
+ end
46
57
 
47
- def self.#{method}
48
- get_values.by_value(#{value.inspect}.freeze)
49
- end
50
- RUBY
58
+ def self.#{method}
59
+ get_values.by_value(#{value.inspect}.freeze)
60
+ end
61
+ RUBY
62
+ end
51
63
 
52
64
  enum_value
53
65
  end
@@ -3,7 +3,7 @@
3
3
  class ReeEnum::Value
4
4
  attr_reader :enum_class, :enum_name, :value, :method, :mapped_value
5
5
 
6
- contract(Class, Symbol, Or[String, Integer], Or[Integer, String], Symbol => Any)
6
+ contract(Class, Symbol, Or[String, Integer], Or[Integer, String], Nilor[Symbol] => Any)
7
7
  def initialize(enum_class, enum_name, value, mapped_value, method)
8
8
  @enum_class = enum_class
9
9
  @enum_name = enum_name
@@ -51,7 +51,7 @@ class ReeEnum::Values
51
51
  @collection.map(&:inspect).inspect
52
52
  end
53
53
 
54
- contract(Or[String, Integer], Or[Integer, String], Symbol => ReeEnum::Value)
54
+ contract(Or[String, Integer], Or[Integer, String], Nilor[Symbol] => ReeEnum::Value)
55
55
  def add(value, mapped_value, method)
56
56
  if @value_type.nil?
57
57
  @value_type = value.class
@@ -59,7 +59,7 @@ class ReeEnum::Values
59
59
  raise ArgumentError, "#{@klass}: value types should be the same for all enum values"
60
60
  end
61
61
 
62
- if @collection.any? { _1.method == method }
62
+ if !method.nil? && @collection.any? { _1.method == method }
63
63
  raise ArgumentError, "#{@klass}: method #{method.inspect} was already added"
64
64
  end
65
65
 
@@ -57,13 +57,24 @@ RSpec.describe ReeEnum::DSL do
57
57
  val :yourself
58
58
  end
59
59
 
60
+ class ContentTypes
61
+ include ReeEnum::DSL
62
+
63
+ enum :content_types
64
+
65
+ val "video/mp4"
66
+ val "image/png"
67
+
68
+ register_as_mapper_type
69
+ end
70
+
60
71
  class TestMapper
61
72
  include ReeMapper::DSL
62
73
 
63
74
  mapper :test_mapper do
75
+ link :numbers
64
76
  link :states
65
77
  link :types
66
- link :numbers
67
78
  end
68
79
 
69
80
  class Dto
@@ -212,5 +223,7 @@ RSpec.describe ReeEnum::DSL do
212
223
  expect(dto.number).to be_a(ReeEnum::Value)
213
224
 
214
225
  expect(TestReeEnum::Reflexives.myself).to eq(:self)
226
+
227
+ expect(TestReeEnum::ContentTypes.method_defined?(:"video/mp4")).to be_falsey
215
228
  }
216
229
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReeLib
4
- VERSION = "1.0.73"
4
+ VERSION = "1.0.74"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ree_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.73
4
+ version: 1.0.74
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruslan Gatiyatov