module Pwss::Fields

Constants

DEFAULT
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

HIDDEN
INPUT_F

Public Class Methods

ask(key, arguments) click to toggle source

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', {'username' => 'a'} will return 'a'.

# 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_lines() click to toggle source

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

# 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
sensitive() click to toggle source
# File lib/pwss/generators/fields.rb, line 116
def self.sensitive
  FIELDS.select { |x| FIELDS[x][HIDDEN] }.keys
end
sensitive?(field) click to toggle source

custom keys are always considered to be sensitive

# File lib/pwss/generators/fields.rb, line 112
def self.sensitive? field
  FIELDS[field] ?  FIELDS[field][HIDDEN] : true
end
to_clean_hash(hash) click to toggle source

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.

# 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