lib/parameters/parameters.rb in parameters-0.1.9 vs lib/parameters/parameters.rb in parameters-0.2.0
- old
+ new
@@ -17,39 +17,46 @@
# @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 instance_variable_get("@#{param.name}".to_sym).nil?
- begin
- if param.value.kind_of?(Proc)
- value = if param.value.arity > 0
- param.value.call(self)
- else
- param.value.call()
- end
- else
+ 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
- rescue TypeError
- value = param.value
end
- instance_variable_set("@#{param.name}".to_sym,value)
+ new_param.value = value
end
- self.params[param.name] = InstanceParam.new(self,param.name,param.description)
+ self.params[param.name] = new_param
end
self.params = values if values.kind_of?(Hash)
end
#
- # Initializes the parameters using initialize_params. If a +Hash+
+ # Initializes the parameters using initialize_params. If a `Hash`
# is passed in as the first argument, it will be used to set the values
- # of parameters described within the Hash.
+ # of parameters described within the `Hash`.
#
def initialize(*args,&block)
initialize_params(args.first)
end
@@ -60,16 +67,19 @@
# The name for the new instance parameter.
#
# @param [Hash] options
# Additional options.
#
- # @option options [String] :description
- # The description for the new parameter.
+ # @option options [Class, Array[Class]] :type
+ # The type to enforce the new parameter values to.
#
# @option options [Object, Proc] :default
# The default value for the new parameter.
#
+ # @option options [String] :description
+ # The description for the new parameter.
+ #
# @return [InstanceParam]
# The newly created instance parameter.
#
# @example
# obj.parameter('var')
@@ -78,11 +88,10 @@
# obj.parameter('var',:default => 3, :description => 'my variable')
#
def parameter(name,options={})
name = name.to_sym
default = options[:default]
- description = options[:description]
# resolve the default value
if default.kind_of?(Proc)
value = if default.arity > 0
default.call(self)
@@ -91,29 +100,37 @@
end
else
value = default
end
+ # create the new parameter
+ new_param = InstanceParam.new(
+ self,
+ name,
+ options[:type],
+ options[:description]
+ )
+
# set the instance variable
- instance_variable_set("@#{name}".to_sym,value)
+ new_param.value = value
# add the new parameter
- self.params[name] = InstanceParam.new(self,name,description)
+ self.params[name] = new_param
instance_eval %{
# define the reader method for the parameter
def #{name}
- instance_variable_get("@#{name}".to_sym)
+ get_param(#{name.inspect}).value
end
# define the writer method for the parameter
- def #{name}=(value)
- instance_variable_set("@#{name}".to_sym,value)
+ def #{name}=(new_value)
+ get_param(#{name.inspect}).value = new_value
end
}
- return params[name]
+ return new_param
end
#
# @return [Hash]
# The parameteres of the class and it's ancestors.
@@ -241,13 +258,13 @@
protected
#
# Requires that the instance parameters with specific names have
- # non +nil+ values.
+ # non `nil` values.
#
# @return [true]
- # All the instance parameters have non +nil+ values.
+ # All the instance parameters have non `nil` values.
#
# @raise [MissingParam]
# One of the instance parameters was not set.
#
def require_params(*names)