Class: Bali::Printer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bali/printer.rb

Overview

module that would allow all defined rules to be printed for check

Constant Summary collapse

SEPARATOR =
" " * 6
SUBTARGET_TITLE_SEPARATOR =
SEPARATOR + ("-" * 80) + "\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pretty_printObject



15
16
17
# File 'lib/bali/printer.rb', line 15

def self.pretty_print
  printable
end

.printableObject



11
12
13
# File 'lib/bali/printer.rb', line 11

def self.printable
  instance.printable
end

Instance Method Details

#load_rule_classesObject



19
20
21
22
23
24
25
26
27
# File 'lib/bali/printer.rb', line 19

def load_rule_classes
  return unless Bali.config.rules_path.present?

  Dir["#{Bali.config.rules_path}/**/*.rb"].each do |rule_class_path|
    require rule_class_path
  end
rescue LoadError
  # ignore
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bali/printer.rb', line 51

def print_role role, target_io
  subtarget = role.name.to_s.capitalize
  subtarget = "By default" if subtarget.blank?
  can_all = role.can_all?
  counter = 0

  target_io << "#{SEPARATOR}#{subtarget}\n"
  target_io << SUBTARGET_TITLE_SEPARATOR

  if can_all
    target_io << "#{SEPARATOR}  #{counter+=1}. #{subtarget} can do anything except if explicitly stated otherwise\n"
  end

  role.rules.each do |rule|
    written_rule = StringIO.new
    written_rule << "#{SEPARATOR}  #{counter+=1}. #{subtarget} #{rule.term} #{rule.operation}"
    if rule.conditional?
      written_rule << ", with condition"
    end
    written_rule << "\n"
    target_io << written_rule.string
  end
end

#printableObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bali/printer.rb', line 29

def printable
  load_rule_classes
  output = StringIO.new

  # build up the string for pretty printing rules
  rule_classes = ObjectSpace.each_object(Class).select { |cls| cls < Bali::Rules }
  rule_classes.sort! { |a, b| a.to_s <=> b.to_s }
  rule_classes.each do |rule_class|
    output << "===== #{rule_class.model_class} =====\n\n"

    rule_class.ruler.roles.each do |subtarget, role|
      print_role role, output
    end

    output << "\n\n"
  end

  output << DateTime.now.strftime("Printed at %Y-%m-%d %I:%M%p %Z")

  output.string
end