Last active
December 30, 2015 11:45
-
-
Save meredian/4325787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Models | |
module CommandRunner | |
def self.included(base) | |
base.class_eval do | |
@commands = [] | |
class << self; | |
attr_reader :commands; | |
def command name, options=nil | |
commands.add_command name, options | |
end | |
end | |
def commands; self.class.commands; end | |
class << @commands | |
klass = self | |
define_method(:add_command) do |command_name, options| | |
if options && options[:params] | |
klass.send(:define_method, :"#{command_name}_check_params") do |params| | |
raise Errors::MissingCommandParameter.new("Params hash missing") unless params | |
options[:params].each do |param_name| | |
raise Errors::MissingCommandParameter.new(param_name) unless params.include?(param_name) | |
end | |
end | |
end | |
send(:<<, command_name) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment