class Calabash::Cucumber::Preferences

Users preferences persisted across runs:

~/.calabash/preferences/preferences.json

Constants

VALID_USAGE_TRACKING_VALUES

@!visibility private

Ordered by permissiveness left to right ascending.

“system_info” implies that “events” are also allowed.

VERSION

@!visibility private

The preferences version

Attributes

path[R]

@!visibility private

Public Class Methods

new() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 15
def initialize
  dot_dir = Calabash::Cucumber::DotDir.directory
  @path = File.join(dot_dir, "preferences", "preferences.json")
end

Public Instance Methods

inspect() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 27
def inspect
  to_s
end
to_s() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 21
def to_s
  puts "Preferences:"
  ap read
end
usage_tracking() click to toggle source

!@visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 32
def usage_tracking
  preferences = read

  unless valid_user_tracking_value?(preferences[:usage_tracking])
    log_defaults_reset
    preferences[:usage_tracking] = defaults[:usage_tracking]
    write(preferences)
  end

  preferences[:usage_tracking]
end
usage_tracking=(value) click to toggle source

!@visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 45
def usage_tracking=(value)
  if !valid_user_tracking_value?(value)
    raise ArgumentError,
      "Expected '#{value}' to be one of #{VALID_USAGE_TRACKING_VALUES.join(", ")}"
  end

  preferences = read
  preferences[:usage_tracking] = value
  write(preferences)
end
user_id() click to toggle source

!@visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 57
def user_id
  preferences = read

  unless valid_user_id?(preferences[:user_id])
    preferences[:user_id] = SecureRandom.uuid
    write(preferences)
  end

  preferences[:user_id]
end
user_id=(value) click to toggle source

!@visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 69
def user_id=(value)
  if !valid_user_id?(value)
    raise ArgumentError,
      "Expected '#{value}' to not be nil and not an empty string"
  end

  preferences = read
  preferences[:user_id] = value
  write(preferences)
end

Private Instance Methods

defaults() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 121
def defaults
  {
    :version => VERSION,
    :usage_tracking => "system_info",
    :user_id => SecureRandom.uuid
  }
end
ensure_preferences_dir() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 113
def ensure_preferences_dir
  dir = File.dirname(@path)
  unless File.exist?(dir)
    FileUtils.mkdir_p(dir)
  end
end
generate_json(hash) click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 155
def generate_json(hash)
  begin
    JSON.pretty_generate(hash)
  rescue TypeError, JSON::UnparserError => _

    log_defaults_reset

    # Will always generate valid JSON
    generate_json(defaults)
  end
end
log_defaults_reset() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 196
      def log_defaults_reset
        RunLoop.log_warn(
%q{An error occurred while accessing your user preferences.

We have reset the preferences to the default settings.

If this happens on a regular basis, please create a GitHub issue.

Your preferences control various Calabash behaviors.  In particular, they tell
us how much usage information you are willing to share.  If you have previously
turned off usage tracking, you will need to disable it again using the command
line tools or the irb.

We do not recommend that edit the preferences file by hand.
})
      end
parse_json(string) click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 182
def parse_json(string)
  begin
    JSON.parse(string, {:symbolize_names => true})
  rescue TypeError, JSON::ParserError => _

    log_defaults_reset

    hash = defaults
    write(hash)
    hash
  end
end
read() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 168
def read
  if File.exist?(path)

    string = File.read(path).force_encoding("UTF-8")

    parse_json(string)
  else
    hash = defaults
    write(hash)
    hash
  end
end
valid_user_id?(value) click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 88
def valid_user_id?(value)
  !value.nil? && value != "" && value.is_a?(String)
end
valid_user_tracking_value?(value) click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 83
def valid_user_tracking_value?(value)
  VALID_USAGE_TRACKING_VALUES.include?(value)
end
version() click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 105
def version
  read[:version]
end
write(hash) click to toggle source

@!visibility private

# File lib/calabash-cucumber/store/preferences.rb, line 130
def write(hash)
  if hash.nil?
    raise ArgumentError, "Hash to write cannot be nil"
  end

  if !hash.is_a?(Hash)
    raise ArgumentError, "Expected a Hash argument"
  end

  if hash.count == 0
    raise ArgumentError, "Hash to write cannot be empty"
  end

  string = generate_json(hash)

  ensure_preferences_dir

  File.open(path, "w:UTF-8") do |file|
    file.write(string)
  end

  true
end