aristotle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/aristotle +4 -0
- data/lib/aristotle.rb +2 -0
- data/lib/aristotle/logic.rb +133 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bdff9adb6b5b8a2da5ffb3c10670043f5909992a
|
4
|
+
data.tar.gz: 04f455894c22dc98b43321dd0aa41c12ac2bfc29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65974da87a7d075657cc30aa21ee97ee641978280cddb69672daed76a11e294d01d8a72b76a755182b0d9327a651736d3867b8bf254437d0a8e072bd12f129a8
|
7
|
+
data.tar.gz: 7e78e67679d174af63b1b9089392f4e15b7237e1f5a595a8bac6f6d2eab27a85f87b599ce12a4b11d9ae75c441e54a7d2fad96d1188d74ee6d8b866d84c37a6f
|
data/bin/aristotle
ADDED
data/lib/aristotle.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
module Aristotle
|
2
|
+
class Logic
|
3
|
+
def initialize(object)
|
4
|
+
@object = object
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.condition(expression, &block)
|
8
|
+
@@conditions ||= {}
|
9
|
+
@@conditions[expression.source] = block
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.action(expression, &block)
|
13
|
+
@@actions ||= {}
|
14
|
+
@@actions[expression.source] = block
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.load_commands
|
18
|
+
@@commands ||= {}
|
19
|
+
|
20
|
+
return if @@commands != {}
|
21
|
+
|
22
|
+
#{Rails.root}/
|
23
|
+
filename = "app/logic/#{logic_name}.logic"
|
24
|
+
logic_data = File.read(filename)
|
25
|
+
|
26
|
+
found_lines = []
|
27
|
+
command = ''
|
28
|
+
|
29
|
+
lines = logic_data.split("\n").map(&:rstrip).select { |l| l != '' }
|
30
|
+
lines.each do |line|
|
31
|
+
next if line.strip.start_with? '#'
|
32
|
+
|
33
|
+
if !line.start_with?(' ')
|
34
|
+
if command != '' && found_lines.length > 0
|
35
|
+
@@commands[command] = found_lines
|
36
|
+
found_lines = []
|
37
|
+
end
|
38
|
+
command = line
|
39
|
+
elsif line.start_with?(' ')
|
40
|
+
found_lines << line.strip
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@@commands[command] = found_lines if command != '' && found_lines.length > 0
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.html_rules
|
47
|
+
load_commands
|
48
|
+
@@commands.map do |command, lines|
|
49
|
+
"<strong>#{command}</strong><ul>#{lines.map{|l| "<li>- #{l.gsub(' if ', ' <strong style="color:blue">IF</strong> ').gsub(/'([^']+)'/, '<strong>\1</strong>')}</li>"}.join}</ul>"
|
50
|
+
end.join('<br>').html_safe
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_condition(condition)
|
54
|
+
if @@conditions.has_key? condition
|
55
|
+
@@conditions[condition].call(@object)
|
56
|
+
else
|
57
|
+
raise "Condition not found: #{condition}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def do_action(action)
|
62
|
+
if @@actions.has_key? action
|
63
|
+
@@actions[action].call(@object)
|
64
|
+
else
|
65
|
+
raise "Action not found: #{action}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def commands(logic_method)
|
70
|
+
self.class.load_commands
|
71
|
+
@@commands[logic_method] || []
|
72
|
+
end
|
73
|
+
|
74
|
+
def process(logic_method)
|
75
|
+
commands(logic_method).each do |command|
|
76
|
+
action, condition = command.split(' if ', 2)
|
77
|
+
|
78
|
+
if process_condition(condition)
|
79
|
+
# puts "Condition matched: #{condition}"
|
80
|
+
# puts "Doing action: #{action}"
|
81
|
+
return do_action(action)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.is_everything_covered?
|
89
|
+
load_commands
|
90
|
+
not_covered = {}
|
91
|
+
@@commands.each do |logic_method, command|
|
92
|
+
command.each do |line|
|
93
|
+
action, condition = line.split(' if ', 2)
|
94
|
+
|
95
|
+
unless @@actions.has_key?(action)
|
96
|
+
not_covered[logic_method] ||= {actions: [], conditions: []}
|
97
|
+
not_covered[logic_method][:actions] << action
|
98
|
+
end
|
99
|
+
unless @@conditions.has_key?(condition)
|
100
|
+
not_covered[logic_method] ||= {actions: [], conditions: []}
|
101
|
+
not_covered[logic_method][:conditions] << condition
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
if not_covered != {}
|
107
|
+
not_covered.each do |request_method, data|
|
108
|
+
puts "\nclass #{self.to_s} < Aristotle::Logic"
|
109
|
+
puts " # #{request_method}:\n"
|
110
|
+
data[:actions].each do |action|
|
111
|
+
puts " action /#{action}/ do |#{logic_name}|\n\n end\n\n"
|
112
|
+
end
|
113
|
+
data[:conditions].each do |condition|
|
114
|
+
puts " condition /#{condition}/ do |#{logic_name}|\n\n end\n\n"
|
115
|
+
end
|
116
|
+
puts "end"
|
117
|
+
end
|
118
|
+
else
|
119
|
+
puts '-> Everything is covered!'
|
120
|
+
end
|
121
|
+
|
122
|
+
not_covered == {}
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.logic_name
|
126
|
+
self.to_s.gsub(/Logic$/, '').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.descendants
|
130
|
+
ObjectSpace.each_object(Class).select { |klass| klass < self }
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aristotle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marius Andra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby business logic engine, inspired by Cucumber
|
14
|
+
email: marius.andra@gmail.com
|
15
|
+
executables:
|
16
|
+
- aristotle
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/aristotle
|
21
|
+
- lib/aristotle.rb
|
22
|
+
- lib/aristotle/logic.rb
|
23
|
+
homepage: http://rubygems.org/gems/aristotle
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.2.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Business logic
|
47
|
+
test_files: []
|