Module: Rufus::Decision

Defined in:
lib/rufus/decision/table.rb,
lib/rufus/decision/hashes.rb,
lib/rufus/decision/version.rb

Defined Under Namespace

Classes: EvalHashFilter, HashFilter, Table

Constant Summary

TREECHECKER = An abstract syntax tree check (prior to any ruby eval).
Rufus::TreeChecker.new do

  exclude_fvccall :abort, :exit, :exit!
  exclude_fvccall :system, :fork, :syscall, :trap, :require, :load

  #exclude_call_to :class
  exclude_fvcall :private, :public, :protected

  exclude_def               # no method definition
  exclude_eval              # no eval, module_eval or instance_eval
  exclude_backquotes        # no `rm -fR the/kitchen/sink`
  exclude_alias             # no alias or aliast_method
  exclude_global_vars       # $vars are off limits
  exclude_module_tinkering  # no module opening
  exclude_raise             # no raise or throw

  exclude_rebinding Kernel # no 'k = Kernel'

  exclude_access_to(
    IO, File, FileUtils, Process, Signal, Thread, ThreadGroup)

  exclude_class_tinkering

  exclude_call_to :instance_variable_get, :instance_variable_set
end
VERSION =
'1.3.0'

Class Method Summary

Class Method Details

+ (Object) check_and_eval(ruby_code, bndng = binding())

Given a string (of ruby code), first makes sure it doesn’t contain harmful code, then evaluates it.



205
206
207
208
209
210
211
212
# File 'lib/rufus/decision/hashes.rb', line 205

def self.check_and_eval (ruby_code, bndng=binding())

  TREECHECKER.check(ruby_code)

  # OK, green for eval...

  eval(ruby_code, bndng)
end

+ (Object) csv_to_a(csv)

Given a CSV string or the URI / path to a CSV file, turns the CSV into an array of array.



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/rufus/decision/table.rb', line 592

def self.csv_to_a (csv)

  return csv if csv.is_a?(Array)

  csv = csv.to_s if csv.is_a?(URI)
  csv = open(csv) if is_uri?(csv)

  csv_lib = defined?(CSV::Reader) ? CSV::Reader : CSV
    # no CSV::Reader for Ruby 1.9.1

  csv_lib.parse(csv).inject([]) { |rows, row|
    row = row.collect { |cell| cell ? cell.strip : '' }
    rows << row if row.find { |cell| (cell != '') }
    rows
  }
end

+ (Boolean) is_uri?(string)

Returns true if the string is a URI false if it’s something else (CSV data ?)

Returns:

  • (Boolean)


612
613
614
615
616
617
618
619
620
621
622
# File 'lib/rufus/decision/table.rb', line 612

def self.is_uri? (string)

  return false if string.index("\n") # quick one

  begin
    URI::parse(string); return true
  rescue
  end

  false
end

+ (Object) transpose(a)

Turns an array of array (rows / columns) into an array of hashes. The first row is considered the “row of keys”.

  [
    [ 'age', 'name' ],
    [ 33, 'Jeff' ],
    [ 35, 'John' ]
  ]

>

 [
   { 'age' => 33, 'name' => 'Jeff' },
   { 'age' => 35, 'name' => 'John' }
 ]

You can also pass the CSV as a string or the URI/path to the actual CSV file.



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/rufus/decision/table.rb', line 643

def self.transpose (a)

  a = csv_to_a(a) if a.is_a?(String)

  return a if a.empty?

  first = a.first

  if first.is_a?(Hash)

    keys = first.keys.sort
    [ keys ] + a.collect { |row|
      keys.collect { |k| row[k] }
    }
  else

    keys = first
    a[1..-1].collect { |row|
      (0..keys.size - 1).inject({}) { |h, i| h[keys[i]] = row[i]; h }
    }
  end
end