Commit 03d01229 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract OptionsRecord class from Options.

Storing the actual options in a separate OptionsRecord while processing
them inside Options class.

Lots of changes because a myriad of instance variable references needed
to be changed to point at @opts record.
parent 8ee55a5f
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ require 'jsduck/app'
require 'jsduck/options'

opts = JsDuck::Options.new
opts.parse!(ARGV)
exit_code = JsDuck::App.new(opts).run
exit_code = JsDuck::App.new(opts.parse(ARGV)).run

exit exit_code
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ module JsDuck

  # The main application logic of jsduck
  class App
    # Initializes app with JsDuck::Options object
    # Initializes app with JsDuck::OptionsRecord object
    def initialize(opts)
      @opts = opts
    end
+198 −208

File changed.

Preview size limit exceeded, changes collapsed.

+34 −0
Original line number Diff line number Diff line
module JsDuck

  # Stores values of command line options.
  #
  # Options can be accessed using normal accessor methods or with
  # Hash-like :[] and :[]= interface.
  #
  # All options are initially defined with an #attribute method, which
  # ensures that accessing an unexisting option will result in an
  # error.
  class OptionsRecord

    # Defines accessor for an option,
    # and assigns a default value for it.
    def attribute(name, default=nil)
      instance_variable_set("@#{name}", default)
      # Use `send` to invoke private attr_accessor method.  As we only
      # expect a single OptionsRecord to exist for the lifetime of the
      # app, it should be safe to define a method on a class.
      self.class.send(:attr_accessor, name)
    end

    # Make options object behave like hash.
    # This allows us to substitute it with hash in unit tests.
    def [](key)
      instance_variable_get("@#{key}")
    end
    def []=(key, value)
      instance_variable_set("@#{key}", value)
    end

  end

end