Class: Describe

Inherits:
IRB::Command::Base
  • Object
show all
Defined in:
lib/alet/irb/command/describe.rb

Instance Method Summary collapse

Instance Method Details

#crate_relation_table(schema) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/alet/irb/command/describe.rb', line 112

def crate_relation_table(schema)
  rows =
    schema.parent_relations.map{|r| [r[:name], t('desc.relation_type.parent'), r[:class_name], r[:field]]} +
    schema.child_relations.map{|r| [r[:name], t('desc.relation_type.child'), r[:class_name], r[:field]]}

  TTY::Table.new(
    [t('desc.column.relation_name'), t('desc.column.relation_type'), t('desc.column.relation_class'), t('desc.column.relation_field')],
    rows)
end

#create_field_table(schema) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/alet/irb/command/describe.rb', line 90

def create_field_table(schema)
  rows = []
  schema.fields.each do |f|
    if f.picklist_values.empty?
      rows << [f.label, f.name, f.type, '']
    else
      f.picklist_values.each_with_index do |pv, i|
        next unless pv.active
        if i == 0
          rows << [f.label, f.name, f.type, %|#{f.label}/#{pv.value}|]
        else
          rows << ['', '', '', %|#{f.label}/#{pv.value}|]
        end
      end
    end
  end

  TTY::Table.new(
    [t('desc.column.label'), t('desc.column.name'), t('desc.column.type'), ''],
    rows)
end

#create_record_type_table(schema) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/alet/irb/command/describe.rb', line 122

def create_record_type_table(schema)
  rows  = schema.record_types
          .select{|rt| rt.active && rt.available}
          .map{|rt| [rt.name, rt.recordTypeId, rt.developerName]}

  TTY::Table.new(
    [t('desc.column.record_type_name'), t('desc.column.record_type_id'), t('desc.column.record_type_developer_name')],
    rows)
end

#execute(arg) ⇒ Object



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
# File 'lib/alet/irb/command/describe.rb', line 11

def execute(arg)
  pastel = Pastel.new
  argv = arg.split(' ')
  opt = OptionParser.new
  opt.on '-r', '--relation'
  opt.on '-t', '--record-type'
  opt.on '-a', '--all'

  params = {}
  opt.parse(argv, into: params)

  object_type = argv.first

  schema = SObjectModel::Schema.new(Alet.describe(object_type.to_sym))

  if params.has_key?(:relation)
    show_relations(schema)
  elsif params.has_key?(:"record-type")
    show_record_types(schema)
  elsif params.has_key?(:all)
    show_all(schema)
  else
    show_fields(schema)
  end
rescue SObjectModel::Rest::RequestError => e
  puts pastel.red(e.message)
rescue SObjectModel::Rest::RecordNotFoundError => e
  puts pastel.red(t('desc.error.notfound'))
end

#show_all(schema) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/alet/irb/command/describe.rb', line 68

def show_all(schema)
  field_table = create_field_table(schema)
  relation_table = crate_relation_table(schema)
  record_type_table = create_record_type_table(schema)

  sio = StringIO.new
  sio << t('desc.table.field')
  sio << "\n"
  sio << field_table.render(:unicode)
  sio << "\n"
  sio << "\n"
  sio << t('desc.table.relation')
  sio << "\n"
  sio << relation_table.render(:unicode)
  sio << "\n"
  sio << "\n"
  sio << t('desc.table.record_type')
  sio << "\n"
  sio << record_type_table.render(:unicode)
  IRB::Pager.page_content(sio.string)
end

#show_fields(schema) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/alet/irb/command/describe.rb', line 41

def show_fields(schema)
  table = create_field_table(schema)
  sio = StringIO.new
  sio << t('desc.table.field')
  sio << "\n"
  sio << table.render(:unicode)
  IRB::Pager.page_content(sio.string)
end

#show_record_types(schema) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/alet/irb/command/describe.rb', line 59

def show_record_types(schema)
  table = create_record_type_table(schema)
  sio = StringIO.new
  sio << t('desc.table.record_type')
  sio << "\n"
  sio << table.render(:unicode)
  IRB::Pager.page_content(sio.string)
end

#show_relations(schema) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/alet/irb/command/describe.rb', line 50

def show_relations(schema)
  table = crate_relation_table(schema)
  sio = StringIO.new
  sio << t('desc.table.relation')
  sio << "\n"
  sio << table.render(:unicode)
  IRB::Pager.page_content(sio.string)
end