Module: YaEnum
- Defined in:
- lib/ya_enum.rb,
lib/ya_enum/matcher.rb,
lib/ya_enum/version.rb
Defined Under Namespace
Classes: Matcher, MissingMethods
Constant Summary
collapse
- VERSION =
"0.1.3"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#variants ⇒ Object
Returns the value of attribute variants.
6
7
8
|
# File 'lib/ya_enum.rb', line 6
def variants
@variants
end
|
Instance Method Details
#all_variants ⇒ Object
47
48
49
|
# File 'lib/ya_enum.rb', line 47
def all_variants
variants.map(&:last)
end
|
#case(variant, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/ya_enum.rb', line 37
def case(variant, &block)
matcher = Matcher.new(all_variants: variants)
if block.arity.zero?
matcher.instance_eval(&block)
else
yield matcher
end
matcher.match_on(variant)
end
|
#variant(name, associated_values = [], &block) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/ya_enum.rb', line 8
def variant(name, associated_values = [], &block)
if variants.nil?
self.variants = []
end
enum = Class.new do
extend TakesMacro
takes associated_values.map { |value| :"#{value}!" }
def self.===(other)
self == other || super
end
end
if block
if associated_values.any?
enum.class_eval(&block)
enum.include(self)
else
enum.instance_eval(&block)
enum.extend(self)
end
end
const_set(name, enum)
variants << [name.to_s, enum]
ensure_all_methods_defined_for_each_variant!
end
|