Module: ActiverecordMysqlRepl::Extensions::Object

Defined in:
lib/active_record_mysql_repl/extensions/object.rb

Instance Method Summary collapse

Instance Method Details

#copyObject Also known as: cp



123
124
125
# File 'lib/active_record_mysql_repl/extensions/object.rb', line 123

def copy
  Clipboard.copy(to_s)
end

#csv(orientation = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_record_mysql_repl/extensions/object.rb', line 72

def csv(orientation = nil)
  arr = if is_a?(Array)
    self
  elsif is_a?(::ActiveRecord::Relation)
    to_a
  else
    [self]
  end

  arr = arr.map do |e|
    e = e.attributes if e.is_a?(::ActiveRecord::Base)
    next e unless e.is_a?(Hash)
    next e.transform_values do |v|
      next JSON.pretty_generate(v) if v.is_a?(Enumerable) && v.size > 0
      next v.to_time if v.is_a?(Time) || v.is_a?(Date)
      next "NULL" if v.nil?
      next '""' if v == ""
      v.to_s
    end
  end

  raise "#{arr} is not an array of hash".red unless arr.all? { |e| e.is_a?(Hash) }
  raise "#{arr} contains Hashes with different keys".red unless arr.map(&:keys).uniq.size == 1

  orientation = case orientation
  when :vertical, :v
    :vertical
  when :horizontal, :h
    :horizontal
  else
    if arr.first.keys.size > 5
      :vertical
    else
      :horizontal
    end
  end

  CSV.generate do |csv|
    case orientation
    when :horizontal
      csv << arr.first.keys
      arr.each { |row| csv << row.values }
    when :vertical
      arr.each do |row|
        row.each { |col, val| csv << [col, val] }
        csv << []
      end
    end
  end
end

#jpObject



130
131
132
# File 'lib/active_record_mysql_repl/extensions/object.rb', line 130

def jp
  JSON.pretty_generate(JSON.parse(to_json))
end

#tabulate(orientation = nil) ⇒ Object Also known as: tab, table, to_table



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_record_mysql_repl/extensions/object.rb', line 11

def tabulate(orientation = nil)
  arr = if is_a?(Array)
    self
  elsif is_a?(::ActiveRecord::Relation)
    to_a
  else
    [self]
  end

  arr = arr.map do |e|
    e = e.attributes if e.is_a?(::ActiveRecord::Base)
    next e unless e.is_a?(Hash)
    next e.transform_values do |v|
      next JSON.pretty_generate(v) if v.is_a?(Enumerable) && v.size > 0
      next v.to_time if v.is_a?(Time) || v.is_a?(Date)
      next "NULL" if v.nil?
      next '""' if v == ""
      v.to_s
    end
  end

  raise "#{arr} is not an array of hash".red unless arr.all? { |e| e.is_a?(Hash) }
  raise "#{arr} contains Hashes with different keys".red unless arr.map(&:keys).uniq.size == 1

  orientation = case orientation
  when :vertical, :v
    :vertical
  when :horizontal, :h
    :horizontal
  else
    if arr.first.keys.size > 5
      :vertical
    else
      :horizontal
    end
  end

  t = Terminal::Table.new

  case orientation
  when :horizontal
    t.headings = arr.first.keys
    t.rows = arr.map(&:values)

  when :vertical
    headings = ["Column", arr.map.with_index { |_, i| "Record#{i+1}" }].flatten
    t.headings = headings

    arr.first.keys.each_with_index do |col|
      cols = [col, arr.map { |r| r[col] }].flatten
      t.add_row cols
    end
  end

  t.to_s
end