Class: Twat::Options

Inherits:
Object
  • Object
show all
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

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:



28
29
30
31
# File 'lib/twat/options.rb', line 28

def method_missing(sym, *args, &block)
  puts sym
  raise InvalidSetOpt
end

Class Method Details

.bool_false?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


10
11
12
# File 'lib/twat/options.rb', line 10

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

.bool_valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.int_valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#beep=(value) ⇒ Object

Raises:



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

def beep=(value)
  val = value.to_sym
  raise InvalidBool unless Options.bool_valid?(val)
  config[:beep] = val
  config.save!
end

#colors=(value) ⇒ Object

Raises:



50
51
52
53
54
55
# File 'lib/twat/options.rb', line 50

def colors=(value)
  val = value.to_sym
  raise InvalidBool unless Options.bool_valid?(val)
  config[:colors] = val
  config.save!
end

#configObject



33
34
35
# File 'lib/twat/options.rb', line 33

def config
  @config ||= Config.new
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.



40
41
42
43
44
45
46
47
48
# File 'lib/twat/options.rb', line 40

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

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

#polling_interval=(value) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/twat/options.rb', line 64

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