Module: Pwss::Fields

Defined in:
lib/pwss/generators/fields.rb

Constant Summary collapse

INPUT_F =
0
DEFAULT =
1
HIDDEN =
2
FIELDS =

this is a set of fields useful for different types of entries see the constants above to make sense of the fields for each entry different types of entries will use the appropriate set of fields

{
  # everyone has...
  "title"       => ["Readline.readline('title: ')", "''", false],
  "url"         => ["Readline.readline('url: ')", "''", false],
  "username"    => ["Readline.readline('username: ')", "''", false],
  "recovery_email" => ["Readline.readline('email: ')", "''", false],
  "password"    => ["Pwss::Password.password(arguments)", "Pwss::Password.ask_password_twice('password')", true],
  "description" => ["get_lines", "''", false],

  # banks also have ...
  "name"        => ["Readline.readline('name: ')", "''", false],
  "iban"        => ["Readline.readline('iban: ')", "ITkk xaaa aabb bbbc cccc cccc ccc", false],

  # cards also have
  "issuer"       => ["Readline.readline('issuer: ')", "''", false],
  "name_on_card" => ["Readline.readline('name on card: ')", "''", false],
  "card_number"  => ["Readline.readline('number: ')", "''", true],
  "valid_from"   => ["Readline.readline('valid from: ')", "''", false],
  "valid_till"   => ["Readline.readline('valid till: ')", "''", false],
  "verification_number" => ["Readline.readline('verification number: ')", "''", true],
  "pin"          => ["Readline.readline('pin: ')", "''", true],

  # SIMs also have
  "puk"         => ["Readline.readline('puk: ')", "XXXX", true],
  "phone"       => ["Readline.readline('phone: ')", "NNN NNN NNNN", false],

  # Code has only title and code
  "code"         => ["Readline.readline('code: ')", "XXXX", true],
  
  # useful for software licenses
  "version"        => ["Readline.readline('version: ')", "''", false],
  "licensed_to"    => ["Readline.readline('licensed to: ')", "''", false],
  "license_number" => ["Readline.readline('license number: ')", "''", true],
  "purchased_on"   => ["Readline.readline('purchased on: ')", "Date.today", false],
}

Class Method Summary collapse

Class Method Details

.ask(key, arguments) ⇒ Object

ask the value for key

This is performed by invoking the function defined for key in the FIELDS, which typically asks for user input. If the user enters a value this is the one the function returns, otherwise we return the default value defined for key in FIELDS.

Optional hash arguments contains a list of arguments to be passed to the function in FIELDS. This allows to customize the behaviour of the user-input function.

**As a special case, if arguments contains key, this is returned as the value. This allows to set the default for a key outside this module.**

Thus, for instance: ask ‘username’, => ‘a’ will return ‘a’.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pwss/generators/fields.rb', line 68

def self.ask key, arguments
  # if the default is specified outside this class, return it!
  return arguments[key] if arguments[key]

  # ... otherwise, do some work and ask for the value!
  input_f = FIELDS[key] ? FIELDS[key][INPUT_F] : "Readline.readline('#{key}: ')"
  value = eval input_f
  if value != nil and value != "" then
    value
  else
    FIELDS[key] ? eval(FIELDS[key][DEFAULT]) : nil
  end
end

.get_linesObject

read n-lines (terminated by a “.”)



83
84
85
86
87
88
89
90
91
92
# File 'lib/pwss/generators/fields.rb', line 83

def self.get_lines
  puts "description (terminate with '.'):"
  lines = []
  line = ""
  until line == "."
    line = Readline.readline
    lines << line if line != "."
  end
  lines.join("\n")
end

.sensitiveObject



116
117
118
# File 'lib/pwss/generators/fields.rb', line 116

def self.sensitive
  FIELDS.select { |x| FIELDS[x][HIDDEN] }.keys
end

.sensitive?(field) ⇒ Boolean

custom keys are always considered to be sensitive

Returns:

  • (Boolean)


112
113
114
# File 'lib/pwss/generators/fields.rb', line 112

def self.sensitive? field
  FIELDS[field] ?  FIELDS[field][HIDDEN] : true
end

.to_clean_hash(hash) ⇒ Object

take a hash as input and reorder the fields according to the order in which the fields are defined in the FIELDS variable

this function is used to present records in the YAML file always in the same order.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pwss/generators/fields.rb', line 99

def self.to_clean_hash hash
  output = Hash.new
  FIELDS.keys.each do |field|
    output[field] = hash[field] if hash[field]
  end
  # all the remaining fields (i.e., user-defined fields in records)
  (hash.keys - FIELDS.keys).each do |field|
    output[field] = hash[field]
  end
  output
end