is-enum 0.8.0 → 0.8.4

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/is-enum.rb +26 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fc3fa2228764cfa4cfce1682be2275fb28be09dc993630299ed18dafc7d0266
4
- data.tar.gz: a4c556e719a95db390b2cc5b66e2ad07fdbd78378e74f794430a02d432a0ad05
3
+ metadata.gz: 7815ad636e70cbd191cb03d9f6977566eee7facc449e1f0e080eab20ffb68769
4
+ data.tar.gz: 53a6d579aba8d3cb03f248ce15ea050848da0a783e88bc89e671094ac711dcdd
5
5
  SHA512:
6
- metadata.gz: bd7b0a13da12579621bf919b545265dc781c359ca22f74304541bbbb8e3f6988e2a001c503a9b6dfcf7a4efd8a9c7f7eb9935c4e93b2fe80e5eed126adb999b3
7
- data.tar.gz: a41c53e5a99900c17c0f6f6b831b17692dafdf6b828115ba779243b288a49718b0a6fe16a06a971ee3a5df50eea7a1b5f1006e0ba05bfec7fef2701140677044
6
+ metadata.gz: 8d57ce0b88d2c56f8114619891e807f57eeb9d7d27ef76f5fba1e8e4c49a0ea400fd0982bf7afe49023b088eff13d0aaa6e384464d6ac45d4f6b02641a363e37
7
+ data.tar.gz: 8767e34a653844422d5ec68fc5b347bf51a78fd8aadb1d1af07bb681a70b247bce6fbd86b2d4621f0265b31d13e9cdb7838d22a0de71cb16d66d158749efc7c3
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # is-enum
2
2
 
3
3
  [![GitHub License](https://img.shields.io/github/license/inat-get/is-enum)](LICENSE)
4
- [![Gem Version](https://badge.fury.io/rb/is-enum.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/is-enum)
4
+ [![Gem Version](https://badge.fury.io/rb/is-enum.svg?icon=si%3Arubygems&d=1)](https://badge.fury.io/rb/is-enum)
5
5
  [![Ruby](https://github.com/inat-get/is-enum/actions/workflows/ruby.yml/badge.svg)](https://github.com/inat-get/is-enum/actions/workflows/ruby.yml)
6
6
  ![Coverage](coverage-badge.svg)
7
7
 
data/lib/is-enum.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  module IS; end
4
6
 
5
7
  class IS::Enum
@@ -39,6 +41,30 @@ class IS::Enum
39
41
  end
40
42
  end
41
43
 
44
+ def of name
45
+ raise ArgumentError, "Invalid name of #{ self }: #{ name.inspect }" unless name.is_a?(Symbol)
46
+ val = @values[name] || @aliases[name]
47
+ raise ArgumentError, "Invalid name of #{ self }: #{ name }" unless val
48
+ return val
49
+ end
50
+
51
+ def from value
52
+ case value
53
+ when nil
54
+ nil
55
+ when self
56
+ value
57
+ when Range
58
+ Range::new from(value.begin), from(value.end), value.exclude_end?
59
+ when Set
60
+ Set[*value.map { |v| from(v) }]
61
+ when Enumerable
62
+ value.map { |v| from(v) }
63
+ else
64
+ raise ArgumentError, "Invalid value of #{ self }: #{ value.inspect }", caller_locations
65
+ end
66
+ end
67
+
42
68
  def each
43
69
  return to_enum(__method__) unless block_given?
44
70
  @values.values.sort_by { |v| v.order_no }.each { |v| yield v }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Shikhalev