lib/parameters/parameters.rb in parameters-0.2.3 vs lib/parameters/parameters.rb in parameters-0.3.0

- old
+ new

@@ -17,37 +17,11 @@ # @param [Hash] values # The names and values to initialize the instance parameters to. # def initialize_params(values={}) self.class.each_param do |param| - new_param = InstanceParam.new( - self, - param.name, - param.type, - param.description - ) - - # do not override existing instance value if present - if new_param.value.nil? - if param.value.kind_of?(Proc) - value = if param.value.arity > 0 - param.value.call(self) - else - param.value.call() - end - else - begin - value = param.value.clone - rescue TypeError - value = param.value - end - end - - new_param.value = value - end - - self.params[param.name] = new_param + self.params[param.name] = param.to_instance(self) end self.params = values if values.kind_of?(Hash) end @@ -86,38 +60,12 @@ # # @example # obj.parameter('var',:default => 3, :description => 'my variable') # def parameter(name,options={}) - name = name.to_sym - default = options[:default] + name = name.to_sym - # resolve the default value - if default.kind_of?(Proc) - value = if default.arity > 0 - default.call(self) - else - default.call() - end - else - value = default - end - - # create the new parameter - new_param = InstanceParam.new( - self, - name, - options[:type], - options[:description] - ) - - # set the instance variable - new_param.value = value - - # add the new parameter - self.params[name] = new_param - instance_eval %{ # define the reader method for the parameter def #{name} get_param(#{name.inspect}).value end @@ -126,10 +74,21 @@ def #{name}=(new_value) get_param(#{name.inspect}).value = new_value end } + # create the new parameter + new_param = InstanceParam.new( + self, + name, + options[:type], + options[:description], + options[:default] + ) + + # add the new parameter + self.params[name] = new_param return new_param end # # @return [Hash] @@ -160,15 +119,17 @@ def params=(values) values.each do |name,value| name = name.to_sym if has_param?(name) - if (value.kind_of?(Parameters::ClassParam) || value.kind_of?(Parameters::InstanceParam)) - value = value.value - end - - self.params[name].value = value + self.params[name].value = case value + when Parameters::ClassParam, + Parameters::InstanceParam + value.value + else + value + end end end end # @@ -210,17 +171,48 @@ # def get_param(name) name = name.to_sym unless has_param?(name) - raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.to_s.dump}",caller) + raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.inspect}") end return self.params[name] end # + # Sets an instance parameter. + # + # @param [Symbol, String] name + # The name of the instance parameter. + # + # @param [Object] value + # The new value for the instance parameter. + # + # @return [Object] + # The new value of the instance parameter. + # + # @raise [ParamNotfound] + # No instance parameter with the specified name could be found. + # + # @example + # obj.set_param('var',2) + # # => 2 + # + # @since 0.3.0 + # + def set_param(name,value) + name = name.to_sym + + unless has_param?(name) + raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.to_s.dump}") + end + + return self.params[name].value = value + end + + # # Returns the description of the parameter with a specific name. # # @param [Symbol, String] name # The name of the instance parameter to search for. # @@ -270,11 +262,11 @@ # def require_params(*names) names.each do |name| name = name.to_s - if instance_variable_get("@#{name}".to_sym).nil? - raise(Parameters::MissingParam,"parameter #{name.dump} has no value",caller) + if instance_variable_get(:"@#{name}").nil? + raise(Parameters::MissingParam,"parameter #{name.dump} has no value") end end return true end