Sha256: f7dfa9de30b6a45987bd11cea5564cbf4d6b5e60f44769a4948ebff990262e21

Contents?: true

Size: 1.01 KB

Versions: 13

Compression:

Stored size: 1.01 KB

Contents

require 'ns-options/option'

module NsOptions

  class Options

    def initialize
      @hash = Hash.new
    end

    # for hash with indifferent access behavior
    def [](name);         @hash[name.to_s];         end
    def []=(name, value); @hash[name.to_s] = value; end

    def keys(*args, &block);   @hash.keys(*args, &block);   end
    def each(*args, &block);   @hash.each(*args, &block);   end
    def empty?(*args, &block); @hash.empty?(*args, &block); end

    def add(name, opt)
      self[name] = opt
    end

    def rm(name)
      @hash.delete(name.to_s)
    end

    def get(name)
      (option = self[name]) ? option.value : nil
    end

    def set(name, new_value)
      self[name].value = new_value
    end

    def required_set?
      are_all_these_options_set? required_options
    end

    private

    def are_all_these_options_set?(options)
      options.inject(true) {|bool, opt| bool && opt.is_set?}
    end

    def required_options
      @hash.values.reject{|opt| !opt.required? }
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ns-options-1.1.6 lib/ns-options/options.rb
ns-options-1.1.5 lib/ns-options/options.rb
ns-options-1.1.4 lib/ns-options/options.rb
ns-options-1.1.3 lib/ns-options/options.rb
ns-options-1.1.2 lib/ns-options/options.rb
ns-options-1.1.1 lib/ns-options/options.rb
ns-options-1.1.0 lib/ns-options/options.rb
ns-options-1.0.1 lib/ns-options/options.rb
ns-options-1.0.0 lib/ns-options/options.rb
ns-options-1.0.0.rc4 lib/ns-options/options.rb
ns-options-1.0.0.rc3 lib/ns-options/options.rb
ns-options-1.0.0.rc2 lib/ns-options/options.rb
ns-options-1.0.0.rc1 lib/ns-options/options.rb