Skip to content

Commit 4ac423a

Browse files
author
Kenneth Geerts
committed
Simplify code
1 parent 0f572bc commit 4ac423a

File tree

6 files changed

+26
-38
lines changed

6 files changed

+26
-38
lines changed

json_logic.gemspec

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44
require 'json_logic/version'
55

66
Gem::Specification.new do |spec|
7-
spec.name = "json_logic"
7+
spec.name = 'json_logic'
88
spec.version = JSONLogic::VERSION
9-
spec.authors = ["Kenneth Geerts"]
10-
spec.email = ["Kenneth.Geerts@up-nxt.com"]
9+
spec.authors = ['Kenneth Geerts']
10+
spec.email = ['Kenneth.Geerts@gmail.com']
1111

12-
spec.summary = %q{See http://jsonlogic.com}
13-
spec.description = %q{JSONLogic parser in ruby.}
14-
spec.license = "MIT"
12+
spec.summary = 'Build complex rules, serialize them as JSON, and execute them in ruby'
13+
spec.description = 'See http://jsonlogic.com'
14+
spec.license = 'MIT'
1515

1616
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
1717
# to allow pushing to a single host or delete this section to allow pushing to any host.
1818
if spec.respond_to?(:metadata)
1919
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
2020
else
21-
raise "RubyGems 2.0 or newer is required to protect against " \
22-
"public gem pushes."
21+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
2322
end
2423

2524
spec.files = `git ls-files -z`.split("\x0").reject do |f|
2625
f.match(%r{^(test|spec|features)/})
2726
end
28-
spec.bindir = "exe"
27+
spec.bindir = 'exe'
2928
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30-
spec.require_paths = ["lib"]
29+
spec.require_paths = ['lib']
3130

32-
spec.add_development_dependency "bundler", "~> 1.13"
33-
spec.add_development_dependency "rake", "~> 10.0"
34-
spec.add_development_dependency "minitest", "~> 5.0"
31+
spec.add_development_dependency 'bundler', '~> 1.13'
32+
spec.add_development_dependency 'rake', '~> 10.0'
33+
spec.add_development_dependency 'minitest', '~> 5.0'
3534
end

lib/json_logic.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
require 'core_ext/deep_fetch'
2-
32
require 'json_logic/truthy'
43
require 'json_logic/operation'
5-
require 'json_logic/rule_set'
6-
require 'json_logic/version'
74

85
module JSONLogic
9-
def self.apply(logic, data = {})
10-
RuleSet.new(logic, data).evaluate
6+
def self.apply(logic, data)
7+
return logic unless logic.is_a?(Hash) # pass-thru
8+
operator, values = logic.first # unwrap single-key hash
9+
values = [values] unless values.is_a?(Array) # syntactic sugar
10+
values.map! { |value| apply(value, data) } # recursion step
11+
values.flatten!(1) if values.size == 1 # [['A']] => ['A']
12+
Operation.perform(operator, values, data || {}) # perform operation
1113
end
1214
end
15+
16+
require 'json_logic/version'

lib/json_logic/rule_set.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/json_logic/truthy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# http://jsonlogic.com/truthy.html
1+
# JsonLogic truthy / falsy logic
2+
# Cf. http://jsonlogic.com/truthy.html
23

34
class Object
45
def truthy?

lib/json_logic/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JSONLogic
2-
VERSION = '0.1'
2+
VERSION = '0.0.1'
33
end

test/json_logic_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
require 'json_logic'
88

99
class JSONLogicTest < Minitest::Test
10-
tests = JSON.parse(open('http://jsonlogic.com/tests.json').read)
10+
test_suite_url = 'http://jsonlogic.com/tests.json'
11+
tests = JSON.parse(open(test_suite_url).read)
1112
count = 1
1213
tests.each do |pattern|
1314
next unless pattern.is_a?(Array)

0 commit comments

Comments
 (0)