Module: Arspy::Operators

Defined in:
lib/arspy/operators.rb,
lib/arspy/operators/selector.rb,
lib/arspy/operators/interpreter.rb,
lib/arspy/operators/selector/base.rb,
lib/arspy/operators/attribute_test.rb,
lib/arspy/operators/interpreter/base.rb,
lib/arspy/operators/attribute_test/base.rb,
lib/arspy/operators/selector/hash_selector.rb,
lib/arspy/operators/selector/range_selector.rb,
lib/arspy/operators/selector/string_selector.rb,
lib/arspy/operators/attribute_test/float_test.rb,
lib/arspy/operators/attribute_test/range_test.rb,
lib/arspy/operators/selector/integer_selector.rb,
lib/arspy/operators/attribute_test/regexp_test.rb,
lib/arspy/operators/attribute_test/string_test.rb,
lib/arspy/operators/attribute_test/integer_test.rb,
lib/arspy/operators/selector/attribute_selector.rb,
lib/arspy/operators/interpreter/null_interpreter.rb,
lib/arspy/operators/selector/unsupported_selector.rb,
lib/arspy/operators/interpreter/method_interpreter.rb,
lib/arspy/operators/attribute_test/unsupported_test.rb,
lib/arspy/operators/interpreter/attribute_interpreter.rb,
lib/arspy/operators/interpreter/association_interpreter.rb,
lib/arspy/operators/interpreter/abbreviated_attribute_interpreter.rb,
lib/arspy/operators/interpreter/abbreviated_association_interpreter.rb

Defined Under Namespace

Modules: AttributeTest, Interpreter, Selector

Constant Summary collapse

@@column_padding =
2

Class Method Summary collapse

Class Method Details

