Class: Twat::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/twat/config.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object



51
52
53
# File 'lib/twat/config.rb', line 51

def method_missing(meth)
  self[meth]
end

Instance Method Details

#[]=(k, v) ⇒ Object



73
74
75
# File 'lib/twat/config.rb', line 73

def []=(k, v)
  config[k] = v
end

#accountObject



108
109
110
# File 'lib/twat/config.rb', line 108

def 
  accounts[]
end

#account=(acct) ⇒ Object

I don’t know how rubyists feel about method returning something vastly different to what method= accepts, but I think the api makes sense



87
88
89
90
91
92
93
# File 'lib/twat/config.rb', line 87

def account=(acct)
  if accounts.include?(acct)
    @account = acct
  else
    raise NoSuchAccount
  end
end

#account_nameObject



99
100
101
102
103
104
105
106
# File 'lib/twat/config.rb', line 99

def 
  if 
    @account
  else
    raise NoDefaultAccount unless config.include?(:default)
    return config[:default].to_sym
  end
end

#account_set?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/twat/config.rb', line 95

def 
  !!@account
end

#accountsObject



55
56
57
# File 'lib/twat/config.rb', line 55

def accounts
  return config[:accounts]
end

#beep?Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/twat/config.rb', line 64

def beep?
  beep = config[:beep] || "false"
  Options.bool_true?(beep)
end

#colors?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/twat/config.rb', line 59

def colors?
  colors = config[:colors] || "true"
  Options.bool_true?(colors)
end

#configObject



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

def config
  begin
    @config ||= YAML.load_file(config_path)
    validate!(@config)
  rescue Errno::ENOENT
    raise NoConfigFile
  end
end

#config_pathObject



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

def config_path
  @config_path ||= ENV['TWAT_CONFIG'] || "#{ENV['HOME']}/.twatrc"
end

#create_unless_exists!Object



21
22
23
24
25
26
27
# File 'lib/twat/config.rb', line 21

def create_unless_exists!
  begin
    config
  rescue NoConfigFile
    @config = { accounts: {} }
  end
end

#endpointObject



116
117
118
# File 'lib/twat/config.rb', line 116

def endpoint
  @endpoint ||= Endpoint.new(endpoint_name)
end

#endpoint_nameObject



112
113
114
# File 'lib/twat/config.rb', line 112

def endpoint_name
  [:endpoint] || :twitter
end

#polling_intervalObject



69
70
71
# File 'lib/twat/config.rb', line 69

def polling_interval
  config[:polling_interval] || 60
end

#save!Object



44
45
46
47
48
49
# File 'lib/twat/config.rb', line 44

def save!
  File.open(config_path, 'w') do |conf|
    conf.chmod(0600)
    conf.puts(@config.to_yaml)
  end
end

#update!Object

update! migrates an old config file to the current API it does this by calling a sequence of migration functions in order which rebuild the config in stages, saving and leaving it in a consistent step at each point



81
82
83
# File 'lib/twat/config.rb', line 81

def update!
  Migrate.new.migrate!(config_path)
end

#validate!(conf) ⇒ Object



38
39
40
41
42
# File 'lib/twat/config.rb', line 38

def validate!(conf)
  # TODO Proper checks, instead of a series of hackish checks
  raise ConfigVersionIncorrect unless conf.include?(:accounts)
  conf
end