lib/rubikon/action.rb in rubikon-0.1.0 vs lib/rubikon/action.rb in rubikon-0.2.0
- old
+ new
@@ -7,12 +7,10 @@
# Instances of the Action class are used to define the real code that should
# be executed when running the application.
class Action
- @@action_count = 0
-
attr_reader :block, :description, :name, :param_type
# Create a new Action using the given name, options and code block.
#
# +name+:: The name of this Action, used in Application options
@@ -22,41 +20,46 @@
# Options:
# +description+:: A description of the action. This isn't used at the
# moment.
# +param_type+:: A single Class or a Array of classes that represent the
# type(s) of argument(s) this action expects
- def initialize(name, options = {}, &block)
+ def initialize(options = {}, &block)
raise BlockMissingError unless block_given?
- @name = name
-
@description = options[:description] || ''
@param_type = options[:param_type] || Object
@block = block
+ @arg_count = block.arity
end
# Run this action's code block
#
# +args+:: The argument which should be relayed to the block of this Action
def run(*args)
- if (@block.arity >= 0 and args.size < @block.arity) or (@block.arity < 0 and args.size < -@block.arity - 1)
- raise MissingArgumentError
- end
+ raise MissingArgumentError unless check_argument_count(args.size)
raise Rubikon::ArgumentTypeError unless check_argument_types(args)
@block[*args]
end
private
+ # Checks if the number of arguments given fits the number of arguments of
+ # this Action
+ #
+ # +count+:: The number of arguments
+ def check_argument_count(count)
+ !((@arg_count >= 0 && count < @arg_count) || (@arg_count < 0 && count < -@arg_count - 1))
+ end
+
# Checks the types of the given arguments using the Class or Array of
# classes given in the +:param_type+ option of this action.
#
# +args+:: The arguments which should be checked
def check_argument_types(args)
if @param_type.is_a? Array
- args.each_index do |i|
- return false unless args[i].is_a? @param_type[i]
+ args.each_index do |arg_index|
+ return false unless args[arg_index].is_a? @param_type[arg_index]
end
else
args.each do |arg|
return false unless arg.is_a? @param_type
end