aristotle 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c1d907da31b330af457d62abfc97661db64d65d
4
- data.tar.gz: bded9bf0b597e36a730749fbf5ff4987a4734481
3
+ metadata.gz: beb607b0e69afe31331e87b364ebe0596d446b6e
4
+ data.tar.gz: c1accdb3321d49082eaff261bda5f77995f54f46
5
5
  SHA512:
6
- metadata.gz: cd4eff834e89b79e76c0489a6103b8de898b0c52c44b6848c54e45df50c0776d4c55d5fb6125297725e489458f0f5f1d634c80e373485c20a57e1bc0798aa29d
7
- data.tar.gz: 894dbe8c782f0ec6e5517f811e8ff02a18c01a54e719d7fc989bc1a3883c235f6d9f952f1f61092bb733ffa02f7bb54add674081de95277190ba9849eb3629fc
6
+ metadata.gz: 30cebb52fa456e598a643fae0c691c070b2ad886e703e28e2a8a637926d71bd6a641947b74bb957edf631b920a99eab61bf267fbae20c81a0122fc442406d5d1
7
+ data.tar.gz: a93c35022426c2e8bd9dcbdeb51d42120ee0520875191760dba25204a49f98166ce2a78e500b099891f1ac67c18a5a8ea844db52def1dfdc3611de4e60a71821
@@ -1,6 +1,6 @@
1
1
  module Aristotle
2
2
  class Command
3
- attr_reader :action, :condition
3
+ attr_reader :action, :condition, :action_proc, :condition_proc
4
4
 
5
5
  def initialize(line, conditions, actions)
6
6
  @action, @condition = line.split(' if ', 2).map(&:strip)
@@ -4,19 +4,64 @@ module Aristotle
4
4
  @klass = klass
5
5
  end
6
6
 
7
- def html_rules
8
- @klass.commands.map do |command, lines|
9
- "<strong>#{command}</strong>"+
7
+ def html_rules(show_code: true)
8
+ @klass.commands.map do |command_title, commands|
9
+ "<strong>#{command_title}</strong>"+
10
10
  "<ul>"+
11
- lines.map do |line|
11
+ commands.map do |command|
12
12
  "<li>- "+
13
- line.action.gsub(/'([^']+)'/, '<strong>\1</strong>')+
13
+ format_fragment(command, :action, show_code: show_code)+
14
14
  " <strong style='color:blue'>IF</strong> "+
15
- line.condition.gsub(/'([^']+)'/, '<strong>\1</strong>')+
15
+ format_fragment(command, :condition, show_code: show_code)+
16
16
  "</li>"
17
17
  end.join +
18
18
  "</ul>"
19
19
  end.join('<br>').html_safe
20
20
  end
21
+
22
+ protected
23
+
24
+ def format_fragment(fragment, part, show_code: true)
25
+ return '' if part != :action && part != :condition
26
+
27
+ text = fragment.send(part).to_s
28
+ text.gsub!(/'([^']+)'/, '<strong>\1</strong>')
29
+
30
+ proc = fragment.send("#{part}_proc")
31
+
32
+ return "<span style='color:red'>#{text}</span>" if proc.blank?
33
+ return text unless show_code
34
+
35
+ code = find_code(*proc.source_location) || 'no code found'
36
+
37
+ code_block = "<span style='color:#aaaaaa;'>#{proc.source_location.join(':')}</span>\n"+
38
+ "<span style='color:#333333'>#{code.join("\n")}</span>"
39
+
40
+ "<span style='position:relative;cursor:help;' onmouseover='this.children[0].style.display=\"block\";' onmouseout='this.children[0].style.display=\"none\";'>"+
41
+ "<pre style='position:absolute;cursor:text;display:none;background:#ffffff;margin-top:0;padding:6px 8px;box-shadow:2px 2px 8px rgba(0,0,0,0.3);z-index:30000;'>"+
42
+ "#{code_block}"+
43
+ "</pre>"+
44
+ "#{text}"+
45
+ "</span>"
46
+ end
47
+
48
+ def find_code(file, line_number)
49
+ lines = load_file(file)
50
+ first_line = lines[line_number - 1]
51
+ indention = first_line.index(/[^ ]/)
52
+
53
+ code_lines = lines[(line_number - 1)..-1]
54
+ end_regexp = Regexp.new('^' + (' ' * indention) + 'end *')
55
+ to = code_lines.find_index { |line| line =~ end_regexp } + 1
56
+
57
+ code_lines.first(to).map { |line| line[indention..-1] }
58
+ rescue
59
+ nil
60
+ end
61
+
62
+ def load_file(file)
63
+ @files ||= {}
64
+ @files[file] ||= open(file).read.split("\n").map(&:rstrip)
65
+ end
21
66
  end
22
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aristotle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marius Andra