case_struct 0.1
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.
- checksums.yaml +7 -0
- data/lib/case_struct.rb +88 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b69e5643ccd14a4c386a23765784eacabe3bc5056f39708ff0df53c2dd535b8c
|
4
|
+
data.tar.gz: d24a3c17fdabd336521289377145565dca0cb8b5b6e6c452bfe4fbbb68a220e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 740ad9a0915007adc90fc0b2f4938f0c79d70cfcfd9b0d99b4d8831caf576cf8bcf06655d21d24210aa4d4363be803c1bdc23285c9ca99d8a9b09945392ec449
|
7
|
+
data.tar.gz: 3364d222150643c93ff50bb1ee55d33ca18bce65fc8dd31f68cf54558d7d9b4a26511749439b17c181f3aa7c1f5dca0983d1076f19b73b4d925ab568307a26dc
|
data/lib/case_struct.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# CaseStruct
|
2
|
+
#
|
3
|
+
# Enable using Hash like structures
|
4
|
+
# as an alternative to case/when expressions.
|
5
|
+
#
|
6
|
+
# This gives the posibility to construct such expressions
|
7
|
+
# dynamically. Kind of a Rule Engine.
|
8
|
+
#
|
9
|
+
# It is intended to be used by extending the structure
|
10
|
+
# with this module.
|
11
|
+
#
|
12
|
+
# ```ruby
|
13
|
+
# cases = {
|
14
|
+
# (1..5) => :low,
|
15
|
+
# (6..10) => :moderate,
|
16
|
+
# (11..) => :high,
|
17
|
+
# }.extend(CaseStruct)
|
18
|
+
#
|
19
|
+
# cases[3] # => :low
|
20
|
+
# cases[8] # => :moderate
|
21
|
+
# cases[100] # => :high
|
22
|
+
# ```
|
23
|
+
#
|
24
|
+
# If used as a proc, it will still behave as the case/when expression:
|
25
|
+
#
|
26
|
+
# ```ruby
|
27
|
+
# [3, 8, 100].map(&cases)
|
28
|
+
# # => [:low, :moderate, :high]
|
29
|
+
# ```
|
30
|
+
#
|
31
|
+
# Defatults to `nil` unless the extended structure defined or a
|
32
|
+
# `default_proc` or a `default` value.
|
33
|
+
#
|
34
|
+
# This supports directly the behaviour of Hashes.
|
35
|
+
#
|
36
|
+
# ```ruby
|
37
|
+
# cases = {
|
38
|
+
# (1..5) => :low,
|
39
|
+
# (6..10) => :moderate,
|
40
|
+
# (11..) => :high,
|
41
|
+
# }.tap { _1.default = :unknown }
|
42
|
+
# .extend(CaseStructure)
|
43
|
+
#
|
44
|
+
# [-1, 3, 8, 100].map(&cases)
|
45
|
+
# # => [:unkown, :low, :moderate, :high]
|
46
|
+
# ```
|
47
|
+
#
|
48
|
+
# Alternatively, define an entry in the structure that is always
|
49
|
+
# true. This is what you can do in case your structure is not a Hash, but an Array of duples.
|
50
|
+
#
|
51
|
+
# ```ruby
|
52
|
+
# cases [
|
53
|
+
# [(1..5), :low],
|
54
|
+
# [(6..10), :moderate],
|
55
|
+
# [(11..), :high],
|
56
|
+
# [Object, :unkown ], # Always true
|
57
|
+
# [(-5..-1), :never_reached]
|
58
|
+
# ]
|
59
|
+
#
|
60
|
+
# [-1, 3, 8, 100].map(&cases)
|
61
|
+
# # => [:unkown, :low, :moderate, :high]
|
62
|
+
# ```
|
63
|
+
#
|
64
|
+
# Notice the `:never_reached` symbol. Everything will be captured by the previous
|
65
|
+
# always true entry.
|
66
|
+
#
|
67
|
+
module CaseStruct
|
68
|
+
def [](key)
|
69
|
+
find(_case_struct_default) { |k, _| k === key }&.last
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_proc
|
73
|
+
proc { self[_1] }
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
def _case_struct_default
|
78
|
+
proc do
|
79
|
+
value = case
|
80
|
+
when respond_to?(:default_proc) && !default_proc.nil?
|
81
|
+
default_proc.call
|
82
|
+
when respond_to?(:default) && !default.nil?
|
83
|
+
default
|
84
|
+
end
|
85
|
+
[nil, value]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: case_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jesús Gómez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Enable using Hash like structures to be used as an alternative to case/when
|
14
|
+
expressions.
|
15
|
+
email: jgomo3@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/case_struct.rb
|
21
|
+
homepage: https://github.com/jgomo3/procer/
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.1.4
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Enable using Hash like structures to be used as an alternative to case/when
|
44
|
+
expressions.
|
45
|
+
test_files: []
|