val 0.0.0 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/val.rb +92 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9247cdb7fe4d2b3a94295af7b84796bab9cd79b2
|
4
|
+
data.tar.gz: eac3b09428e4dc6ba85899ff2654c22c12e5763f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 062e46a526133f70db1bffa87b4d0c765f61de4bc511738f4ba1b7753060bb626488e4b9aa8de340e05bf0333e412ecd453e839e3f2e1ae492923d6878f6a9f0
|
7
|
+
data.tar.gz: 9489c6a848da693fc028ae4a4c5579c0a9c3b329f40e17df79d3c5af3811f5320e2c5aadc6cd8827b647dcc3abfb5ee300a405077ee34fbea1e2aef17a4698a5
|
data/lib/val.rb
CHANGED
@@ -1,23 +1,104 @@
|
|
1
1
|
class Val
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
module DSL
|
3
|
+
def OR *all
|
4
|
+
Op::OR[*all]
|
5
|
+
end
|
6
|
+
|
7
|
+
def val &block
|
8
|
+
self.class.new &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def key name, *conditions
|
12
|
+
first = conditions.first
|
13
|
+
|
14
|
+
if [Module, Val, NilClass].any? &[first, :is_a?]
|
15
|
+
type = first
|
16
|
+
@conditions << Key.new(name, type)
|
17
|
+
else
|
18
|
+
@conditions.concat conditions.map { |array|
|
19
|
+
ArrayCondition.new name, array
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_a type
|
25
|
+
condition = -> value { value.is_a? type }
|
26
|
+
@conditions << condition
|
27
|
+
end
|
28
|
+
|
29
|
+
def is array
|
30
|
+
condition = -> value {
|
31
|
+
begin
|
32
|
+
array.to_proc === value
|
33
|
+
rescue TypeError, ArgumentError
|
34
|
+
false
|
35
|
+
end
|
36
|
+
}
|
37
|
+
@conditions << condition
|
38
|
+
end
|
39
|
+
|
40
|
+
def m name
|
41
|
+
condition = -> value {
|
42
|
+
value.respond_to? name
|
43
|
+
}
|
44
|
+
@conditions << condition
|
7
45
|
end
|
8
46
|
end
|
9
|
-
|
10
|
-
|
11
|
-
OR
|
47
|
+
|
48
|
+
module Op
|
49
|
+
OR = -> *values do
|
50
|
+
-> value do
|
51
|
+
values.any? &[:===, value]
|
52
|
+
end
|
53
|
+
end
|
12
54
|
end
|
55
|
+
|
56
|
+
require 'to_proc/all'
|
13
57
|
|
14
|
-
|
58
|
+
include DSL
|
15
59
|
|
16
60
|
def initialize &block
|
17
|
-
@
|
61
|
+
@conditions = []
|
62
|
+
result = instance_exec &block
|
63
|
+
if @conditions.empty?
|
64
|
+
condition = result
|
65
|
+
@conditions << condition
|
66
|
+
end
|
18
67
|
end
|
19
68
|
|
20
69
|
def === value
|
21
|
-
@
|
70
|
+
@conditions.all? &[:===, value]
|
71
|
+
end
|
72
|
+
|
73
|
+
Bool = new { OR(true, false) }
|
74
|
+
|
75
|
+
class ArrayCondition
|
76
|
+
def initialize name, array
|
77
|
+
@name, @proc = name, array.to_proc
|
78
|
+
end
|
79
|
+
|
80
|
+
def === value
|
81
|
+
@proc === value[@name]
|
82
|
+
rescue ArgumentError
|
83
|
+
false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class Key
|
88
|
+
def initialize name, type
|
89
|
+
@name, @type = name, type
|
90
|
+
end
|
91
|
+
|
92
|
+
def === value
|
93
|
+
value.respond_to?(:[]) && value[@name] && valid_type?(value[@name])
|
94
|
+
end
|
95
|
+
|
96
|
+
def valid_type? name
|
97
|
+
if @type
|
98
|
+
@type === name
|
99
|
+
else
|
100
|
+
true
|
101
|
+
end
|
102
|
+
end
|
22
103
|
end
|
23
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: val
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: to_proc
|