val 0.0.0 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/val.rb +92 -11
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0803f1d8b1e7a46060786ab7d0877426986b1dc
4
- data.tar.gz: 20575e1b85cf0f670d885da5f09439d45ec53d12
3
+ metadata.gz: 9247cdb7fe4d2b3a94295af7b84796bab9cd79b2
4
+ data.tar.gz: eac3b09428e4dc6ba85899ff2654c22c12e5763f
5
5
  SHA512:
6
- metadata.gz: 61701f1ed849b74c05c7d272b6148e9a8ec1c21ec1f97f2a537ace68a2dddb6e050eb68a622d2e4d316295da49a57cae7f1a6b4dc1f3230aa991eb05cbfa3d66
7
- data.tar.gz: d8377455628b76cc4cbab41ab237321e3c0c0ca1f369b59f50659c31da54815c839ecf5f22959c2df723ce941f4be92b9af9fad94c5402186f4834d760ff460e
6
+ metadata.gz: 062e46a526133f70db1bffa87b4d0c765f61de4bc511738f4ba1b7753060bb626488e4b9aa8de340e05bf0333e412ecd453e839e3f2e1ae492923d6878f6a9f0
7
+ data.tar.gz: 9489c6a848da693fc028ae4a4c5579c0a9c3b329f40e17df79d3c5af3811f5320e2c5aadc6cd8827b647dcc3abfb5ee300a405077ee34fbea1e2aef17a4698a5
data/lib/val.rb CHANGED
@@ -1,23 +1,104 @@
1
1
  class Val
2
- require 'to_proc/all'
3
-
4
- OR = -> *values do
5
- -> value do
6
- values.any? &[:===, value]
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
- def OR *all
11
- OR[*all]
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
- Bool = new { OR[true, false] }
58
+ include DSL
15
59
 
16
60
  def initialize &block
17
- @type = instance_exec &block
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
- @type === value
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.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-07-29 00:00:00.000000000 Z
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: to_proc