Class: BrowserStakeout::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/browser_stakeout/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Options

Returns a new instance of Options.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/browser_stakeout/options.rb', line 5

def initialize(args)
  super()
  
  @orig_args = args.clone
  self[:safari]  = true
  self[:firefox] = false
  self[:index]   = "index\.htm[l]?"
  
  @opts = OptionParser.new do |o|
    o.banner = "Usage: #{File.basename($0)} [options] files-to-watch"
    
    o.on("-i", "--[no-]index FILENAME", 
            "Specify the file to refresh whenever changes are found") do |i|
      self[:index] = i
    end
    
    o.on("-s", "--[no-]safari", "Reload the page(s) in Safari") do |s|
      self[:safari] = s
    end
      
    o.on("-f", "--[no-]firefox", "Reload the page(s) in Firefox") do |f|
      self[:firefox] = f
    end
      
    o.on_tail("-h", "--help", "Show this message") do
      self[:show_help] = true
    end
  end
  
  begin
    @opts.parse!(args)
    @files = args
  rescue OptionParser::InvalidOption => e
    self[:invalid_argument] = e.message
  end
  

  # Decide which browsers need to be refreshed.
  # TODO: How can this be extended to include other browsers easily?
  @browsers = []
  @browsers << BrowserStakeout::Browsers::Safari if self[:safari]
  @browsers << BrowserStakeout::Browsers::Firefox if self[:firefox]
  raise "Zero browsers? Come on. Is that really what you want to do?" if @browsers.length == 0
end

Instance Attribute Details

#browsersObject (readonly)

Returns the value of attribute browsers.



3
4
5
# File 'lib/browser_stakeout/options.rb', line 3

def browsers
  @browsers
end

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/browser_stakeout/options.rb', line 3

def files
  @files
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/browser_stakeout/options.rb', line 3

def opts
  @opts
end

#orig_argsObject (readonly)

Returns the value of attribute orig_args.



3
4
5
# File 'lib/browser_stakeout/options.rb', line 3

def orig_args
  @orig_args
end

Instance Method Details

#find_index(files) ⇒ Object



50
51
52
# File 'lib/browser_stakeout/options.rb', line 50

def find_index(files)
  files.keys.find {|f| f =~ /#{self[:index]}/}
end