Module: Device::Helper

Included in:
IO, ParamsDat, ISO8583::Bitmap
Defined in:
lib/device/helper.rb

Instance Method Summary collapse

Instance Method Details

#attachObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/device/helper.rb', line 17

def attach
  Device::Display.clear
  puts "Connecting..."
  if Device::Network.connected? < 0
    if (ret = Device::Network.attach) == 0
      puts "Connected #{ret}"
    else
      puts "Attach fail #{ret}"
      sleep 4
      return false
    end
  else
    puts "Already connected"
  end
  true
end

#check_download_error(ret) ⇒ Object

TODO Add i18n or something



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
# File 'lib/device/helper.rb', line 35

def check_download_error(ret)
  value = true
  case ret
  when Device::Transaction::Download::SERIAL_NUMBER_NOT_FOUND
    puts "Serial number not found."
    value =  false
  when Device::Transaction::Download::FILE_NOT_FOUND
    puts "File not found."
    value = false
  when Device::Transaction::Download::FILE_NOT_CHANGE
    puts "File is the same."
  when Device::Transaction::Download::SUCCESS
    puts "Success."
  when Device::Transaction::Download::COMMUNICATION_ERROR
    puts "Communication failure."
    value = false
  when Device::Transaction::Download::MAPREDUCE_RESPONSE_ERROR
    puts "Encoding error."
    value = false
  when Device::Transaction::Download::IO_ERROR
    puts "IO Error."
    value = false
  else
    puts "Communication fail."
    value = false
  end

  value
end

#form(label, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/device/helper.rb', line 7

def form(label, options = {})
  Device::Display.clear
  options = form_default(options)
  default = options.delete(:default)
  puts "#{label} (#{default}):"
  string = get_format(options.delete(:min), options.delete(:max), options)
  return default if string.nil? || string.empty?
  string
end

#ljust(string, size, new_string) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/device/helper.rb', line 126

def ljust(string, size, new_string)
  string_plain = string.to_s
  if size > string_plain.size
    string_plain + (new_string * (size - string_plain.size))
  else
    string_plain
  end
end

:default => {:detail => 10, # default value to return if enter
:number => true, # Add number to label or not
"option X" => => 10,
"option Y" => => 11

}



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/device/helper.rb', line 71

def menu(title, selection, options = {})
  options[:number] = true if options[:number].nil?

  Device::Display.clear
  print_title(title, options[:default])
  values = Hash.new
  selection.each_with_index do |value,i|
    values[i.to_i] = value[1]
    if options[:number]
      Device::Display.print("#{i+1} - #{value[0]}", i+2, 0)
    else
      Device::Display.print(value[0], i+2, 0)
    end
  end

  key = getc

  return options[:default] if key == IO::ENTER || key == IO::CANCEL
  [values[key.to_i - 1]].flatten.first
end

#number_to_currency(value, options = {}) ⇒ Object



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
122
123
124
# File 'lib/device/helper.rb', line 92

def number_to_currency(value, options = {})
  options[:delimiter] ||= ","
  options[:precision] ||= 2
  options[:separator] ||= "."

  if value.is_a? Float
    number, unit = value.to_s.split(".")
    unit = unit.to_s
    len = number.size + unit.size
  else
    len    = value.to_s.size
    unit   = value.to_s[(len - options[:precision])..-1]
    if len <= options[:precision]
      number = ""
    else
      number = value.to_s[0..(len - (options[:precision] + 1)).abs]
    end
  end

  text = ""
  i = 0
  number.reverse.each_char do |ch|
    i += 1
    text << ch
    text << options[:delimiter] if (i % 3 == 0) && (len - unit.size) != i
  end
  currency = [rjust(text.reverse, 1, "0"),rjust(unit, options[:precision], "0")].join options[:separator]
  if options[:label]
    options[:label] + currency
  else
    currency
  end
end

#rjust(string, size, new_string) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/device/helper.rb', line 135

def rjust(string, size, new_string)
  string_plain = string.to_s
  if size > string_plain.size
    (new_string * (size - string_plain.size)) + string_plain
  else
    string_plain
  end
end