Skip to content

Instantly share code, notes, and snippets.

@meredian
Last active December 30, 2015 11:45
Show Gist options
  • Save meredian/4325787 to your computer and use it in GitHub Desktop.
Save meredian/4325787 to your computer and use it in GitHub Desktop.
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