shiny_json_logic 0.1.0
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/.gitignore +20 -0
- data/.idea/.gitignore +8 -0
- data/.idea/modules.xml +8 -0
- data/.idea/shiny_json_logic.iml +55 -0
- data/.idea/vcs.xml +6 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +140 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/core_ext/array.rb +16 -0
- data/lib/core_ext/hash.rb +7 -0
- data/lib/shiny_json_logic/operations/addition.rb +11 -0
- data/lib/shiny_json_logic/operations/all.rb +20 -0
- data/lib/shiny_json_logic/operations/and.rb +12 -0
- data/lib/shiny_json_logic/operations/base.rb +21 -0
- data/lib/shiny_json_logic/operations/concatenation.rb +13 -0
- data/lib/shiny_json_logic/operations/different.rb +12 -0
- data/lib/shiny_json_logic/operations/division.rb +11 -0
- data/lib/shiny_json_logic/operations/double_not.rb +12 -0
- data/lib/shiny_json_logic/operations/equal.rb +11 -0
- data/lib/shiny_json_logic/operations/filter.rb +18 -0
- data/lib/shiny_json_logic/operations/greater.rb +11 -0
- data/lib/shiny_json_logic/operations/greater_equal.rb +11 -0
- data/lib/shiny_json_logic/operations/if.rb +17 -0
- data/lib/shiny_json_logic/operations/inclusion.rb +11 -0
- data/lib/shiny_json_logic/operations/iterable/base.rb +50 -0
- data/lib/shiny_json_logic/operations/map.rb +14 -0
- data/lib/shiny_json_logic/operations/max.rb +11 -0
- data/lib/shiny_json_logic/operations/merge.rb +11 -0
- data/lib/shiny_json_logic/operations/min.rb +11 -0
- data/lib/shiny_json_logic/operations/missing.rb +22 -0
- data/lib/shiny_json_logic/operations/missing_some.rb +15 -0
- data/lib/shiny_json_logic/operations/modulo.rb +11 -0
- data/lib/shiny_json_logic/operations/none.rb +20 -0
- data/lib/shiny_json_logic/operations/not.rb +12 -0
- data/lib/shiny_json_logic/operations/or.rb +12 -0
- data/lib/shiny_json_logic/operations/product.rb +11 -0
- data/lib/shiny_json_logic/operations/reduce.rb +34 -0
- data/lib/shiny_json_logic/operations/smaller.rb +11 -0
- data/lib/shiny_json_logic/operations/smaller_equal.rb +11 -0
- data/lib/shiny_json_logic/operations/some.rb +18 -0
- data/lib/shiny_json_logic/operations/strict_different.rb +12 -0
- data/lib/shiny_json_logic/operations/strict_equal.rb +15 -0
- data/lib/shiny_json_logic/operations/substring.rb +16 -0
- data/lib/shiny_json_logic/operations/subtraction.rb +12 -0
- data/lib/shiny_json_logic/operations/var.rb +20 -0
- data/lib/shiny_json_logic/truthy.rb +14 -0
- data/lib/shiny_json_logic/version.rb +3 -0
- data/lib/shiny_json_logic.rb +106 -0
- data/shiny_json_logic.gemspec +33 -0
- metadata +169 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 767316e2e2775832da535aaac73c14677024aa61bac2ad51fedd38067977559c
|
|
4
|
+
data.tar.gz: e9bbd1bf5f8895317d28ffed44aad831418e6d42bfe308a5b479921e62566ab6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ddb2063f3bc0dbd508460fb3e48dae210de9d1fbe5886c2a6d9388c8be2637bf4a73666ec055d8d64e59cd832c88a6886d6cbdc4fe955a7d6282456b942ad4a2
|
|
7
|
+
data.tar.gz: 7b1aad77939d8b39aab1e835a7c1a5655fa7c1a955847059bd006a2867aca670e49bb1abe7af5db28186c0b699b736f1364e6fd12fafc63ee2238e39d70ce79f
|
data/.gitignore
ADDED
data/.idea/.gitignore
ADDED
data/.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/shiny_json_logic.iml" filepath="$PROJECT_DIR$/.idea/shiny_json_logic.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
|
3
|
+
<component name="ModuleRunConfigurationManager">
|
|
4
|
+
<shared />
|
|
5
|
+
</component>
|
|
6
|
+
<component name="NewModuleRootManager">
|
|
7
|
+
<content url="file://$MODULE_DIR$">
|
|
8
|
+
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
|
9
|
+
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
|
10
|
+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
|
11
|
+
</content>
|
|
12
|
+
<orderEntry type="jdk" jdkName="ruby-2.6.8-p205" jdkType="RUBY_SDK" />
|
|
13
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.2, ruby-2.6.8-p205) [gem]" level="application" />
|
|
15
|
+
<orderEntry type="library" scope="PROVIDED" name="byebug (v11.1.3, ruby-2.6.8-p205) [gem]" level="application" />
|
|
16
|
+
<orderEntry type="library" scope="PROVIDED" name="coderay (v1.1.3, ruby-2.6.8-p205) [gem]" level="application" />
|
|
17
|
+
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
18
|
+
<orderEntry type="library" scope="PROVIDED" name="method_source (v1.0.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
19
|
+
<orderEntry type="library" scope="PROVIDED" name="pry (v0.14.1, ruby-2.6.8-p205) [gem]" level="application" />
|
|
20
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
21
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec (v3.12.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
22
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.12.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
23
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.12.2, ruby-2.6.8-p205) [gem]" level="application" />
|
|
24
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.12.2, ruby-2.6.8-p205) [gem]" level="application" />
|
|
25
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.12.0, ruby-2.6.8-p205) [gem]" level="application" />
|
|
26
|
+
</component>
|
|
27
|
+
<component name="RakeTasksCache">
|
|
28
|
+
<option name="myRootTask">
|
|
29
|
+
<RakeTaskImpl id="rake">
|
|
30
|
+
<subtasks>
|
|
31
|
+
<RakeTaskImpl description="Build shiny_json_logic-0.1.0.gem into the pkg directory" fullCommand="build" id="build" />
|
|
32
|
+
<RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
|
|
33
|
+
<RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
|
|
34
|
+
<RakeTaskImpl description="Build and install shiny_json_logic-0.1.0.gem into system gems" fullCommand="install" id="install" />
|
|
35
|
+
<RakeTaskImpl id="install">
|
|
36
|
+
<subtasks>
|
|
37
|
+
<RakeTaskImpl description="Build and install shiny_json_logic-0.1.0.gem into system gems without network access" fullCommand="install:local" id="local" />
|
|
38
|
+
</subtasks>
|
|
39
|
+
</RakeTaskImpl>
|
|
40
|
+
<RakeTaskImpl description="Create tag v0.1.0 and build and push shiny_json_logic-0.1.0.gem to rubygems.org" fullCommand="release[remote]" id="release[remote]" />
|
|
41
|
+
<RakeTaskImpl description="Run RSpec code examples" fullCommand="spec" id="spec" />
|
|
42
|
+
<RakeTaskImpl description="" fullCommand="default" id="default" />
|
|
43
|
+
<RakeTaskImpl description="" fullCommand="release" id="release" />
|
|
44
|
+
<RakeTaskImpl id="release">
|
|
45
|
+
<subtasks>
|
|
46
|
+
<RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
|
|
47
|
+
<RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
|
|
48
|
+
<RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
|
|
49
|
+
</subtasks>
|
|
50
|
+
</RakeTaskImpl>
|
|
51
|
+
</subtasks>
|
|
52
|
+
</RakeTaskImpl>
|
|
53
|
+
</option>
|
|
54
|
+
</component>
|
|
55
|
+
</module>
|
data/.idea/vcs.xml
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
shiny_json_logic (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
byebug (11.1.3)
|
|
10
|
+
coderay (1.1.3)
|
|
11
|
+
diff-lcs (1.5.0)
|
|
12
|
+
method_source (1.0.0)
|
|
13
|
+
pry (0.14.1)
|
|
14
|
+
coderay (~> 1.1)
|
|
15
|
+
method_source (~> 1.0)
|
|
16
|
+
rake (10.5.0)
|
|
17
|
+
rspec (3.12.0)
|
|
18
|
+
rspec-core (~> 3.12.0)
|
|
19
|
+
rspec-expectations (~> 3.12.0)
|
|
20
|
+
rspec-mocks (~> 3.12.0)
|
|
21
|
+
rspec-core (3.12.0)
|
|
22
|
+
rspec-support (~> 3.12.0)
|
|
23
|
+
rspec-expectations (3.12.2)
|
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
25
|
+
rspec-support (~> 3.12.0)
|
|
26
|
+
rspec-mocks (3.12.2)
|
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
28
|
+
rspec-support (~> 3.12.0)
|
|
29
|
+
rspec-support (3.12.0)
|
|
30
|
+
|
|
31
|
+
PLATFORMS
|
|
32
|
+
ruby
|
|
33
|
+
|
|
34
|
+
DEPENDENCIES
|
|
35
|
+
bundler (~> 1.17)
|
|
36
|
+
byebug
|
|
37
|
+
pry
|
|
38
|
+
rake (~> 10.0)
|
|
39
|
+
rspec (~> 3.0)
|
|
40
|
+
shiny_json_logic!
|
|
41
|
+
|
|
42
|
+
BUNDLED WITH
|
|
43
|
+
2.7.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Luis Moyano
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# ShinyJsonLogic ✨
|
|
2
|
+
|
|
3
|
+
> Shine bright like a Ruby 💎
|
|
4
|
+
|
|
5
|
+
A **working** JSON Logic implementation in Ruby.
|
|
6
|
+
|
|
7
|
+
This gem exists because the official JSONLogic Ruby implementation has been incomplete and unmaintained for quite a while.
|
|
8
|
+
|
|
9
|
+
`ShinyJsonLogic` aims to be predictable, aligned with specs, test-driven and ready to use in Ruby's production code.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add it to your Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem "shiny_json_logic"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then run:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
bundle install
|
|
25
|
+
```
|
|
26
|
+
Or install it yourself:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
gem install shiny_json_logic
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
Basic usage is intentionally simple:
|
|
35
|
+
|
|
36
|
+
``` ruby
|
|
37
|
+
require "shiny_json_logic"
|
|
38
|
+
|
|
39
|
+
rule = {
|
|
40
|
+
"==" => [
|
|
41
|
+
{ "var" => "status" },
|
|
42
|
+
"active"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
data = { "status" => "active" }
|
|
47
|
+
|
|
48
|
+
ShinyJsonLogic.apply(rule, data)
|
|
49
|
+
# => true
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Nested Logic
|
|
53
|
+
You can nest rules as deeply as needed:
|
|
54
|
+
|
|
55
|
+
``` ruby
|
|
56
|
+
rule = {
|
|
57
|
+
"if" => [
|
|
58
|
+
{ "var" => "financing" },
|
|
59
|
+
{ "missing" => ["apr"] },
|
|
60
|
+
[]
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
data = { "financing" => true }
|
|
65
|
+
|
|
66
|
+
ShinyJsonLogic.apply(rule, data)
|
|
67
|
+
# => ["apr"]
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Supported operators
|
|
72
|
+
|
|
73
|
+
The goal is full JSON Logic coverage. Currently supported:
|
|
74
|
+
|
|
75
|
+
#### Logic
|
|
76
|
+
|
|
77
|
+
`if, and, or, !, !!`
|
|
78
|
+
|
|
79
|
+
#### Comparison
|
|
80
|
+
|
|
81
|
+
`==, ===, !=, !==, >, >=, <, <=`
|
|
82
|
+
|
|
83
|
+
#### Data access
|
|
84
|
+
|
|
85
|
+
`var, missing, missing_some`
|
|
86
|
+
|
|
87
|
+
#### Math
|
|
88
|
+
|
|
89
|
+
`+, -, *, /, %`
|
|
90
|
+
`min, max`
|
|
91
|
+
|
|
92
|
+
#### Strings
|
|
93
|
+
|
|
94
|
+
`cat, substr`
|
|
95
|
+
|
|
96
|
+
#### Arrays
|
|
97
|
+
|
|
98
|
+
`merge, in`
|
|
99
|
+
|
|
100
|
+
#### Iterable operations
|
|
101
|
+
`map, reduce, filter, some, all, none`
|
|
102
|
+
|
|
103
|
+
(See lib/shiny_json_logic/operations for the authoritative list.)
|
|
104
|
+
|
|
105
|
+
## Development
|
|
106
|
+
|
|
107
|
+
After checking out the repo:
|
|
108
|
+
|
|
109
|
+
``` bash
|
|
110
|
+
bin/setup
|
|
111
|
+
bundle exec rspec
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
You can also open a console with:
|
|
115
|
+
|
|
116
|
+
``` bash
|
|
117
|
+
bin/console
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
To install the gem locally:
|
|
121
|
+
|
|
122
|
+
``` bash
|
|
123
|
+
bundle exec rake install
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Contributing
|
|
127
|
+
|
|
128
|
+
Contributions are welcome — especially:
|
|
129
|
+
|
|
130
|
+
spec alignment improvements, missing operators, edge-case tests or performance improvements
|
|
131
|
+
|
|
132
|
+
Please include tests with any change.
|
|
133
|
+
|
|
134
|
+
Repository: https://github.com/luismoyano/shiny_json_logic
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT License.
|
|
139
|
+
|
|
140
|
+
Use it, fork it, ship it (:
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "shiny_json_logic"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'backport_dig' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
|
|
2
|
+
|
|
3
|
+
class Array
|
|
4
|
+
def self.wrap(object)
|
|
5
|
+
return [] if object.nil?
|
|
6
|
+
return object.to_ary || [object] if object.respond_to?(:to_ary)
|
|
7
|
+
|
|
8
|
+
[object]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def deep_fetch(index, default = nil)
|
|
12
|
+
indexes = index.to_s.split('.').map(&:to_i)
|
|
13
|
+
value = dig(*indexes) rescue default
|
|
14
|
+
value.nil? ? default : value # value can be false (Boolean)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "shiny_json_logic/truthy"
|
|
2
|
+
require "shiny_json_logic/operations/iterable/base"
|
|
3
|
+
|
|
4
|
+
module ShinyJsonLogic
|
|
5
|
+
module Operations
|
|
6
|
+
class All < Iterable::Base
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def on_each(_item)
|
|
10
|
+
ShinyJsonLogic.apply(filter, data)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def on_after(results)
|
|
14
|
+
return false if results.empty?
|
|
15
|
+
|
|
16
|
+
results.all? { |res| res == true }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "shiny_json_logic/truthy"
|
|
2
|
+
|
|
3
|
+
module ShinyJsonLogic
|
|
4
|
+
module Operations
|
|
5
|
+
class Base
|
|
6
|
+
def initialize(rules, data)
|
|
7
|
+
@rules = rules
|
|
8
|
+
@data = data
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
raise "Not implemented"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
attr_reader :rules
|
|
18
|
+
attr_accessor :data
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require "shiny_json_logic/truthy"
|
|
2
|
+
require "shiny_json_logic/operations/iterable/base"
|
|
3
|
+
|
|
4
|
+
module ShinyJsonLogic
|
|
5
|
+
module Operations
|
|
6
|
+
class Filter < Iterable::Base
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def on_each(item)
|
|
10
|
+
item if Truthy.call(ShinyJsonLogic.apply(filter, data))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def on_after(results)
|
|
14
|
+
results.compact
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "shiny_json_logic/truthy"
|
|
2
|
+
require "shiny_json_logic/operations/base"
|
|
3
|
+
|
|
4
|
+
module ShinyJsonLogic
|
|
5
|
+
module Operations
|
|
6
|
+
class If < Base
|
|
7
|
+
def call
|
|
8
|
+
rules.each_slice(2) do |condition, value|
|
|
9
|
+
return condition if value.nil?
|
|
10
|
+
return value if Truthy.call(condition)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
nil
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "shiny_json_logic/truthy"
|
|
2
|
+
|
|
3
|
+
module ShinyJsonLogic
|
|
4
|
+
module Operations
|
|
5
|
+
module Iterable
|
|
6
|
+
class Base < ShinyJsonLogic::Operations::Base
|
|
7
|
+
def initialize(rules, data)
|
|
8
|
+
super
|
|
9
|
+
@collection = ShinyJsonLogic.apply(rules.fetch(0), data) || []
|
|
10
|
+
@filter = rules.fetch(1)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
on_before
|
|
15
|
+
|
|
16
|
+
collection.map do |item|
|
|
17
|
+
on_before_each(item)
|
|
18
|
+
on_each(item)
|
|
19
|
+
end.then do |results|
|
|
20
|
+
on_after(results)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def on_each(item)
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def on_before_each(item)
|
|
31
|
+
data[current_key] = item
|
|
32
|
+
data.merge!(item) if item.is_a?(Hash)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def on_before
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def on_after(results)
|
|
39
|
+
results
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def current_key
|
|
43
|
+
""
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
attr_reader :collection, :filter
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|