lib/parameters/parameters.rb in parameters-0.3.1 vs lib/parameters/parameters.rb in parameters-0.4.0
- old
+ new
@@ -1,37 +1,49 @@
require 'parameters/exceptions'
require 'parameters/class_methods'
+require 'parameters/module_methods'
require 'parameters/class_param'
require 'parameters/instance_param'
require 'parameters/exceptions'
require 'parameters/extensions/meta'
module Parameters
def self.included(base)
base.extend ClassMethods
+
+ if base.kind_of?(Module)
+ # add Module specific methods
+ base.extend Parameters::ModuleMethods
+ end
end
#
# Initalizes the parameters of the object using the given
# values, which can override the default values of parameters.
#
# @param [Hash] values
# The names and values to initialize the instance parameters to.
#
+ # @api public
+ #
def initialize_params(values={})
- self.class.each_param do |param|
- self.params[param.name] = param.to_instance(self)
+ if self.class.included_modules.include?(Parameters)
+ self.class.each_param do |param|
+ self.params[param.name] = param.to_instance(self)
+ end
end
self.params = values if values.kind_of?(Hash)
end
#
# 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`.
#
+ # @api public
+ #
def initialize(*args,&block)
initialize_params(args.first)
end
#
@@ -59,10 +71,12 @@
# obj.parameter('var')
#
# @example
# obj.parameter('var',:default => 3, :description => 'my variable')
#
+ # @api public
+ #
def parameter(name,options={})
name = name.to_sym
instance_eval %{
# define the reader method for the parameter
@@ -72,10 +86,14 @@
# define the writer method for the parameter
def #{name}=(new_value)
get_param(#{name.inspect}).value = new_value
end
+
+ def #{name}?
+ !!get_param(#{name.inspect}).value
+ end
}
# create the new parameter
new_param = InstanceParam.new(
self,
@@ -92,20 +110,24 @@
#
# @return [Hash]
# The parameteres of the class and it's ancestors.
#
+ # @api semipublic
+ #
def class_params
self.class.params
end
#
# @return [Hash]
# The instance parameters of the object.
#
+ # @api semipublic
+ #
def params
- @_params ||= {}
+ @parameters ||= {}
end
#
# Sets the values of existing parameters in the object.
#
@@ -114,10 +136,12 @@
#
# @example
# obj.params = {:x => 5, :y => 2}
# # => {:x=>5, :y=>2}
#
+ # @api semipublic
+ #
def params=(values)
values.each do |name,value|
name = name.to_sym
if has_param?(name)
@@ -136,10 +160,12 @@
# Iterates over each instance parameter in the object.
#
# @yield [param]
# The block that will be passed each instance parameter.
#
+ # @api semipublic
+ #
def each_param(&block)
self.params.each_value(&block)
end
#
@@ -148,10 +174,12 @@
# specified name.
#
# @example
# obj.has_param?('rhost') # => true
#
+ # @api semipublic
+ #
def has_param?(name)
self.params.has_key?(name.to_sym)
end
#
@@ -167,10 +195,12 @@
# Could not find the instance parameter with the specified name.
#
# @example
# obj.get_param('var') # => InstanceParam
#
+ # @api semipublic
+ #
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.inspect}")
@@ -198,10 +228,12 @@
# obj.set_param('var',2)
# # => 2
#
# @since 0.3.0
#
+ # @api semipublic
+ #
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}")
@@ -223,10 +255,12 @@
# Could not find the instance parameter with the specified name.
#
# @example
# obj.describe_param('rhost') # => "remote host"
#
+ # @api semipublic
+ #
def describe_param(name)
get_param(name).description
end
#
@@ -242,10 +276,12 @@
# Could not find the instance parameter with the specified name.
#
# @example
# obj.param_value('rhost') # => 80
#
+ # @api semipublic
+ #
def param_value(name)
get_param(name).value
end
protected
@@ -257,9 +293,11 @@
# @return [true]
# All the instance parameters have non `nil` values.
#
# @raise [MissingParam]
# One of the instance parameters was not set.
+ #
+ # @api public
#
def require_params(*names)
names.each do |name|
name = name.to_s