Sha256: 40474ea501ca70aa0ae96d640ed01ad1968bdc55c69e38f3df658916c705251f

Contents?: true

Size: 1.56 KB

Versions: 16

Compression:

Stored size: 1.56 KB

Contents

module JsDuck
  module Options

    # Stores values of command line options.
    #
    # All options are initially defined with an #attribute method, which
    # ensures that accessing an unexisting option will result in an
    # error.
    class Record
      def initialize
        @validators = {}
      end

      # 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

      # Defines a validator function that gets run after all the
      # options have been parsed.  When validation fails, the function
      # should return an error message string (or an array of string
      # for multi-line error message) otherwise nil, to signify
      # success.
      def validator(name, &block)
        @validators[name] = block
      end

      # Runs all the validators.  Returns an error message string from
      # the first failed validation or nil when everything is OK.
      #
      # Alternatively runs just one validator by name. Used in testing.
      def validate!(name=nil)
        validators = name ? [@validators[name]] : @validators.values

        validators.each do |block|
          if err = block.call()
            return err
          end
        end
        return nil
      end

    end

  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/options/record.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.10 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.9 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.8 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.7 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.5 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.4 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.3 lib/jsduck/options/record.rb
jsduck-troopjs-0.0.1 lib/jsduck/options/record.rb
jsduck-6.0.0beta lib/jsduck/options/record.rb