Skip to content

Commit 19f1822

Browse files
committed
+ from
1 parent ff139b6 commit 19f1822

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
is-enum (0.8.2)
4+
is-enum (0.8.4)
55

66
GEM
77
remote: https://rubygems.org/
@@ -46,7 +46,7 @@ DEPENDENCIES
4646
CHECKSUMS
4747
diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
4848
docile (1.4.1) sha256=96159be799bfa73cdb721b840e9802126e4e03dfc26863db73647204c727f21e
49-
is-enum (0.8.2)
49+
is-enum (0.8.4)
5050
rake (13.3.1) sha256=8c9e89d09f66a26a01264e7e3480ec0607f0c497a861ef16063604b1b08eb19c
5151
redcarpet (3.6.1) sha256=d444910e6aa55480c6bcdc0cdb057626e8a32c054c29e793fa642ba2f155f445
5252
rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587

is-enum.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification::new do |s|
22
s.name = "is-enum"
3-
s.version = "0.8.2"
3+
s.version = "0.8.4"
44
s.summary = "Enum types for Ruby"
55
s.description = "Enum types for Ruby."
66
s.authors = ["Ivan Shikhalev"]

lib/is-enum.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'set'
4+
35
module IS; end
46

57
class IS::Enum
@@ -46,6 +48,23 @@ def of name
4648
return val
4749
end
4850

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+
4968
def each
5069
return to_enum(__method__) unless block_given?
5170
@values.values.sort_by { |v| v.order_no }.each { |v| yield v }

0 commit comments

Comments
 (0)