Class: Twat::Options

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/twat/options.rb

Constant Summary collapse

BOOL_TRUE =
["yes", "true", "1", "on"]
BOOL_FALSE =
["no", "false", "0", "off"]
BOOL_VALS =
BOOL_TRUE + BOOL_FALSE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Exceptions

#with_handled_exceptions

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

A set of wrappers around the global config object to set given attributes Catching failures is convenient because of the method_missing? hook

Raises:



42
43
44
# File 'lib/twat/options.rb', line 42

def method_missing(sym, *args, &block)
  raise InvalidSetOpt, "Options doesn't know about #{sym}"
end

Class Method Details

.bool_false?(val) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/twat/options.rb', line 17

def self.bool_false?(val)
  BOOL_FALSE.include?(val.to_s.downcase)
end

.bool_true?(val) ⇒ Boolean

TODO - Some meta programming to just define boolean values ala attr_accessor

Returns:

  • (Boolean)


13
14
15
# File 'lib/twat/options.rb', line 13

def self.bool_true?(val)
  BOOL_TRUE.include?(val.to_s.downcase)
end

.bool_valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/twat/options.rb', line 21

def self.bool_valid?(val)
  BOOL_VALS.include?(val.to_s.downcase)
end

.int_valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/twat/options.rb', line 25

def self.int_valid?(val)
  !!/^[0-9]+$/.match(val)
end

.to_bool(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/twat/options.rb', line 29

def self.to_bool(value)
  case
  when bool_true?(value)
    return true
  when bool_false?(value)
    return false
  else
    raise InvalidBool
  end
end

Instance Method Details

#beep=(value) ⇒ Object



70
71
72
73
# File 'lib/twat/options.rb', line 70

def beep=(value)
  config[:beep] = Options.to_bool(value)
  config.save!
end

#colors=(value) ⇒ Object



65
66
67
68
# File 'lib/twat/options.rb', line 65

def colors=(value)
  config[:colors] = Options.to_bool(value)
  config.save!
end

#configObject



46
47
48
49
50
# File 'lib/twat/options.rb', line 46

def config
  @config ||= Config.new.tap do |n|
    raise NoConfigFile unless n.exists?
  end
end

#default=(value) ⇒ Object

This is deliberately not abstracted (it could be easily accessed from withing the method_missing method, but that will just lead to nastiness later when I implement colors, for example.



55
56
57
58
59
60
61
62
63
# File 'lib/twat/options.rb', line 55

def default=(value)
  value = value.to_sym
  unless config.accounts.include?(value)
    raise NoSuchAccount
  end

  config[:default] = value
  config.save!
end

#polling_interval=(value) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/twat/options.rb', line 80

def polling_interval=(value)
  raise InvalidInt unless Options.int_valid?(value)
  val = value.to_i
  if val < 15 then
    puts "Polling intervals of < 15secs will exceed your daily API requests"
    exit
  else
    config[:polling_interval] = val
    config.save!
  end
end

#show_mentions=(value) ⇒ Object



75
76
77
78
# File 'lib/twat/options.rb', line 75

def show_mentions=(value)
  config[:show_mentions] = Options.to_bool(value)
  config.save!
end