Class: Rmate::Settings

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rmate.rb', line 21

def initialize
  @host, @port = 'localhost', 52698

  @wait    = false
  @force   = false
  @verbose = false
  @lines   = []
  @names   = []
  @types   = []

  read_disk_settings

  @host = ENV['RMATE_HOST'].to_s if ENV.has_key? 'RMATE_HOST'
  @port = ENV['RMATE_PORT'].to_i if ENV.has_key? 'RMATE_PORT'

  parse_cli_options

  @host = parse_ssh_connection if @host == 'auto'
end

Instance Attribute Details

#forceObject

Returns the value of attribute force.



19
20
21
# File 'lib/rmate.rb', line 19

def force
  @force
end

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/rmate.rb', line 19

def host
  @host
end

#linesObject

Returns the value of attribute lines.



19
20
21
# File 'lib/rmate.rb', line 19

def lines
  @lines
end

#namesObject

Returns the value of attribute names.



19
20
21
# File 'lib/rmate.rb', line 19

def names
  @names
end

#portObject

Returns the value of attribute port.



19
20
21
# File 'lib/rmate.rb', line 19

def port
  @port
end

#typesObject

Returns the value of attribute types.



19
20
21
# File 'lib/rmate.rb', line 19

def types
  @types
end

#verboseObject

Returns the value of attribute verbose.



19
20
21
# File 'lib/rmate.rb', line 19

def verbose
  @verbose
end

#waitObject

Returns the value of attribute wait.



19
20
21
# File 'lib/rmate.rb', line 19

def wait
  @wait
end

Instance Method Details

#parse_cli_optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rmate.rb', line 52

def parse_cli_options
  OptionParser.new do |o|
    o.on(           '--host=name',       "Connect to host.", "Use 'auto' to detect the host from SSH.", "Defaults to #{@host}.") { |v| @host    = v          }
    o.on('-p',      '--port=#', Integer, "Port number to use for connection.", "Defaults to #{@port}.")                          { |v| @port    = v          }
    o.on('-w',      '--[no-]wait',       'Wait for file to be closed by TextMate.')                                              { |v| @wait    = v          }
    o.on('-l',      '--line [NUMBER]',   'Place caret on line [NUMBER] after loading file.')                                     { |v| @lines <<= v          }
    o.on('-m',      '--name [NAME]',     'The display name shown in TextMate.')                                                  { |v| @names <<= v          }
    o.on('-t',      '--type [TYPE]',     'Treat file as having [TYPE].')                                                         { |v| @types <<= v          }
    o.on('-f',      '--force',           'Open even if the file is not writable.')                                               { |v| @force   = v          }
    o.on('-v',      '--verbose',         'Verbose logging messages.')                                                            { |v| @verbose = v          }
    o.on_tail('-h', '--help',            'Show this message.')                                                                   { puts o; exit              }
    o.on_tail(      '--version',         'Show version.')                                                                        { puts VERSION_STRING; exit }
    o.parse!
  end
end

#parse_ssh_connectionObject



68
69
70
# File 'lib/rmate.rb', line 68

def parse_ssh_connection
  ENV['SSH_CONNECTION'].nil? ? 'localhost' : ENV['SSH_CONNECTION'].split(' ').first
end

#read_disk_settingsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/rmate.rb', line 41

def read_disk_settings
  [ "/etc/rmate.rc", "~/.rmate.rc"].each do |current_file|
    file = File.expand_path current_file
    if File.exist? file
      params = YAML::load(File.open(file))
      @host = params["host"] unless params["host"].nil?
      @port = params["port"] unless params["port"].nil?
    end
  end
end