.awesome_print(klass_object_or_array, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/arspy/operators.rb', line 35

def self.awesome_print(klass_object_or_array, options={})
  #AwesomePrint.new(options).puts(klass_object_or_array)
  ap(klass_object_or_array)
  nil
end

.disable_abbreviationsObject



119
# File 'lib/arspy/operators.rb', line 119

def self.disable_abbreviations; Interpreter.enable_abbreviations(false); end

.enable_abbreviationsObject



118
# File 'lib/arspy/operators.rb', line 118

def self.enable_abbreviations; Interpreter.enable_abbreviations(true); end

.format_column_association(assoc) ⇒ Object



41
42
43
44
# File 'lib/arspy/operators.rb', line 41

def self.format_column_association(assoc)
  select_options = assoc.options.select{|k,v| [:through, :as, :polymorphic].include?(k)}
  [assoc.name.to_s, assoc.macro.to_s, "(#{assoc.options[:class_name] || assoc.name.to_s.singularize.camelize})", select_options.empty? ? '' : Hash[*select_options.flatten].inspect]
end

.format_column_field(field) ⇒ Object



45
46
47
# File 'lib/arspy/operators.rb', line 45

def self.format_column_field(field)
  [field.name.to_s, ":#{field.type}", format_db_type(field), format_db_type_modifiers(field)]
end

.format_db_type(field) ⇒ Object



48
49
50
51
52
# File 'lib/arspy/operators.rb', line 48

def self.format_db_type(field)
  prec_scale = field.precision && field.scale ? "#{field.precision}:#{field.scale}" : ""
  default = field.default ? " (#{field.default})" : ""
  "#{field.sql_type}#{prec_scale}#{default}"
end

.format_db_type_modifiers(field) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/arspy/operators.rb', line 53

def self.format_db_type_modifiers(field)
  modifiers = []
  modifiers << 'PRIMARY' if field.primary
  modifiers << 'NOT NULL' unless field.null
  #modifiers << "LIMIT(#{field.limit})" if field.limit
  modifiers.join(',')
end

.interpret(array, method_name, *args) ⇒ Object



121
122
123
# File 'lib/arspy/operators.rb', line 121

def self.interpret(array, method_name, *args)
  Interpreter.for(array, method_name).interpret(*args)
end

.list_associations(klass_or_object, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/arspy/operators.rb', line 8

def self.list_associations(klass_or_object, *args)
  active_record_klass = klass_or_object.is_a?(Class) ? klass_or_object : klass_or_object.class
  return unless active_record_klass.ancestors.include?(ActiveRecord::Base)
  counts = {}
  rows = active_record_klass.reflect_on_all_associations.map do |assoc|
    counts[assoc.macro] ||= 0
    counts[assoc.macro] += 1
    self.format_column_association(assoc)
  end
  rows.sort!{|row1,row2| row1.first <=> row2.first}
  return rows if args.include?(:data)
  self.print_matrix(rows)
  "Total: #{counts.inject(0){|sum, count| sum+count.last}} (" + counts.map{|count| "#{count.last} #{count.first}" }.join(', ') + ")"
end

.list_fields(klass_or_object, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/arspy/operators.rb', line 23

def self.list_fields(klass_or_object, *args)
  active_record_klass = klass_or_object.is_a?(Class) ? klass_or_object : klass_or_object.class
  return unless active_record_klass.ancestors.include?(ActiveRecord::Base)
  rows = active_record_klass.columns.map do |column|
    self.format_column_field(column)
  end
  rows.sort!{|row1,row2| row1.first <=> row2.first}
  return rows if args.include?(:data)
  self.print_matrix(rows)
  "Total #{active_record_klass.columns.size} field#{active_record_klass.columns.size == 1 ? '' : 's'}"
end


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/arspy/operators.rb', line 61

def self.print_array(array, *args)
  if args.empty?
    case array.first
    when ActiveRecord::Base then ap(array)
    else
      array.each{|element| puts element}
    end
  end
  #array.each{|element| puts element.is_a?(String) ? element : element.inspect } if args.empty?
  self.print_matrix(
    array.map do |obj|
      args.map do |arg|
        case arg
        when Symbol then obj.__send__(arg)
        when String then obj.respond_to?(arg) ? obj.__send__(arg) : (obj.instance_eval(arg) rescue nil)
        else nil
        end
      end
    end
  ) unless args.empty?
  nil
end


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/arspy/operators.rb', line 127

def self.print_matrix(matrix_array)
  return nil if matrix_array.empty?
  raise 'Cannot print a non-matrix array' unless matrix_array.all?{|ar| ar.is_a? Array }

  columns_per_row = matrix_array.map{|ar| ar.size }.max
  init_array = Array.new(columns_per_row, 0)
  max_widths = matrix_array.inject(init_array)do |mw, row|
    row.each_with_index do |string, index|
      mw[index] = [string.to_s.length, mw[index]].max
    end
    mw
  end
  matrix_array.each do |row|
    index = -1
    puts (row.map{|column| column.to_s + ' '*(max_widths[index += 1] - column.to_s.length) }.join(' '*@@column_padding))
  end
  nil
end


84
85
86
87
88
89
# File 'lib/arspy/operators.rb', line 84

def self.print_object(object, *args)
  print_matrix([args.map{|arg| object[arg]}]) unless args.empty?
  #AwesomePrint.new.puts(object) if args.empty?
  ap(object) if args.empty?
  nil
end

.test_object(obj, args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/arspy/operators.rb', line 90

def self.test_object(obj, args)
  args.any? do |arg|
    case arg
    when String then obj.instance_eval(arg) rescue false
    when Integer then obj.id == arg
    when Hash
      arg.any?{|key,val| self.test_attribute(obj, key, (val.is_a?(Array) ? val : [val]) ) }
    else
      false
    end
  end
end

.with(array, *args) ⇒ Object

t.people.with(20..30) t.people.with(2,3,4) t.people.with(:name=>[/Ed/, /Jen/, ‘Joe’]) t.people.with(:age=>10..20) t.people.with(‘statistics.statistic_type.abbrevation’=>%w(GP SV% GAA))



107
108
109
110
111
112
# File 'lib/arspy/operators.rb', line 107

def self.with(array, *args)
  return array if (args.empty? || array.nil? || array.empty?)
  array.select{|obj| 
    obj && args.any?{|arg|
      Selector.for(arg).select?(obj) }}
end

.without(array, *args) ⇒ Object



113
114
115
116
# File 'lib/arspy/operators.rb', line 113

def self.without(array, *args)
  return array if (args.empty? || array.nil? || array.empty?)
  array.select{|obj| obj && !args.any?{|arg| Selector.for(arg).select?(obj) }}
end