lib/parameters/parameters.rb in parameters-0.1.0 vs lib/parameters/parameters.rb in parameters-0.1.1

- old
+ new

@@ -124,10 +124,19 @@ # exception will be raised. # def param_value(name) get_param(name).value end + + # + # Print the class parameters to the given _output_ stream. + # + def print_params(output=STDOUT) + each_param do |param| + output.puts param + end + end end end # # Initalizes the parameters of the object using the given @@ -145,11 +154,11 @@ end instance_variable_set("@#{param.name}",value) end - params[param.name] = InstanceParam.new(self,param.name,param.description) + self.params[param.name] = InstanceParam.new(self,param.name,param.description) end end # # Initializes the parameters using initialize_parameters. If a +Hash+ @@ -183,11 +192,11 @@ # set the instance variable instance_variable_set("@#{name}",options[:default]) # add the new parameter - params[name] = InstanceParam.new(self,name,options[:description]) + self.params[name] = InstanceParam.new(self,name,options[:description]) instance_eval %{ # define the reader method for the parameter def #{name} instance_variable_get("@#{name}") @@ -223,23 +232,31 @@ # # => {:x=>5, :y=>2} # def params=(values) values.each do |name,value| if has_param?(name) - instance_variable_set("@#{name}",value) + self.params[name.to_sym].value = value end end end # + # Iterates over each instance parameter, passing each one to the given + # _block_. + # + def each_param(&block) + self.params.each_value(&block) + end + + # # Returns +true+ if the a parameter with the specified _name_ exists, # returns +false+ otherwise. # # obj.has_param?('rhost') # => true # def has_param?(name) - params.has_key?(name.to_sym) + self.params.has_key?(name.to_sym) end # # Returns the parameter with the specified _name_. If no such parameter # exists, a ParamNotFound exception will be raised. @@ -251,11 +268,11 @@ unless has_param?(name) raise(ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.to_s.dump}",caller) end - return params[name] + return self.params[name] end # # Returns the description of the parameter with the specified _name_. # If no such parameter exists, a ParamNotFound exception will be raised. @@ -275,16 +292,32 @@ def param_value(name) get_param(name).value end # - # Sets the values of the parameters listed in the specified _values_. + # Print the instance parameters to the given _output_ stream. # - # obj.set_params(:rhost => 'www.example.com', :rport => 80) - # # => {:rhost=>"www.example.com", :rport=>80} + def print_params(output=STDOUT) + each_param do |param| + output.puts param + end + end + + protected + # - def set_params(values={}) - values.each do |name,value| - get_param(name).value = value + # Requires that the parameters with the specified _names_ have + # non +nil+ values. If a parameter with a +nil+ value is found + # a MissingParam exception will be raised. + # + def require_params(*names) + names.each do |name| + name = name.to_s + + unless instance_variable_get("@#{name}") + raise(MissingParam,"parameter #{name.dump} has no value",caller) + end end + + return true end end