Class: Flydata::Command::Base

Inherits:
Object
  • Object
show all
Includes:
ExclusiveRunnable, Flydata::CommandLoggable
Defined in:
lib/flydata/command/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExclusiveRunnable

included

Methods included from Flydata::CommandLoggable

#before_logging, #log_error_stderr, #log_info_stdout, #log_warn_stderr

Constructor Details

#initialize(options = Slop.new) ⇒ Base

Returns a new instance of Base.


16
17
18
19
# File 'lib/flydata/command/base.rb', line 16

def initialize(options = Slop.new)
  @api_client = ApiClient.instance
  @opts = options
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.


20
21
22
# File 'lib/flydata/command/base.rb', line 20

def opts
  @opts
end

Instance Method Details

#ask_input_table_nameObject


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/flydata/command/base.rb', line 102

def ask_input_table_name
  input = nil
  loop do
    log_info_stdout("Input a table name.")
    say(">> ")
    input = gets.strip
    if input =~ /^[a-zA-Z0-9_]+$/
      break
    else
      puts "Please enter a valid table name."
    end
  end
  log_info(">> #{input}")
  input
end

#ask_yes_no(message, default_yes = true) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flydata/command/base.rb', line 63

def ask_yes_no(message, default_yes=true)
  suffix = default_yes ? "(Y/n)" : "(y/n)"
  prompt = "#{message} #{suffix}:  "
  if opts && opts.yes? # Yes option
    log_info_stdout("#{prompt}Yes")
    return true
  end

  loop do
    ans = ask(prompt)
    return true if default_yes and ans == ''
    if ans.size > 0
      case ans[0].downcase
      when 'y'
        log_info("#{prompt}Yes")
        return true
      when 'n'
        log_info("#{prompt}No")
        return false
      end
    end
    log_warn_stderr(" ! Please answer y[es] or n[o]")
    newline
  end
end

#choose_one(message, prompt, menu_list, value_list = []) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/flydata/command/base.rb', line 88

def choose_one(message, prompt, menu_list, value_list=[])
  choice = nil
  value_list = menu_list unless menu_list.size == value_list.size
  log_info_stdout(message) if message
  choose do |menu|
    menu.index = :number
    menu.index_suffix = ") "
    menu.select_by = :index
    menu.prompt = prompt ? prompt : ">> "
    menu.choices(*menu_list) {|item| choice = value_list[menu_list.index(item)]}
  end
  log_info_stdout("  -> #{choice}")
  choice
end

#data_entryObject


43
44
45
46
47
# File 'lib/flydata/command/base.rb', line 43

def data_entry
  @de ||= retrieve_data_entries.first
  raise "There are no data entries." unless @de
  @de
end

#flydataObject


21
# File 'lib/flydata/command/base.rb', line 21

def flydata; @api_client end

#newlineObject

print console


59
# File 'lib/flydata/command/base.rb', line 59

def newline; puts end

#register_crontabObject


49
50
51
52
53
54
55
56
# File 'lib/flydata/command/base.rb', line 49

def register_crontab
  data_entries = retrieve_data_entries
  if data_entries.any?{|e| e['log_deletion']}
    # require on demand to avoid mutual require issue
    require 'flydata/command/crontab'
    Flydata::Command::Crontab.new.run
  end
end

#retrieve_data_entriesObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flydata/command/base.rb', line 23

def retrieve_data_entries
  data_entries = flydata.get('/data_entries')
  unless flydata.response.code == 200 and data_entries
    raise "Failed to retrieve data_entries"
  end
  data_entries.collect do |de|
    if Flydata::Preference::DataEntryPreference.conf_exists?(de)
      Flydata::Preference::DataEntryPreference.load_conf(de)
    else
      Flydata::Preference::DataEntryPreference.filter_data_entry(de)
      de
    end
  end
end

#separator(str = "=") ⇒ Object


60
61
62
# File 'lib/flydata/command/base.rb', line 60

def separator(str="=")
  puts str * 64
end

#show_purpose_nameObject


38
39
40
41
# File 'lib/flydata/command/base.rb', line 38

def show_purpose_name
  de = data_entry
  log_info_stdout("Your current application name is '#{de['purpose_name']}'")
end